Skip to content

Commit

Permalink
add paginated view of stream plugins, though it's really very crappy;…
Browse files Browse the repository at this point in the history
… needed it to test out bitbucket stuff
  • Loading branch information
jmoiron committed Apr 26, 2011
1 parent 9b4844b commit 0312685
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions stream/models.py
Expand Up @@ -6,6 +6,8 @@
from flask import render_template
from micromongo import *

__all__ = ['Entry', 'Plugin']

class Entry(Model):
collection = 'jmoiron.stream_entry'
spec = {
Expand All @@ -25,4 +27,13 @@ def _render(self):
return render_template(template, entry=self)


class Plugin(Model):
collection = 'jmoiron.stream_plugin'
spec = {
'tag' : Field(required=True, type=basestring),
'name' : Field(required=True, type=basestring),
'id' : Field(type=int), # legacy
'interval' : Field(type=int),
'arguments': Field(required=True)
}

3 changes: 3 additions & 0 deletions stream/templates/index.html
Expand Up @@ -3,7 +3,10 @@
{% block body %}
<h1>Stream</h1>
<p>Welcome to the stream! Check out <a href="{{ url_for('blog.index') }}">my blog</a></p>
{% if has_prev %}<a href="{{ url_for('stream.show_page', num=current-1) }}">&lt;- {{ current-1}}</a>{% endif %}
{% if has_next %}<a href="{{ url_for('stream.show_page', num=current+1) }}">{{ current+1}} -&gt;</a>{% endif %}
{% for entry in entries %}
{{ entry.rendered|safe }}
{% endfor %}

{% endblock %}
24 changes: 22 additions & 2 deletions stream/views.py
Expand Up @@ -3,12 +3,32 @@

"""stream application views"""

from flask import Module, g, request, render_template
from flask import *
from models import Entry

stream = Module(__name__, 'stream')

per_page = 25

@stream.route("/")
def index():
entries = Entry.find().order_by('-timestamp')[:25]
entries = Entry.find().order_by('-timestamp')[:per_page]
return render_template('stream/index.html', **locals())

@stream.route("/page/<int:num>")
def show_page(num):
total = Entry.find().count()
begin = (num-1) * per_page
end = num * per_page
if begin > total:
abort(404)
entries = Entry.find().order_by('-timestamp')[begin:end]
has_next = end < total
has_prev = num > 1
return render_template('stream/index.html', **{
'entries': entries,
'has_next': has_next,
'has_prev': has_prev,
'current': num
})

0 comments on commit 0312685

Please sign in to comment.