Commits

Anonymous committed 09003e5

optimized queries

Comments (0)

Files changed (3)

newsmeme/models.py

         for post in self.all():
             yield post.json
 
+    def as_list(self):
+        """
+        Return restricted list of columns for list queries
+        """
+
+        deferred_cols = ("description", 
+                         "tags",
+                         "author.email",
+                         "author.password",
+                         "author.activation_key",
+                         "author.openid",
+                         "author.date_joined",
+                         "author.receive_email",
+                         "author.email_alerts",
+                         "author.followers",
+                         "author.following")
+
+
+        options = [db.defer(col) for col in deferred_cols]
+        return self.options(*options)
+        
     def deadpooled(self):
         return self.filter(Post.score <= 0)
 

newsmeme/views/frontend.py

 @keep_login_url
 def index(page=1):
     
-    page_obj = Post.query.current().hottest().restricted(g.user).\
+    page_obj = Post.query.current().hottest().\
+        restricted(g.user).as_list().\
         paginate(page, per_page=PER_PAGE)
         
     page_url = lambda page: url_for("frontend.index", page=page)
 @keep_login_url
 def latest(page=1):
     
-    page_obj = Post.query.current().restricted(g.user).\
+    page_obj = Post.query.current().restricted(g.user).as_list().\
         paginate(page, per_page=PER_PAGE)
 
     page_url = lambda page: url_for("frontend.latest", page=page)
 @frontend.route("/deadpool/<int:page>/")
 @keep_login_url
 def deadpool(page=1):
-    page_obj = Post.query.deadpooled().restricted(g.user).\
+    page_obj = Post.query.deadpooled().restricted(g.user).as_list().\
         paginate(page, per_page=PER_PAGE)
 
     page_url = lambda page: url_for("frontend.deadpool", page=page)
         return redirect(url_for("frontend.index"))
     
     page_obj = Post.query.search(keywords).restricted(g.user).\
-        paginate(page, per_page=PER_PAGE)
+        as_list().paginate(page, per_page=PER_PAGE)
 
     if page_obj.total == 1:
 
 def tag(slug, page=1):
     tag = Tag.query.filter_by(slug=slug).first_or_404()
 
-    page_obj = tag.posts.restricted(g.user).\
+    page_obj = tag.posts.restricted(g.user).as_list().\
         paginate(page, per_page=PER_PAGE)
+
     page_url = lambda page: url_for('frontend.tag',
                                     slug=slug,
                                     page=page)

newsmeme/views/user.py

     user = User.query.filter_by(username=username).first_or_404()
 
     page_obj = Post.query.filter_by(author=user).restricted(g.user).\
-        paginate(page, PER_PAGE)
+        as_list().paginate(page, PER_PAGE)
     
     page_url = lambda page: url_for('user.posts',
                                     username=username,