Commits

danjac  committed 3bb24ca

get_config added

  • Participants
  • Parent commits a61ac87

Comments (0)

Files changed (4)

File irc_reader/__init__.py

-from flask import Flask, render_template, request
+from flask import Flask, render_template, request, current_app
 
 from irc_reader.models import Channel, Message, db
 
 DEBUG = True
 SECRET_KEY = 'secret'
+PAGE_SIZE = 100
+
+def get_config(name, default=None):
+    return current_app.config.get(name, default)
 
 def create_app(config):
 
 
     @app.route("/")
     def index():
-        channels = Channel.query.all()
+        channels = Channel.query.order_by(Channel.name).all()
         return render_template("index.html", channels=channels)
 
 
     @app.route("/channel/<int:channel_id>/<int:page>/")
     def channel(channel_id, page=1):
         channel = Channel.query.get_or_404(channel_id)
-        messages = Message.query.filter_by(channel=channel).paginate(page, 100)
+        page_obj = Message.query.filter_by(channel=channel).\
+            paginate(page, get_config('PAGE_SIZE'))
 
         return render_template("channel.html", 
-                               messages=messages,
+                               page_obj=page_obj,
                                channel=channel)
 
 
     @app.route("/nick/<nick>/")
     @app.route("/nick/<nick>/<int:page>/")
     def nick(nick, page=1):
-        messages = Message.query.\
-            filter_by(nick=nick).\
-                options(db.eagerload('channel')).paginate(page, 100)
+        page_obj = Message.query.filter_by(nick=nick).\
+            options(db.eagerload('channel')).\
+                paginate(page, get_config('PAGE_SIZE'))
 
         return render_template("nick.html",
-                               messages=messages,
+                               page_obj=page_obj,
                                nick=nick)
 
 
         search = request.args.get('search', '')
         if search:
             search = '%' + search + '%'
-            options = db.or_(Message.nick.like(search),
-                             Message.message.like(search))
+            q = db.or_(Message.nick.like(search),
+                       Message.message.like(search))
 
-            messages = Message.query.filter(options)
+            messages = Message.query.filter(q).options(db.eagerload('channel'))
         else:
             messages = []
 

File irc_reader/templates/channel.html

 <html>
     <body>
         <ul>
-        {% for message in messages.items %}
+        {% 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>

File irc_reader/templates/nick.html

 <html>
     <body>
         <ul>
-        {% for message in messages.items %}
+        {% for message in page_obj.items %}
         <li>{{ message.date.strftime("%d-%m-Y %H:%M") }} {{ message.message }}</li>
         {% endfor %}
         </ul>

File parse_irc_logs.py

 import glob
 import os
-import stat
 
 import settings