From 5bdd3f6e871599acb339aadc9b78cc12860f9759 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 23 Apr 2018 16:07:21 -0700 Subject: skeleton route/views --- fatcat/routes.py | 54 +++++++++++++-- fatcat/templates/container_view.html | 10 +++ fatcat/templates/creator_view.html | 10 +++ fatcat/templates/file_view.html | 10 +++ fatcat/templates/release_view.html | 31 +++++++++ tests/fixtures.py | 124 +++++++++++++++++++++++++++++++++-- tests/routes.py | 11 ++++ 7 files changed, 238 insertions(+), 12 deletions(-) create mode 100644 fatcat/templates/container_view.html create mode 100644 fatcat/templates/creator_view.html create mode 100644 fatcat/templates/file_view.html create mode 100644 fatcat/templates/release_view.html diff --git a/fatcat/routes.py b/fatcat/routes.py index d0d77ee7..c4cde22a 100644 --- a/fatcat/routes.py +++ b/fatcat/routes.py @@ -1,8 +1,9 @@ import os +import json from flask import Flask, render_template, send_from_directory, request, \ url_for, abort, g, redirect, jsonify, session -from fatcat import app, db +from fatcat import app, db, api ### Views ################################################################### @@ -13,12 +14,53 @@ def work_create(): @app.route('/work/random', methods=['GET']) def work_random(): - work = {} # XXX - return render_template('work_view.html', work=work, primary=work['primary']) + rv = app.get('/v0/work/random') + return redirect(rv.location) -@app.route('/work/random', methods=['GET']) -def work_view(work_id): - return render_template('work_view.html') +@app.route('/work/', methods=['GET']) +def work_view(ident): + rv = api.api_work_get(ident) + if rv.status_code != 200: + # TODO: better wrapping for all entities + return abort(rv.status_code) + entity = json.loads(rv.data.decode('utf-8')) + return render_template('work_view.html', work=entity) + +@app.route('/release/', methods=['GET']) +def release_view(ident): + rv = api.api_release_get(ident) + if rv.status_code != 200: + # TODO: better wrapping for all entities + return abort(rv.status_code) + entity = json.loads(rv.data.decode('utf-8')) + return render_template('release_view.html', release=entity) + +@app.route('/creator/', methods=['GET']) +def creator_view(ident): + rv = api.api_creator_get(ident) + if rv.status_code != 200: + # TODO: better wrapping for all entities + return abort(rv.status_code) + entity = json.loads(rv.data.decode('utf-8')) + return render_template('creator_view.html', creator=entity) + +@app.route('/container/', methods=['GET']) +def container_view(ident): + rv = api.api_container_get(ident) + if rv.status_code != 200: + # TODO: better wrapping for all entities + return abort(rv.status_code) + entity = json.loads(rv.data.decode('utf-8')) + return render_template('container_view.html', container=entity) + +@app.route('/file/', methods=['GET']) +def file_view(ident): + rv = api.api_file_get(ident) + if rv.status_code != 200: + # TODO: better wrapping for all entities + return abort(rv.status_code) + entity = json.loads(rv.data.decode('utf-8')) + return render_template('file_view.html', file=entity) ### Static Routes ########################################################### diff --git a/fatcat/templates/container_view.html b/fatcat/templates/container_view.html new file mode 100644 index 00000000..d842f7e1 --- /dev/null +++ b/fatcat/templates/container_view.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block body %} + +

Container: {{ container.id }}

+ +TODO: + +
{{ container }}
+ +{% endblock %} diff --git a/fatcat/templates/creator_view.html b/fatcat/templates/creator_view.html new file mode 100644 index 00000000..f7be9f2c --- /dev/null +++ b/fatcat/templates/creator_view.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block body %} + +

Creator: {{ creator.id }}

+ +TODO: + +
{{ creator }}
+ +{% endblock %} diff --git a/fatcat/templates/file_view.html b/fatcat/templates/file_view.html new file mode 100644 index 00000000..ff55e21c --- /dev/null +++ b/fatcat/templates/file_view.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% block body %} + +

File: {{ file.id }}

+ +TODO: + +
{{ file }}
+ +{% endblock %} diff --git a/fatcat/templates/release_view.html b/fatcat/templates/release_view.html new file mode 100644 index 00000000..ee68161c --- /dev/null +++ b/fatcat/templates/release_view.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} +{% block body %} + +

{{ release.title }}

+ +

Release type: {{ release.type }} +

