php avatar php committed 8d25e34

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.

Comments (0)

Files changed (7)

hgscm/apps/www/models.py

 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'''
         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"))

hgscm/apps/www/templatetags/extras.py

 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()
 
         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)

hgscm/apps/www/views.py

 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", { },
     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)

hgscm/settings.py

     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',

hgscm/templates/about.html

 		<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>

hgscm/templates/fragments/downloadbutton.html

+<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 %}

hgscm/templates/frontpage.html

 {% extends "base.html" %}
 
+{% load extras %}
 {% block content %}
 
 <div class="row">
 		<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>
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.