Have /entity/pick return a JSON object instead of a user view
authorSverre Rabbelier <srabbelier@gmail.com>
Sun, 25 Jan 2009 11:44:25 +0000
changeset 977 d212d5f4c41a
parent 976 d1a9c7179bbb
child 978 e05b09b53486
Have /entity/pick return a JSON object instead of a user view This is in preparation of a patch that will add a more fancy picker with JS. Patch by: Sverre Rabbelier
app/soc/models/base.py
app/soc/views/models/base.py
--- a/app/soc/models/base.py	Sun Jan 25 11:31:40 2009 +0000
+++ b/app/soc/models/base.py	Sun Jan 25 11:44:25 2009 +0000
@@ -47,7 +47,21 @@
   """
 
   _fields_cache = None
-  
+
+  def toDict(self):
+    """Returns a dict with all StringProperty values of this entity
+    """
+
+    result = {}
+
+    for key, value in self.properties().iteritems():
+      # Skip everything but StringProperties
+      if not isinstance(value, db.StringProperty):
+        continue
+      result[key] = getattr(self, key)
+
+    return result
+
   @classmethod
   def fields(cls):
     """Called by the Django template engine during template instantiation.
--- a/app/soc/views/models/base.py	Sun Jan 25 11:31:40 2009 +0000
+++ b/app/soc/views/models/base.py	Sun Jan 25 11:44:25 2009 +0000
@@ -24,6 +24,8 @@
   ]
 
 
+import simplejson
+
 from django import http
 from django.utils.translation import ugettext
 
@@ -566,8 +568,10 @@
       params: a dict with params for this View
     """
 
+    if not simplejson:
+      raise Exception("Simplejson not installed")
+
     get_dict = request.GET
-    filter = {}
 
     # scope_path is not required
     scope_path = get_dict.get('scope_path', None)
@@ -575,11 +579,26 @@
     field = get_dict['field']
 
     if scope_path:
+      filter = {}
       filter['scope_path'] = scope_path
+      data = data = self._logic.getForFields(filter)
+    else:
+      data = self._logic.getForLimitAndOffset(1000)
+
+    data = [i.toDict() for i in data]
 
-    redirect = redirects.getReturnRedirect(return_url, field)
-    return self.select(request, self, redirect, page_name=page_name,
-                       params=params, filter=filter)
+    to_json = {
+        'data': data,
+        'return_url': return_url,
+        'field': field,
+        }
+
+    json = simplejson.dumps(to_json)
+
+    context = {'json': json}
+    template = 'soc/json.html'
+
+    return helper.responses.respond(request, template, context)
 
   def _editPost(self, request, entity, fields):
     """Performs any required processing on the entity to post its edit page.