History +

Contributors: +{% for c in release.contributors %} {{ c.name }}; {% endfor %} + +

Title: {{ release.title }} +

Date: {{ release.date }} + +{% if release.container %} +

Container: {{ release.container.title }} +{% endif %} + +{% if release.doi %} +

DOI: {{ release.doi }} +{% endif %} + +{% if releases %} +

    +{% for r in releases %} +
      {{ r.title }} ({{ y.date }} - {{ y.release_type }}) +{% endfor %} +
    +{% else %} +{% endif %} + +{% endblock %} diff --git a/tests/fixtures.py b/tests/fixtures.py index 9176b8c5..04b4314a 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -18,15 +18,127 @@ def app(): @pytest.fixture def rich_app(app): - assert app.post('/v0/work', + + rv = app.post('/v0/editgroup', + data=json.dumps(dict( + extra=dict(q=1, u="zing"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + editgroup_id = obj['id'] + + rv = app.post('/v0/container', + data=json.dumps(dict( + name="schmournal", + publisher="society of authors", + issn="2222-3333", + editgroup=editgroup_id, + extra=dict(a=2, i="zing"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + container_id = obj['id'] + + rv = app.post('/v0/creator', + data=json.dumps(dict( + name="anon y. mouse", + orcid="0000-0002-1825-0097", + editgroup=editgroup_id, + extra=dict(w=1, q="zing"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + creator_id = obj['id'] + + rv = app.post('/v0/work', + data=json.dumps(dict( + title="dummy work", + work_type="book", + editgroup=editgroup_id, + extra=dict(a=3, b="zing"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + work_id = obj['id'] + + # this stub work will be referenced + rv = app.post('/v0/release', + data=json.dumps(dict( + title="derivative work", + work_type="journal-article", + work=work_id, + creators=[creator_id], + doi="10.1234/58", + editgroup=editgroup_id, + refs=[ + dict(stub="some other journal article"), + ], + extra=dict(f=7, b="zing"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + stub_release_id = obj['id'] + + rv = app.post('/v0/release', + data=json.dumps(dict( + title="dummy work", + work_type="book", + work=work_id, + container=container_id, + creators=[creator_id], + doi="10.1234/5678", + editgroup=editgroup_id, + refs=[ + dict(stub="some book", target=stub_release_id), + ], + extra=dict(f=7, b="loopy"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + release_id = obj['id'] + + rv = app.post('/v0/file', data=json.dumps(dict( - title="dummy", - work_type="thing", - extra=dict(a=1, b="zing"))), - headers={"content-type": "application/json"} - ).status_code == 200 + sha1="deadbeefdeadbeef", + size=1234, + releases=[release_id], + editgroup=editgroup_id, + extra=dict(f=4, b="zing"))), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + file_id = obj['id'] + + rv = app.post('/v0/editgroup/{}/accept'.format(editgroup_id), + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + return app +def test_rich_app_fixture(rich_app): + app = rich_app + + assert ChangelogEntry.query.count() == 1 + + for cls in (WorkIdent, WorkRev, WorkEdit, + ContainerIdent, ContainerRev, ContainerEdit, + CreatorIdent, CreatorRev, CreatorEdit, + FileIdent, FileRev, FileEdit): + assert cls.query.count() == 1 + for cls in (ReleaseIdent, ReleaseRev, ReleaseEdit): + assert cls.query.count() == 2 + + for cls in (WorkIdent, + ContainerIdent, + CreatorIdent, + FileIdent): + assert cls.query.filter(cls.is_live==True).count() == 1 + assert ReleaseIdent.query.filter(ReleaseIdent.is_live==True).count() == 2 + + # test that editor's active edit group is now invalid + editor = Editor.query.first() + assert editor.active_edit_group == None + ## Helpers ################################################################## diff --git a/tests/routes.py b/tests/routes.py index 47b99e30..194dc865 100644 --- a/tests/routes.py +++ b/tests/routes.py @@ -18,3 +18,14 @@ def test_static_routes(rich_app): assert app.get("/static/bogus/route").status_code == 404 +def test_all_views(rich_app): + app = rich_app + + for route in ('work', 'release', 'creator', 'container', 'file'): + print(route) + rv = app.get('/{}/1'.format(route)) + assert rv.status_code == 200 + + rv = app.get('/v0/work/random') + rv = app.get(rv.location) + assert rv.status_code == 200 -- cgit v1.2.3