[extensions-web] Implement multiversion extensions page



commit 4cdab7959b42e8b13e99dcd781006dffa48626df
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Nov 23 22:43:33 2011 -0500

    Implement multiversion extensions page
    
    Rather than have one active "latest" version, allow multiple active versions,
    with the best choice being selected by JavaScript. This means that extension
    authors can target multiple shell versions, simply by uploading two different
    extension versions with shell versions.

 .../extensions/templates/extensions/detail.html    |    3 +-
 sweettooth/extensions/urls.py                      |    4 +-
 sweettooth/extensions/views.py                     |   36 +++++++++++++++----
 sweettooth/static/js/extensionUtils.js             |   26 ++++++++++++++
 sweettooth/static/js/extensions.js                 |   11 ++++--
 5 files changed, 65 insertions(+), 15 deletions(-)
---
diff --git a/sweettooth/extensions/templates/extensions/detail.html b/sweettooth/extensions/templates/extensions/detail.html
index 4f5fb08..2a6bb0f 100644
--- a/sweettooth/extensions/templates/extensions/detail.html
+++ b/sweettooth/extensions/templates/extensions/detail.html
@@ -24,8 +24,7 @@
 
     <div class="extension"
          data-uuid="{{ extension.uuid }}"
-         data-sv="{{ version.shell_versions_json }}"
-         data-pk="{{ version.pk }}">
+         data-svm="{{ shell_version_map }}">
       <div class="switch{% if is_rejected %} insensitive{% endif %}"></div>
 
       {% block icon %}
diff --git a/sweettooth/extensions/urls.py b/sweettooth/extensions/urls.py
index a596b17..0a7624a 100644
--- a/sweettooth/extensions/urls.py
+++ b/sweettooth/extensions/urls.py
@@ -41,9 +41,9 @@ urlpatterns = patterns('',
         views.extension_version_view, name='extensions-version-detail'),
 
     url(r'^extension/(?P<pk>\d+)/(?P<slug>.+)/$',
-        views.extension_latest_version_view, name='extensions-detail'),
+        views.extension_view, name='extensions-detail'),
     url(r'^extension/(?P<pk>\d+)/$',
-        views.extension_latest_version_view, dict(slug=None), name='extensions-detail'),
+        views.extension_view, dict(slug=None), name='extensions-detail'),
 
     url(r'^local/', direct_to_template, dict(template='extensions/local.html'), name='extensions-local'),
 
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 33c5a24..ea38ddb 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -71,12 +71,28 @@ def shell_update(request):
 
     return operations
 
-# Even though this is showing a version, the PK matches an extension
+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
+
 @model_view(models.Extension)
-def extension_latest_version_view(request, obj, **kwargs):
-    extension, version = obj, obj.latest_version
+def extension_view(request, obj, **kwargs):
+    extension, versions = obj, obj.visible_versions
 
-    if version is None:
+    if versions.count() == 0 and not extension.user_can_edit(request.user):
         raise Http404()
 
     # Redirect if we don't match the slug.
@@ -93,11 +109,11 @@ def extension_latest_version_view(request, obj, **kwargs):
     else:
         template_name = "extensions/detail.html"
 
-    status = version.status
-    context = dict(version = version,
+    shell_version_map = build_shell_version_map(versions)
+
+    context = dict(shell_version_map = json.dumps(shell_version_map),
                    extension = extension,
-                   is_visible = status in models.VISIBLE_STATUSES,
-                   status = status)
+                   is_visible = True)
     return render(request, template_name, context)
 
 @model_view(models.ExtensionVersion)
@@ -135,8 +151,12 @@ def extension_version_view(request, obj, **kwargs):
     else:
         template_name = "extensions/detail.html"
 
+    version_obj = dict(pk = version.pk, version = version.version)
+    shell_version_map = dict((sv.version_string, version_obj) for sv in version.shell_versions.all())
+
     context = dict(version = version,
                    extension = extension,
+                   shell_version_map = json.dumps(shell_version_map),
                    is_preview = is_preview,
                    is_visible = status in models.VISIBLE_STATUSES,
                    is_rejected = status in models.REJECTED_STATUSES,
diff --git a/sweettooth/static/js/extensionUtils.js b/sweettooth/static/js/extensionUtils.js
index 9b25403..3afdb95 100644
--- a/sweettooth/static/js/extensionUtils.js
+++ b/sweettooth/static/js/extensionUtils.js
@@ -38,6 +38,32 @@ define([], function() {
         return false;
     };
 
+    module.grabProperExtensionVersion = function(map, current) {
+        var versionA = map[current];
+
+        var parts = current.split('.');
+
+        // Unstable releases
+        if (parseInt(parts[1]) % 2 != 0) {
+            if (versionA !== undefined)
+                return versionA;
+            else
+                return null;
+        }
+
+        var versionB = map[(parts[0] + '.' + parts[1])];
+
+        if (versionA !== undefined && versionB !== undefined) {
+            return (versionA.version > versionB.version) ? versionA : versionB;
+        } else if (versionA !== undefined) {
+            return versionA;
+        } else if (versionB !== undefined) {
+            return versionB;
+        } else {
+            return null;
+        }
+    };
+
     return module;
 
 });
diff --git a/sweettooth/static/js/extensions.js b/sweettooth/static/js/extensions.js
index b780b9e..6fd1700 100644
--- a/sweettooth/static/js/extensions.js
+++ b/sweettooth/static/js/extensions.js
@@ -45,18 +45,23 @@ function($, messages, dbusProxy, extensionUtils) {
     };
 
     function addExtensionSwitch(uuid, extension, $elem) {
-        var shellVersions = $elem.data('sv');
-
         var $switch = $elem.find('.switch');
         var _state = ExtensionState.UNINSTALLED;
+        var svm = $elem.data('svm');
+
+        if (!svm)
+            _state = ExtensionState.OUT_OF_DATE;
+
+        var vpk = extensionUtils.grabProperExtensionVersion(svm, dbusProxy.ShellVersion);
 
-        if (shellVersions && !versionCheck(shellVersions, dbusProxy.ShellVersion)) {
+        if (vpk === null) {
             _state = ExtensionState.OUT_OF_DATE;
         } else if (extension && !$.isEmptyObject(extension)) {
             _state = extension.state;
         }
 
         $elem.data({'elem': $elem,
+                    'pk': (vpk === null ? 0 : vpk.pk),
                     'state': _state,
                     'uninstalled': false,
                     'undo-uninstall-message': null});



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