diff options
-rw-r--r-- | TODO | 4 | ||||
-rw-r--r-- | fatcat/__init__.py | 26 | ||||
-rw-r--r-- | fatcat/api.py | 12 | ||||
-rw-r--r-- | fatcat/models.py | 11 | ||||
-rw-r--r-- | fatcat/routes.py | 21 | ||||
-rw-r--r-- | tests/test_backend.py | 16 |
6 files changed, 56 insertions, 34 deletions
@@ -1,7 +1,5 @@ -update public pointer tables: -- 'live' boolean (default false) -- redirect column (to self) +helpers... ORM? later: - public IDs are UUID (sqlite hack?) diff --git a/fatcat/__init__.py b/fatcat/__init__.py index 230fd608..cc5d2a65 100644 --- a/fatcat/__init__.py +++ b/fatcat/__init__.py @@ -7,4 +7,30 @@ app = Flask(__name__) app.config.from_object(Config) db = SQLAlchemy(app) +examples = { + "work": { + "id": "rzga5b9cd7efgh04iljk", + "rev": "12345", + "previous": None, + "state": None, + "redirect_id": None, + "edit_id": None, + "extra_json": None, + "title": "Structure and Interpretation", + "work_type": "journal-article", + "date": None, + "contributors": [ + {"name": "Alyssa P. Hacker"}, + ], + "primary": { + "title": "Structure and Interpretation", + "release_type": "online", + "date": "2000-01-01", + "doi": "10.491/599.sdo14", + }, + "releases": [ + ], + }, +} + from fatcat import routes, models, api, sql diff --git a/fatcat/api.py b/fatcat/api.py index d09eedd5..1c68822a 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -1,13 +1,17 @@ from flask import Flask, render_template, send_from_directory, request, \ url_for, abort, g, redirect, jsonify -from fatcat import app, db +from fatcat import app, db, examples from fatcat.models import * ### Views ################################################################### -@app.route('/work/<work_id>', methods=['GET']) -def work_get(): - work = WorkId.query.filter_by(id=work_id).first_or_404() +@app.route('/v0/work/<work_id>', methods=['GET']) +def work_get(work_id): + if work_id == "random": + work = examples['work'] + else: + work = WorkId.query.filter_by(id=work_id).first_or_404() return jsonify(work) + diff --git a/fatcat/models.py b/fatcat/models.py index 2f0bfd72..214ff8ac 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -2,6 +2,14 @@ import enum from fatcat import db +""" +states for identifiers: +- pre-live: points to a rev (during edit/accept period) +- live: points to a rev +- redirect: live, points to upstream rev, also points to redirect id + => if live and redirect non-null, all other fields copied from redirect target +- deleted: live, but doesn't point to a rev +""" # TODO: EntityMixin, EntityIdMixin @@ -25,6 +33,7 @@ class WorkId(db.Model): """ __tablename__ = 'work_id' id = db.Column(db.Integer, primary_key=True, nullable=False) + live = db.Column(db.Boolean, nullable=False, default=False) revision_id = db.Column(db.ForeignKey('work_revision.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('work_id.id'), nullable=True) @@ -64,7 +73,7 @@ class ReleaseRevision(db.Model): __tablename__ = 'release_revision' id = db.Column(db.Integer, primary_key=True, autoincrement=True) previous = db.Column(db.ForeignKey('release_revision.id'), nullable=True) - state = db.Column(db.String) # TODO: enum + state = db.Column(db.String) # TODO: enum redirect_id = db.Column(db.ForeignKey('release_id.id'), nullable=True) edit_id = db.Column(db.ForeignKey('edit.id')) extra_json = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) diff --git a/fatcat/routes.py b/fatcat/routes.py index 61228638..c59922b3 100644 --- a/fatcat/routes.py +++ b/fatcat/routes.py @@ -2,7 +2,7 @@ import os from flask import Flask, render_template, send_from_directory, request, \ url_for, abort, g, redirect, jsonify -from fatcat import app, db +from fatcat import app, db, examples ### Views ################################################################### @@ -13,25 +13,10 @@ def work_create(): @app.route('/work/random', methods=['GET']) def work_random(): - work = { - "title": "Structure and Interpretation", - "work_type": "book", - "date": None, - "contributors": [ - {"name": "Alyssa P. Hacker"}, - ], - "primary": { - "title": "Structure and Interpretation", - "release_type": "online", - "date": "2000-01-01", - "doi": "10.491/599.sdo14", - }, - "releases": [ - ] - } + work = examples['work'] return render_template('work_view.html', work=work, primary=work['primary']) -@app.route('/work/<work_id>/random', methods=['GET']) +@app.route('/work/random', methods=['GET']) def work_view(work_id): return render_template('work_view.html') diff --git a/tests/test_backend.py b/tests/test_backend.py index 23016e09..345c8a95 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -14,9 +14,9 @@ import tempfile def check_entity_fields(e): for key in ('id', 'rev', 'previous', 'state', 'redirect_id', 'edit_id', 'extra_json'): - assert_in(key, e) + assert key in e for key in ('id', 'rev'): - assert_is_not_none(e[key]) + assert e[key] is not None ## API Tests ################################################################ @@ -41,17 +41,17 @@ class FatcatTestCase(unittest.TestCase): # Missing Id (TODO) #rv = self.app.get('/v0/work/rzga5b9cd7efgh04iljk') - #assert rv.status is 404 + #assert rv.status == 404 - return pytest.skip("need to put first") + # Valid Id (TODO) + #rv = self.app.get('/v0/work/r3zga5b9cd7ef8gh084714iljk') + #assert rv.status_code == 200 - # Valid Id - rv = self.app.get('/v0/work/r3zga5b9cd7ef8gh084714iljk') - assert rv.status_code == 200 + rv = self.app.get('/v0/work/random') obj = json.loads(rv.data.decode('utf-8')) check_entity_fields(obj) assert obj['title'] - assert_equal(obj['work_type'], "journal-article") + assert obj['work_type'] == "journal-article" def test_something(self): |