Commits

Anonymous committed 620d964

by date funcs

  • Participants
  • Parent commits 1058d7c

Comments (0)

Files changed (5)

irc_reader/__init__.py

+import datetime
 import markdown
 
 from flask import Flask, render_template, request, current_app, url_for, \
     def channel(channel_id, page=1):
 
         channel = Channel.query.get_or_404(channel_id)
-        
+        dates = Message.query.filter_by(channel=channel).dates()
+
+        return render_template("channel.html", 
+                               dates=dates,
+                               channel=channel)
+
+    @app.route("/date/<int:channel_id>/<int:year>/<int:month>/<int:day>/")
+    @app.route("/date/<int:channel_id>/<int:year>/<int:month>/<int:day>/<int:page>")
+    def date(channel_id, year, month, day, page=1):
+
+        channel = Channel.query.get_or_404(channel_id)
+
+        date = datetime.date(year, month, day)
+
         page_obj = Message.query.filter_by(channel=channel).\
-            paginate(page, get_config('PER_PAGE'))
+            by_date(date).paginate(page, get_config('PER_PAGE'))
 
         page_url = lambda page: \
-            url_for('channel', channel_id=channel.id, page=page)
+            url_for('date', channel_id=channel.id,
+                            page=page,
+                            year=year,
+                            month=month,
+                            day=day)
 
-        return render_template("channel.html", 
+        return render_template("date.html",
+                               channel=channel,
+                               date=date,
                                page_obj=page_obj,
-                               page_url=page_url,
-                               channel=channel)
+                               page_url=page_url)
 
 
     @app.route("/nick/<nick>/")

irc_reader/models.py

         """
         Return unique list of dates for messages
         """
+        return sorted(set(d[0].date() for d in self.values(Message.date)))
+        
+    def by_date(self, date):
+        return self.filter(db.func.date(Message.date)==date)
 
-        return sorted(set(d[0] for d in self.values(Message.date)))
-        
 class Message(db.Model):
     __tablename__ = "messages"
     

irc_reader/templates/channel.html

 {% extends "layout.html" %}
-{% from "_pagination.html" import paginate %}
 {% block content %}
         <h2>{{ channel }}</h2>
         <ul class="messages">
-        {% for message in page_obj.items %}
-        {% if message.nick %}
-        <li>{{ message.date.strftime("%d-%m-%Y %H:%M") }} <strong><a href="{{ url_for('nick', nick=message.nick) }}">{{ message.nick }}</a></strong> {{ message.message|markdown }}</li>
-        {% else %}
-        <li>{{ message.date.strftime("%d-%m-%Y %H:%M") }}  <strong>{{ message.message|markdown }}</strong></li>
-        {% endif %}
+        {% for date in dates %}
+        <li><a href="{{ url_for('date', year=date.year, month=date.month, day=date.day, channel_id=channel.id) }}">{{ date.strftime('%d-%m-%Y') }}</a></li>
         {% endfor %}
     </ul>
-    {{ paginate(page_obj, page_url) }}
 {% endblock %}
 from settings import *
 
 SQLALCHEMY_DATABASE_URI = "sqlite:///test_irc_reader.db"
+SQLALCHEMY_ECHO = False
 TESTING = True
 
 
 import test_settings
 
-from datetime import datetime
+from datetime import datetime, date
 
 from flaskext.testing import TestCase
 
         
         now = datetime.now()
 
-        date = dates[0]
+        dt = dates[0]
 
-        assert date.year == now.year
-        assert date.month == now.month
-        assert date.day == now.day
+        assert dt.year == now.year
+        assert dt.month == now.month
+        assert dt.day == now.day
 
 
+    def test_by_date(self):
+
+        dt = date(2010, 1, 7)
+
+        messages = Message.query.by_date(dt)
+
+        assert messages.count() == 412
+
 class TestViews(_TestCase):
 
     def test_index(self):