# HG changeset patch # User David Soria Parra # Date 1235139163 -3600 # Node ID 8d25e34c21c2576bffea11123c9f85c7058526c7 # Parent fb737a306703fa40f0093c772082fc949202dc9a templatetags: Add DownloadButton tag to display a download button according to operating system This is somewhat a hacky way to do it, but it works, and we might redo it alter on. diff -r fb737a306703 -r 8d25e34c21c2 hgscm/apps/www/models.py --- a/hgscm/apps/www/models.py Fri Feb 20 15:12:33 2009 +0100 +++ b/hgscm/apps/www/models.py Fri Feb 20 15:12:43 2009 +0100 @@ -1,7 +1,7 @@ from django.db import models from django.utils import simplejson from django.conf import settings -import os +import os, re def get_download(platform, version): '''get the download for the right version''' @@ -13,7 +13,19 @@ if (latest and entry['latest'] == 'true') or entry['version'] == version: for version in entry['versions']: if version['identifier'] == platform: - return version['url'] + return version +def get_download_for_agent(agent, version): + '''get the download for the right version''' + f = open(os.path.join(settings.MEDIA_ROOT, "downloads.json")) + list = simplejson.load(f) + f.close() + latest = version == 'latest' or not version + for entry in list: + if (latest and entry['latest'] == 'true') or entry['version'] == version: + for version in entry['versions']: + if re.search(version['system'], agent): + return version + def get_latest_version(): '''return the latest available version''' f = open(os.path.join(settings.MEDIA_ROOT, "downloads.json")) diff -r fb737a306703 -r 8d25e34c21c2 hgscm/apps/www/templatetags/extras.py --- a/hgscm/apps/www/templatetags/extras.py Fri Feb 20 15:12:33 2009 +0100 +++ b/hgscm/apps/www/templatetags/extras.py Fri Feb 20 15:12:43 2009 +0100 @@ -1,6 +1,8 @@ from django import template from django.conf import settings -import random, os +from django.template import Context +from hgscm.apps.www.models import get_latest_version, get_download_for_agent, get_download +import random, os, re register = template.Library() @@ -18,7 +20,29 @@ f.close() return result +class DownloadButtonNode(template.Node): + def __init__(self, extended): + self._extended = extended + def render(self, context): + agent = context['request'].META['HTTP_USER_AGENT'] + t = template.loader.get_template('fragments/downloadbutton.html') + c = Context() + + version = get_download_for_agent(agent, 'latest') + if not version: + version = get_download('source', 'latest') + c['download_system'] = version['system'] + c['download_url'] = version['url'] + c['latest_version'] = get_latest_version() + c['extended'] = self._extended + return t.render(c) + def do_mercurial_tricks (parser, token): return MercurialTricksNode() +def do_download_button(parser, token): + extended = len(token.split_contents()) > 1 + return DownloadButtonNode(extended) + register.tag('mercurial_tricks', do_mercurial_tricks) +register.tag('download_button', do_download_button) diff -r fb737a306703 -r 8d25e34c21c2 hgscm/apps/www/views.py --- a/hgscm/apps/www/views.py Fri Feb 20 15:12:33 2009 +0100 +++ b/hgscm/apps/www/views.py Fri Feb 20 15:12:43 2009 +0100 @@ -7,7 +7,7 @@ import os def frontpage(request): - return render_to_response("frontpage.html", { 'latest_version': get_latest_version() }, + return render_to_response("frontpage.html", { }, RequestContext(request)) def about(request): return render_to_response("about.html", { }, @@ -16,7 +16,7 @@ return render_to_response("thepage.html", { }, RequestContext(request)) def download(request, platform, version): - return HttpResponseRedirect(get_download(platform, version)) + return HttpResponseRedirect(get_download(platform, version)['url']) def downloads(request): f = open(os.path.join(settings.MEDIA_ROOT, "downloads.json")) list = simplejson.load(f) diff -r fb737a306703 -r 8d25e34c21c2 hgscm/settings.py --- a/hgscm/settings.py Fri Feb 20 15:12:33 2009 +0100 +++ b/hgscm/settings.py Fri Feb 20 15:12:43 2009 +0100 @@ -73,6 +73,11 @@ os.path.join(BASE_DIR, "templates"), ) +TEMPLATE_CONTEXT_PROCESSORS = ( + 'django.core.context_processors.request', + 'django.core.context_processors.media', +) + INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', diff -r fb737a306703 -r 8d25e34c21c2 hgscm/templates/about.html --- a/hgscm/templates/about.html Fri Feb 20 15:12:33 2009 +0100 +++ b/hgscm/templates/about.html Fri Feb 20 15:12:43 2009 +0100 @@ -34,12 +34,7 @@

Mercurial is used for version control of files. Similar projects include Git and Bazaar, and other version control systems without a distributed architecture include Subversion and CVS.

-

Download Mercurial

- - Download now - Mercurial 2.42 - Windows XP | Vista | 7 - + {% download_button %} {% mercurial_tricks %}
diff -r fb737a306703 -r 8d25e34c21c2 hgscm/templates/fragments/downloadbutton.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hgscm/templates/fragments/downloadbutton.html Fri Feb 20 15:12:43 2009 +0100 @@ -0,0 +1,17 @@ + + Download now + Mercurial {{ latest_version }} + {{ download_system }} + +{% if extended %} +
+
Requirements
+
Python 2.4 (get python)
+ + +
Another OS?
Get mercurial for:
+
Mac OS X
+
Windows
+
other
+
+{% endif %} diff -r fb737a306703 -r 8d25e34c21c2 hgscm/templates/frontpage.html --- a/hgscm/templates/frontpage.html Fri Feb 20 15:12:33 2009 +0100 +++ b/hgscm/templates/frontpage.html Fri Feb 20 15:12:43 2009 +0100 @@ -1,5 +1,6 @@ {% extends "base.html" %} +{% load extras %} {% block content %}
@@ -8,21 +9,7 @@

Mercurial is a free, distributed source control management tool. It efficiently handles projects of any size and offers an easy and intuitive interface. Please notice, this page is currently under development and based on a mockup.

- - Download now - Mercurial {{ latest_version }} - Source - -
-
Requirements
-
Python 2.4 (get python)
- - -
Another OS?
Get mercurial for:
-
Mac OS X
-
Windows
-
other
-
+ {% download_button 'true' %}