From 95317172ddee2ea6f18512262f481dd7f6fede8b Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 23 Apr 2018 20:36:57 -0700 Subject: basic api+route views for editor/changelog --- fatcat/api.py | 21 +++++++++++++++++++-- fatcat/api_client.py | 21 ++++++++++++--------- fatcat/models.py | 16 ++++++++++++---- fatcat/routes.py | 21 +++++++++++++++++++++ fatcat/templates/editgroup_view.html | 8 ++------ fatcat/templates/editor_changelog.html | 17 +++++++++++++++++ fatcat/templates/editor_view.html | 9 +++++++++ tests/api.py | 21 +++++++++++++++++++++ tests/routes.py | 6 ++++++ 9 files changed, 119 insertions(+), 21 deletions(-) create mode 100644 fatcat/templates/editor_changelog.html create mode 100644 fatcat/templates/editor_view.html diff --git a/fatcat/api.py b/fatcat/api.py index 905a4317..60d0ba0c 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -10,7 +10,7 @@ from fatcat.sql import * def get_or_create_edit_group(param=None): if param != None: - edit_group = EditGroup.query.filter(EditGroup.id==1).first_or_404() + edit_group = EditGroup.query.filter(EditGroup.id==int(param)).first_or_404() return edit_group editor = Editor.query.filter(Editor.id==1).first() if editor.active_edit_group: @@ -219,7 +219,9 @@ def api_file_create(): @app.route('/v0/editgroup/', methods=['GET']) def api_edit_group_get(ident): - entity = EditGroup.query.filter(EditGroup.id==ident).first_or_404() + entity = EditGroup.query\ + .join(EditGroup.editor)\ + .filter(EditGroup.id==ident).first_or_404() # TODO: fill in all the related edit types... return edit_group_schema.jsonify(entity) @@ -242,3 +244,18 @@ def api_edit_group_accept(ident): entity = EditGroup.query.filter(EditGroup.id==ident).first_or_404() accept_editgroup(entity) return jsonify({'success': True}) + + +@app.route('/v0/editor/', methods=['GET']) +def api_editor_get(username): + entity = Editor.query.filter(Editor.username==username).first_or_404() + return editor_schema.jsonify(entity) + +@app.route('/v0/editor//changelog', methods=['GET']) +def api_editor_changelog(username): + entries = ChangelogEntry.query\ + .join(ChangelogEntry.edit_group)\ + .join(EditGroup.editor)\ + .filter(Editor.username==username)\ + .all() + return changelogentry_schema.jsonify(entries, many=True) diff --git a/fatcat/api_client.py b/fatcat/api_client.py index 12c70407..181c3ca7 100644 --- a/fatcat/api_client.py +++ b/fatcat/api_client.py @@ -9,11 +9,9 @@ class FatCatApiClient: self.host_url = host_url self.session = requests.Session() - def get(self, path): return self.session.get(self.host_url + path) - def post(self, path, data=None, headers=None): hdrs = {"content-type": "application/json"} if headers: @@ -22,22 +20,27 @@ class FatCatApiClient: # data = json.dumps(data, indent=None).encode('utf-8') return self.session.post(self.host_url + path, json=data, headers=hdrs) - def import_crossref_file(self, json_file): + eg = self.new_edit_group() with open(json_file, 'r') as file: for line in file: obj = json.loads(line) - self.import_crossref_dict(obj) - + self.import_crossref_dict(obj, editgroup=eg) + self.accept_editgroup(eg) def new_edit_group(self): - rv = self.post('/v0/editgroup') + rv = self.post('/v0/editgroup', data=dict( + editor=1)) assert rv.status_code == 200 editgroup_id = rv.json()['id'] return editgroup_id + def accept_editgroup(self, eg): + rv = self.post('/v0/editgroup/{}/accept'.format(eg)) + assert rv.status_code == 200 + return rv - def import_crossref_dict(self, meta): + def import_crossref_dict(self, meta, editgroup=None): # creators creators = [] @@ -71,7 +74,7 @@ class FatCatApiClient: # work and release title = meta['title'][0] rv = self.post('/v0/work', - data=dict(title=title)) #work_type="book" + data=dict(title=title, editgroup=editgroup)) #work_type="book" assert rv.status_code == 200 work_id = rv.json()['id'] @@ -88,6 +91,7 @@ class FatCatApiClient: issue=meta.get('issue', None), volume=meta.get('volume', None), pages=meta.get('page', None), + editgroup=editgroup, extra=dict(crossref={ 'links': meta.get('link', []), 'subject': meta['subject'], @@ -96,7 +100,6 @@ class FatCatApiClient: assert rv.status_code == 200 release_id = rv.json()['id'] - def health(self): rv = self.get("/health") assert rv.status_code == 200 diff --git a/fatcat/models.py b/fatcat/models.py index 957c929f..01aef79f 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -246,15 +246,15 @@ class EditGroup(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) editor_id = db.Column(db.ForeignKey('editor.id'), nullable=False) description = db.Column(db.String) - editor = db.relationship('Editor', foreign_keys='EditGroup.editor_id') extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) extra_json = db.relationship("ExtraJson") - # TODO: changelog entry relationship + + editor = db.relationship("Editor", foreign_keys="EditGroup.editor_id") class Editor(db.Model): __tablename__ = 'editor' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - username = db.Column(db.String) + username = db.Column(db.String, nullable=False, unique=True) is_admin = db.Column(db.Boolean, nullable=False, default=False) active_edit_group_id = db.Column(db.ForeignKey('edit_group.id', use_alter=True)) active_edit_group = db.relationship('EditGroup', foreign_keys='Editor.active_edit_group_id') @@ -264,6 +264,7 @@ class ChangelogEntry(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) edit_group_id = db.Column(db.ForeignKey('edit_group.id')) timestamp = db.Column(db.Integer) + edit_group = db.relationship("EditGroup") ### Other ################################################################### @@ -441,6 +442,13 @@ class EditorSchema(ma.ModelSchema): class EditGroupSchema(ma.ModelSchema): class Meta: model = EditGroup + editor = ma.Nested(EditorSchema) -editor_schema = EditGroupSchema() +editor_schema = EditorSchema() edit_group_schema = EditGroupSchema() + +class ChangelogEntrySchema(ma.ModelSchema): + class Meta: + model = ChangelogEntry + +changelogentry_schema = ChangelogEntrySchema() diff --git a/fatcat/routes.py b/fatcat/routes.py index 3bfcf58a..75411da9 100644 --- a/fatcat/routes.py +++ b/fatcat/routes.py @@ -83,6 +83,27 @@ def editgroup_current(ident): eg = api.get_or_create_edit_group() return redirect('/editgroup/{}'.format(eg.id)) +@app.route('/editor/', methods=['GET']) +def editor_view(username): + rv = api.api_editor_get(username) + 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('editor_view.html', editor=entity) + +@app.route('/editor//changelog', methods=['GET']) +def editor_changelog(username): + rv = api.api_editor_get(username) + if rv.status_code != 200: + # TODO: better wrapping for all entities + return abort(rv.status_code) + editor = json.loads(rv.data.decode('utf-8')) + rv = api.api_editor_changelog(username) + changelog_entries = json.loads(rv.data.decode('utf-8')) + return render_template('editor_changelog.html', editor=editor, + changelog_entries=changelog_entries) + ### Static Routes ########################################################### diff --git a/fatcat/templates/editgroup_view.html b/fatcat/templates/editgroup_view.html index 13eb2d7f..144c9502 100644 --- a/fatcat/templates/editgroup_view.html +++ b/fatcat/templates/editgroup_view.html @@ -1,13 +1,9 @@ {% extends "base.html" %} {% block body %} -

