Anonymous avatar Anonymous committed d2f1415

basequery class

Comments (0)

Files changed (3)

irc_reader/__init__.py

     @app.route("/channel/<int:channel_id>/")
     @app.route("/channel/<int:channel_id>/<int:page>/")
     def channel(channel_id, page=1):
+
         channel = Channel.query.get_or_404(channel_id)
+        
         page_obj = Message.query.filter_by(channel=channel).\
             paginate(page, get_config('PER_PAGE'))
 
     @app.route("/nick/<nick>/")
     @app.route("/nick/<nick>/<int:page>/")
     def nick(nick, page=1):
+        
         page_obj = Message.query.filter_by(nick=nick).\
-            options(db.eagerload('channel')).\
-                paginate(page, get_config('PER_PAGE'))
+            with_channel().paginate(page, get_config('PER_PAGE'))
 
         page_url = lambda page: \
             url_for('nick', nick=nick, page=page)
     @app.route("/search/<int:channel_id>/")
     @app.route("/search/<int:channel_id>/<int:page>/")
     def search(channel_id, page=1):
+        
         channel = Channel.query.get_or_404(channel_id)
 
         search = request.args.get('search', '')
-        if search:
-            search = '%' + search + '%'
-            q = db.or_(Message.nick.like(search),
-                       Message.message.like(search))
 
-            q = q & db.and_(Message.channel_id==channel.id)
+        page_obj = Message.query.filter_by(channel=channel).\
+            with_channel().search(search).\
+                paginate(page, get_config('PER_PAGE'))
 
-            page_url = lambda page: \
-                url_for('search', page=page,
-                                  channel_id=channel.id,
-                                  search=search)
-
-            page_obj = Message.query.filter(q).\
-                options(db.eagerload('channel')).\
-                    paginate(page, get_config('PER_PAGE'))
-
-        else:
-            page_obj = page_url = None
+        page_url = lambda page: \
+            url_for('search', page=page,
+                              channel_id=channel.id,
+                              search=search)
 
         return render_template('search.html',
                                search=search,

irc_reader/models.py

-from flaskext.sqlalchemy import SQLAlchemy
+from flaskext.sqlalchemy import SQLAlchemy, BaseQuery
 
 db = SQLAlchemy()
 
     def __str__(self):
         return self.name
 
+class MessageQuery(BaseQuery):
+
+    def with_channel(self):
+        """
+        Eagerloads channel
+        """
+        return self.options(db.eagerload('channel'))
+
+    def search(self, search):
+        if not search:
+            return self.filter(False)
+        search = unicode('%' + search + '%')
+        
+        q = db.or_(Message.nick.like(search),
+                   Message.message.like(search))
+
+        return self.filter(q)
+        
 class Message(db.Model):
     __tablename__ = "messages"
     
 
     channel = db.relation(Channel, backref="messages")
 
+    query_class = MessageQuery
+    
     __mapper_args__ = {'order_by' : date.asc()}
 
     def __str__(self):
 from irc_reader import create_app
 from irc_reader.models import db, Channel, Message
 
-class TestViews(TestCase):
+class _TestCase(TestCase):
     
     def setUp(self):
         db.create_all()
                               message="testing")
 
             db.session.add(message)
-
     
         db.session.commit()
 
+class TestModels(_TestCase):
+
+    def test_search_messages(self):
+
+        assert Message.query.search('tester').count() == 200
+
+class TestViews(_TestCase):
+
     def test_index(self):
         response = self.client.get("/")
         self.assert_200(response)
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.