Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Add basic item view
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Graves committed Jun 17, 2015
1 parent 6d0654d commit 4cc8d29
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kepler/app.py
Expand Up @@ -3,6 +3,7 @@
from flask import Flask
from .extensions import db
from kepler.job import job_blueprint
from kepler.item import item_blueprint
from .settings import DefaultConfig
from .exceptions import UnsupportedFormat

Expand All @@ -26,6 +27,7 @@ def register_extensions(app):

def register_blueprints(app):
app.register_blueprint(job_blueprint)
app.register_blueprint(item_blueprint)


def register_errorhandlers(app):
Expand Down
10 changes: 10 additions & 0 deletions kepler/item/__init__.py
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from flask import Blueprint

from kepler.item.views import ItemView


item_blueprint = Blueprint('item', __name__)
ItemView.register(item_blueprint, 'items', '/items/')
31 changes: 31 additions & 0 deletions kepler/item/views.py
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from flask import request, render_template
from flask.views import View

from kepler.models import Item, Job


class ItemView(View):
def dispatch_request(self, *args, **kwargs):
if request.method == 'GET':
if request.endpoint.endswith('.index'):
return self.list()
return self.show(*args, **kwargs)

def list(self):
items = Item.query.all()
return render_template('item/index.html', items=items)

def show(self, id):
item = Item.query.get_or_404(id)
jobs = item.jobs.order_by(Job.time.desc())
return render_template('item/show.html', item=item, jobs=jobs)

@classmethod
def register(cls, app, endpoint, url):
view_func = cls.as_view(endpoint)
app.add_url_rule(url, 'index', methods=['GET'], view_func=view_func)
app.add_url_rule('%s<path:id>' % url, 'resource', methods=['GET'],
view_func=view_func)
9 changes: 9 additions & 0 deletions kepler/templates/item/index.html
@@ -0,0 +1,9 @@
{% extends 'base.html' %}

{% block content %}
<ol>
{% for item in items %}
<li><a href="{{ url_for('.resource', id=item.id) }}">{{ item.uri }}</a></li>
{% endfor %}
</ol>
{% endblock %}
15 changes: 15 additions & 0 deletions kepler/templates/item/show.html
@@ -0,0 +1,15 @@
{% extends 'base.html' %}

{% block content %}
<h1>{{ item.uri }}</h1>
<ol>
{% for job in jobs %}
<li>
<a href="{{ url_for('job.resource', id=job.id) }}">
{{ job.time.strftime('%Y-%m-%dT%H:%M:%S%z') }}
</a>
{{ job.status }}
</li>
{% endfor %}
</ol>
{% endblock %}
16 changes: 16 additions & 0 deletions tests/test_views.py
Expand Up @@ -62,3 +62,19 @@ def testJobListReturnsOnlyMostRecentJobs(self, testapp, db):
r = testapp.get('/jobs/')
assert 'COMPLETED' in r.text
assert 'PENDING' not in r.text


class TestItem(object):
def testItemListReturnsItems(self, testapp, db):
db.session.add_all([Item(uri=u'Foo'), Item(uri=u'Bar')])
db.session.commit()
r = testapp.get('/items/')
assert 'Foo' in r.text
assert 'Bar' in r.text

def testItemRetrievalShowsItem(self, testapp, db):
item = Item(uri=u'Foobar')
db.session.add(item)
db.session.commit()
r = testapp.get('/items/%s' % item.id)
assert 'Foobar' in r.text

0 comments on commit 4cc8d29

Please sign in to comment.