[extensions-web] Add a simple view to query for extensions



commit 9c586acd1da7e2e5318dcf821b0da394623f8369
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Oct 26 12:14:11 2011 -0400

    Add a simple view to query for extensions

 sweettooth/extensions/models.py |   13 ++++++++++++-
 sweettooth/extensions/urls.py   |    2 ++
 sweettooth/extensions/views.py  |   31 +++++++++++++++++++++++--------
 3 files changed, 37 insertions(+), 9 deletions(-)
---
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 914154f..e42e40f 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -87,7 +87,7 @@ class InvalidShellVersion(Exception):
     pass
 
 class ShellVersionManager(models.Manager):
-    def get_for_version_string(self, version_string):
+    def parse_version_string(self, version_string):
         version = version_string.split('.', 2)
         major, minor = version[:2]
         major, minor = int(major), int(minor)
@@ -102,6 +102,17 @@ class ShellVersionManager(models.Manager):
             # Two-digit odd versions are illegal: 3.1, 3.3
             raise InvalidShellVersion()
 
+        return major, minor, point
+
+    def lookup_for_version_string(self, version_string):
+        major, minor, point = self.parse_version_string(version_string)
+        try:
+            return self.get(major=major, minor=minor, point=point)
+        except self.model.DoesNotExist:
+            return None
+
+    def get_for_version_string(self, version_string):
+        major, minor, point = self.parse_version_string(version_string)
         obj, created = self.get_or_create(major=major, minor=minor, point=point)
         return obj
 
diff --git a/sweettooth/extensions/urls.py b/sweettooth/extensions/urls.py
index b3db280..43e1564 100644
--- a/sweettooth/extensions/urls.py
+++ b/sweettooth/extensions/urls.py
@@ -19,6 +19,8 @@ ajax_patterns = patterns('',
 )
 
 shell_patterns = patterns('',
+    url(r'^extension-query/', views.ajax_query_view),
+
     url(r'^extension-info/', views.ajax_details_view),
 
     url(r'^download-extension/(?P<uuid>.+)\.shell-extension\.zip$',
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 494dccd..8e4c92d 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -150,6 +150,14 @@ def ajax_image_upload_view(field):
         obj.save()
     return inner 
 
+def ajax_details(extension):
+    return dict(pk = extension.pk,
+                uuid = extension.uuid,
+                name = extension.name,
+                creator = extension.creator.username,
+                link = reverse('extensions-detail', kwargs=dict(pk=extension.pk)),
+                icon = extension.icon.url)
+
 @ajax_view
 def ajax_details_view(request):
     uuid = request.GET.get('uuid', None)
@@ -158,17 +166,24 @@ def ajax_details_view(request):
         raise Http404()
 
     extension = get_object_or_404(models.Extension, uuid=uuid)
+    return ajax_details(extension)
 
-    data = dict(pk = extension.pk,
-                uuid = extension.uuid,
-                name = extension.name,
-                creator = extension.creator.username,
-                link = reverse('extensions-detail', kwargs=dict(pk=extension.pk)))
+ ajax_view
+def ajax_query_view(request):
+    query_params = {}
+
+    versions = request.GET.getlist('shell_version')
+    if versions:
+        versions = [models.ShellVersion.lookup_for_version_string(v) for v in versions]
+        versions = [v for v in versions if v is not None]
+        query_params['versions__shell_versions__in'] = versions
 
-    if extension.icon:
-        data['icon'] = extension.icon.url
+    uuids = request.GET.getlist('uuid')
+    if uuids:
+        query_params['uuid__in'] = uuids
 
-    return data
+    extensions = models.Extension.filter(**query_params)
+    return [ajax_details(e) for e in extensions]
 
 @login_required
 def upload_file(request, pk):



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