changeset 86:f97c99f193fd

Popular and New menus are now populated, but the New one is erroneous--see the TODO in the comments for more information.
author Atul Varma <varmaa@toolness.com>
date Thu, 13 Mar 2008 18:23:59 -0500
parents c6dc5b79a07a
children 663f0410ff39
files PersonasBackend/personas/json_feeds.py
diffstat 1 files changed, 27 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/PersonasBackend/personas/json_feeds.py	Thu Mar 13 18:02:45 2008 -0500
+++ b/PersonasBackend/personas/json_feeds.py	Thu Mar 13 18:23:59 2008 -0500
@@ -3,6 +3,9 @@
 
 from PersonasBackend.personas import models
 
+NEW_MENU_ID = "personas-new-menu"
+POPULAR_MENU_ID = "personas-popular-menu"
+
 def _makeJsonResponse( obj ):
     json = simplejson.dumps( obj, indent=4 )
     response = HttpResponse( json )
@@ -11,11 +14,11 @@
 
 def categories( request ):
     categoryDicts = [
-        {"label": "Most Popular", "id" : "personas-popular-menu",
+        {"label": "Most Popular", "id" : POPULAR_MENU_ID,
          "type" : "list", "parent" : "top" },
 	{"label": "Recent", "id" : "personas-recent-menu",
          "type" : "recent", "parent" : "top" },
-	{"label": "New", "id" : "personas-new-menu",
+	{"label": "New", "id" : NEW_MENU_ID,
          "type" : "list", "parent" : "top" },
 	{"label": "All", "id" : "personas-category-menu",
          "type" : "category", "parent" : "top" },
@@ -34,7 +37,27 @@
 def personas( request ):
     personaDicts = []
 
-    for persona in models.Persona.objects.filter( status="published" ):
+    isPopular = {}
+    isNew = {}
+
+    published = models.Persona.objects.filter( status="published" )
+
+    for persona in published.order_by( "-popularity" )[:10]:
+        isPopular[persona.id] = True
+
+    # TODO: This isn't really a good way of determining what the
+    # newest Personas are, because, for instance, the date_updated
+    # field is changed whenever a Persona's popularity is increased.
+    for persona in published.order_by( "-date_updated" )[:10]:
+        isNew[persona.id] = True
+
+    for persona in published:
+        categories = [persona.category.json_id]
+        if persona.id in isPopular:
+            categories.append( POPULAR_MENU_ID )
+        if persona.id in isNew:
+            categories.append( NEW_MENU_ID )
+
         personaDicts.append(
             {"label" : persona.name,
              "id" : persona.json_id,
@@ -42,7 +65,7 @@
              "baseURL" : "",
              "headerURL" : persona.get_header_url(),
              "footerURL" : persona.get_footer_url(),
-             "menu" : persona.category.json_id,
+             "menu" : ",".join( categories ),
              "preview" : "yes"}
             )