Commits

danjac  committed 8f2942c

pagination fixes

  • Participants
  • Parent commits 3bb24ca

Comments (0)

Files changed (3)

File irc_reader/__init__.py

-from flask import Flask, render_template, request, current_app
+from flask import Flask, render_template, request, current_app, url_for
 
 from irc_reader.models import Channel, Message, db
 
 DEBUG = True
 SECRET_KEY = 'secret'
-PAGE_SIZE = 100
+PER_PAGE = 100
 
 def get_config(name, default=None):
     return current_app.config.get(name, default)
     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('PAGE_SIZE'))
+            paginate(page, get_config('PER_PAGE'))
+
+        page_url = lambda page: \
+            url_for('channel', channel_id=channel.id, page=page)
 
         return render_template("channel.html", 
                                page_obj=page_obj,
+                               page_url=page_url,
                                channel=channel)
 
 
     def nick(nick, page=1):
         page_obj = Message.query.filter_by(nick=nick).\
             options(db.eagerload('channel')).\
-                paginate(page, get_config('PAGE_SIZE'))
+                paginate(page, get_config('PER_PAGE'))
+
+        page_url = lambda page: \
+            url_for('nick', nick=nick, page=page)
 
         return render_template("nick.html",
                                page_obj=page_obj,
+                               page_url=page_url,
                                nick=nick)
 
 
     @app.route("/search/<int:channel_id>/")
-    def search(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))
 
-            messages = Message.query.filter(q).options(db.eagerload('channel'))
+            q = q & db.and_(Message.channel_id==channel.id)
+
+            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:
-            messages = []
+            page_obj = page_url = None
 
         return render_template('search.html',
                                search=search,
-                               messages=messages)
+                               page_obj=page_obj,
+                               page_url=page_url,
+                               channel=channel)
 
     return app
 

File irc_reader/templates/channel.html

-
+{% from "_pagination.html" import paginate %}
 <html>
     <body>
         <ul>
         {% for message in page_obj.items %}
         <li>{{ message.date.strftime("%d-%m-Y %H:%M") }} {% if message.nick %} <strong><a href="{{ url_for('nick', nick=message.nick) }}">{{ message.nick }}</a></strong> {% endif %}{{ message.message }}</li>
         {% endfor %}
-        </ul>
+    </ul>
+    {{ paginate(page_obj, page_url) }}
     </body>
 </html>

File test_settings.py

 from settings import *
 
-SQLALCHEMY_DATABASE_URI = "sqlite://"
+SQLALCHEMY_DATABASE_URI = "sqlite:///test_irc_reader.db"
 TESTING = True