Anonymous avatar Anonymous committed 0b1fda9

optimize query

Comments (0)

Files changed (1)

newsmeme/models.py

         the user or c) posts authored by friends
         """
 
-        if user is None:
-            return self.filter(Post.access==Post.PUBLIC)
-
-        if user.is_moderator:
+        if user and user.is_moderator:
             return self
 
-        return self.filter(db.or_(Post.access==Post.PUBLIC,
-                                  Post.author_id==user.id,
-                                  db.and_(Post.access==Post.FRIENDS, 
-                                          Post.author_id.in_(user.friends))))
+        criteria = [Post.access==Post.PUBLIC]
 
+        if user:
+            criteria.append(Post.author_id==user.id)
+            if user.friends:
+                criteria.append(db.and_(Post.access==Post.FRIENDS,
+                                        Post.author_id.in_(user.friends)))
+        
+        return self.filter(reduce(db.or_, criteria))
 
     def search(self, keywords):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.