Edit Group: {{ editgroup.id}}

+

Edit Group: #{{ editgroup.id}}

- description = db.Column(db.String) - editor = db.relationship('Editor', foreign_keys='EditGroup.editor_id') - - -

Editor: {{ editgroup.editor.username }} +

Editor: {{ editgroup.editor.username }}

Description: {{ editgroup.description }}

TODO: list edits by type...

diff --git a/fatcat/templates/editor_changelog.html b/fatcat/templates/editor_changelog.html new file mode 100644 index 00000000..5d87d82e --- /dev/null +++ b/fatcat/templates/editor_changelog.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% block body %} + +

Editor Changelog: {{ editor.username }}

+ +

Editor: {{ editor.username }} + +

Changes accepted (aka, merged editgroups): +

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

Editor: {{ editor.username }}

+ +

Is Admin? {{ editor.is_admin }} +

Changelog + +{% endblock %} diff --git a/tests/api.py b/tests/api.py index 8b495c61..5fff779e 100644 --- a/tests/api.py +++ b/tests/api.py @@ -287,3 +287,24 @@ def test_api_container_lookup(rich_app): data=json.dumps(dict(issn="not_even_valid_issn")), headers={"content-type": "application/json"}) assert rv.status_code == 400 + +def test_api_editor_get(rich_app): + app = rich_app + + rv = app.get('/v0/editor/admin', + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + print(obj) + assert obj['username'] == "admin" + assert obj['id'] == 1 + +def test_api_editor_changelog(rich_app): + app = rich_app + + rv = app.get('/v0/editor/admin/changelog', + headers={"content-type": "application/json"}) + assert rv.status_code == 200 + obj = json.loads(rv.data.decode('utf-8')) + print(obj) + assert len(obj) == 1 diff --git a/tests/routes.py b/tests/routes.py index 49afc5ce..e389a55f 100644 --- a/tests/routes.py +++ b/tests/routes.py @@ -32,3 +32,9 @@ def test_all_views(rich_app): rv = app.get('/editgroup/1') assert rv.status_code == 200 + + rv = app.get('/editor/admin') + assert rv.status_code == 200 + + rv = app.get('/editor/admin/changelog') + assert rv.status_code == 200 -- cgit v1.2.3