[extensions-web] extensions: Export shell version map on list page



commit a01f26cf3f88d0a4797326474ca42e1d9cab2e2f
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Dec 9 11:57:11 2011 -0500

    extensions: Export shell version map on list page

 sweettooth/extensions/models.py                    |   25 ++++++++++++++++++++
 .../extensions/templates/extensions/list.html      |    2 +-
 sweettooth/extensions/views.py                     |   25 ++-----------------
 3 files changed, 29 insertions(+), 23 deletions(-)
---
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 5c0a20e..7554a20 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -45,6 +45,23 @@ class ExtensionManager(models.Manager):
     def visible(self):
         return self.filter(versions__status__in=VISIBLE_STATUSES).distinct()
 
+def build_shell_version_map(versions):
+    shell_version_map = {}
+    for version in versions:
+        for shell_version in version.shell_versions.all():
+            key = shell_version.version_string
+            if key not in shell_version_map:
+                shell_version_map[key] = version
+
+            if version.version > shell_version_map[key].version:
+                shell_version_map[key] = version
+
+    for key, version in shell_version_map.iteritems():
+        shell_version_map[key] = dict(pk = version.pk,
+                                      version = version.version)
+
+    return shell_version_map
+
 class Extension(models.Model):
     name = models.CharField(max_length=200)
     uuid = models.CharField(max_length=200, unique=True, db_index=True)
@@ -109,6 +126,14 @@ class Extension(models.Model):
             if version.source:
                 version.replace_metadata_json()
 
+    @property
+    def visible_shell_version_map(self):
+        return build_shell_version_map(self.visible_versions)
+
+    @property
+    def visible_shell_version_map_json(self):
+        return json.dumps(self.visible_shell_version_map)
+
 class InvalidShellVersion(Exception):
     pass
 
diff --git a/sweettooth/extensions/templates/extensions/list.html b/sweettooth/extensions/templates/extensions/list.html
index ef5692d..9b4b33c 100644
--- a/sweettooth/extensions/templates/extensions/list.html
+++ b/sweettooth/extensions/templates/extensions/list.html
@@ -10,7 +10,7 @@
   {% endif %}
 
   {% for extension in extension_list %}
-    <li class="extension">
+    <li class="extension" data-svm="{{ extension.visible_shell_version_map_json }}">
       <h3 class="extension-name"><a href="{% url extensions-detail pk=extension.pk %}" class="title-link"><img src="{{ extension.icon.url }}" class="icon">{{ extension.name }}</a></h3>
       <span class="author">by <a href="{% url auth-profile user=extension.creator.username %}">{{ extension.creator }}</a></span>
       <p class="description">
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 8b5f339..6ea2cad 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -74,23 +74,6 @@ def shell_update(request):
 
     return operations
 
-def build_shell_version_map(versions):
-    shell_version_map = {}
-    for version in versions:
-        for shell_version in version.shell_versions.all():
-            key = shell_version.version_string
-            if key not in shell_version_map:
-                shell_version_map[key] = version
-
-            if version.version > shell_version_map[key].version:
-                shell_version_map[key] = version
-
-    for key, version in shell_version_map.iteritems():
-        shell_version_map[key] = dict(pk = version.pk,
-                                      version = version.version)
-
-    return shell_version_map
-
 def extensions_list(request):
     queryset = models.Extension.objects.visible()
     if request.GET.get('sort', '') == 'recent':
@@ -141,9 +124,7 @@ def extension_view(request, obj, **kwargs):
     else:
         template_name = "extensions/detail.html"
 
-    shell_version_map = build_shell_version_map(versions)
-
-    context = dict(shell_version_map = json.dumps(shell_version_map),
+    context = dict(shell_version_map = obj.visible_shell_version_map_json,
                    extension = extension,
                    all_versions = extension.versions.order_by('-version'),
                    is_visible = True,
@@ -263,7 +244,7 @@ def ajax_details(extension):
                 creator = extension.creator.username,
                 link = reverse('extensions-detail', kwargs=dict(pk=extension.pk)),
                 icon = extension.icon.url,
-                shell_version_map = build_shell_version_map(extension.visible_versions))
+                shell_version_map = extension.visible_shell_version_map_json)
 
 @ajax_view
 def ajax_details_view(request):
@@ -318,7 +299,7 @@ def ajax_set_status_view(request, newstatus):
     context = dict(version=version,
                    extension=extension)
 
-    return dict(svm=build_shell_version_map(extension.visible_versions),
+    return dict(svm=extension.visible_shell_version_map_json,
                 mvs=render_to_string('extensions/multiversion_status.html', context))
 
 @login_required



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]