changeset 78:8d25e34c21c2

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.
author David Soria Parra <dsp@php.net>
date Fri, 20 Feb 2009 15:12:43 +0100
parents fb737a306703
children 50012ff8f920
files hgscm/apps/www/models.py hgscm/apps/www/templatetags/extras.py hgscm/apps/www/views.py hgscm/settings.py hgscm/templates/about.html hgscm/templates/fragments/downloadbutton.html hgscm/templates/frontpage.html
diffstat 7 files changed, 66 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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"))
--- 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)
--- 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)
--- 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',
--- 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 @@
 		<p>Mercurial is used for version control of files. Similar projects include <a href="http://git-scm.org">Git</a> and <a href="http://bazaar-vcs.org">Bazaar</a>, and other version control systems without a distributed architecture include <a href="http://subversion.tigris.org/">Subversion</a> and <a href="http://www.nongnu.org/cvs/">CVS</a>.
 		</div>
 		<div class="col">
-			<h2>Download Mercurial</h2>
-			<a class="download typeface-js" href="javascript:void(0);">
-			<strong>Download now</strong>
-			Mercurial <em>2.42</em>
-			<span>Windows XP | Vista | 7</span>
-			</a>
+                {% download_button %}
 	            {% mercurial_tricks %}
 		</div>
 	</div>
--- /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 @@
+<a class="download typeface-js" href="{{download_url}}">
+			<strong>Download now</strong>
+			Mercurial <em>{{ latest_version }}</em>
+			<span>{{ download_system }}</span>
+</a>
+{% if extended %}
+<dl>
+    <dt class="typeface-js">Requirements</dt>
+    <dd>Python 2.4 (<a href="http://www.python.org">get python</a>)</dd>
+    <!--2.4 is necessary for TortoiseHG, Mercurial only needs 2.3-->
+
+    <dt>Another OS?<br><em>Get mercurial for:</em></dt>
+    <dd><a href="{% url download "latest" "macosx" %}">Mac OS X</a></dd>
+    <dd><a href="{% url download "latest" "windows" %}">Windows</a></dd>
+    <dd><a href="{% url downloads %}">other</a></dd>
+</dl>
+{% endif %}
--- 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 %}
 
 <div class="row">
@@ -8,21 +9,7 @@
 		<h2>Mercurial is a free, distributed source control management tool. It efficiently handles projects of any size and offers an easy and intuitive interface. <strong>Please notice, this page is currently under development and based on a mockup.</strong></h2>
 	</div>
 	<div class="col">					
-		<a class="download typeface-js" href="{% url download "latest" "source"%}">
-			<strong>Download now</strong>
-			Mercurial <em>{{ latest_version }}</em>
-			<span>Source</span>
-		</a>
-		<dl>
-			<dt class="typeface-js">Requirements</dt>
-			<dd>Python 2.4 (<a href="http://www.python.org">get python</a>)</dd>
-			<!--2.4 is necessary for TortoiseHG, Mercurial only needs 2.3-->
-			
-			<dt>Another OS?<br><em>Get mercurial for:</em></dt>
-			<dd><a href="{% url download "latest" "macosx" %}">Mac OS X</a></dd>
-			<dd><a href="{% url download "latest" "windows" %}">Windows</a></dd>
-			<dd><a href="{% url downloads %}">other</a></dd>
-		</dl>				
+        {% download_button 'true' %}
 	</div>
 </div>