diff options
-rw-r--r-- | fatcat/api.py | 22 | ||||
-rw-r--r-- | fatcat/models.py | 2 | ||||
-rw-r--r-- | fatcat/sql.py | 33 | ||||
-rw-r--r-- | tests/test_backend.py | 25 |
4 files changed, 70 insertions, 12 deletions
diff --git a/fatcat/api.py b/fatcat/api.py index 6e2c75a4..2fe50156 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -8,7 +8,10 @@ from fatcat.sql import * ### Helpers ################################################################# -def get_or_create_edit_group(): +def get_or_create_edit_group(param=None): + if param != None: + edit_group = EditGroup.query.filter(EditGroup.id==1).first_or_404() + return edit_group editor = Editor.query.filter(Editor.id==1).first() if editor.active_edit_group: return editor.active_edit_group @@ -37,7 +40,7 @@ def api_work_create(): TODO: use marshmallow? """ params = request.get_json() - edit_group = get_or_create_edit_group() + edit_group = get_or_create_edit_group(params.get('editgroup')) rev = WorkRev( title=params.get('title', None), work_type=params.get('work_type', None), @@ -65,7 +68,7 @@ def api_release_get(ident): @app.route('/v0/release', methods=['POST']) def api_release_create(): params = request.get_json() - edit_group = get_or_create_edit_group() + edit_group = get_or_create_edit_group(params.get('editgroup')) rev = ReleaseRev( title=params.get('title', None), release_type=params.get('release_type', None), @@ -92,7 +95,7 @@ def api_creator_get(ident): @app.route('/v0/creator', methods=['POST']) def api_creator_create(): params = request.get_json() - edit_group = get_or_create_edit_group() + edit_group = get_or_create_edit_group(params.get('editgroup')) rev = CreatorRev( name=params.get('name', None), orcid=params.get('orcid', None), @@ -115,7 +118,7 @@ def api_container_get(ident): @app.route('/v0/container', methods=['POST']) def api_container_create(): params = request.get_json() - edit_group = get_or_create_edit_group() + edit_group = get_or_create_edit_group(params.get('editgroup')) rev = ContainerRev( name=params.get('name', None), publisher=params.get('publisher', None), @@ -138,7 +141,7 @@ def api_file_get(ident): @app.route('/v0/file', methods=['POST']) def api_file_create(): params = request.get_json() - edit_group = get_or_create_edit_group() + edit_group = get_or_create_edit_group(params.get('editgroup')) rev = FileRev( sha1=params.get('sha1', None), size=params.get('size', None), @@ -172,3 +175,10 @@ def api_edit_group_create(): db.session.add(eg) db.session.commit() return edit_group_schema.jsonify(eg) + +@app.route('/v0/editgroup/<int:ident>/accept', methods=['POST']) +def api_edit_group_accept(ident): + entity = EditGroup.query.filter(EditGroup.id==ident).first_or_404() + accept_editgroup(entity) + return jsonify({'success': True}) + diff --git a/fatcat/models.py b/fatcat/models.py index de913147..d10ec1d9 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -248,7 +248,7 @@ class EditGroup(db.Model): 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") # XXX: for all entities + extra_json = db.relationship("ExtraJson") class Editor(db.Model): __tablename__ = 'editor' diff --git a/fatcat/sql.py b/fatcat/sql.py index 92074ec7..7dd02b71 100644 --- a/fatcat/sql.py +++ b/fatcat/sql.py @@ -1,5 +1,6 @@ import json +import time import random import hashlib from fatcat import db @@ -77,3 +78,35 @@ def add_crossref_via_model(meta): db.session.add_all(author_revs) db.session.add_all(author_ids) db.session.commit() + +def accept_editgroup(eg): + + # check if already accepted + # XXX: add a test for this + assert ChangelogEntry.query.filter(ChangelogEntry.edit_group_id==eg.id).count() == 0 + + # start transaction (TODO: explicitly?) + + # for each entity type: + for cls in (WorkEdit, ReleaseEdit, CreatorEdit, ContainerEdit, FileEdit): + edits = cls.query.filter(cls.edit_group_id==eg.id).all() + print(edits) + # for each entity edit->ident: + for edit in edits: + # update entity ident state (activate, redirect, delete) + edit.ident.is_live = True + edit.ident.rev_id = edit.rev_id + edit.ident.redirect_id = edit.redirect_id + db.session.add(edit.ident) + + # append log/changelog row + cle = ChangelogEntry( + edit_group_id=eg.id, + # TODO: is this UTC? + timestamp=int(time.time())) + db.session.add(cle) + + # update edit group state + db.session.add(eg) + + db.session.commit() diff --git a/tests/test_backend.py b/tests/test_backend.py index 4bf0224d..373cceb9 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -182,7 +182,7 @@ class APITestCase(FatcatTestCase): # not alive yet assert WorkIdent.query.filter(WorkIdent.is_live==True).count() == 0 - def test_api_complete_create(self): + def test_api_rich_create(self): # TODO: create user? @@ -240,6 +240,7 @@ class APITestCase(FatcatTestCase): data=json.dumps(dict( title="dummy work", work_type="book", + # XXX: #work=work_id, #container=container_id, #creators=[creator_id], @@ -271,12 +272,18 @@ class APITestCase(FatcatTestCase): ReleaseIdent, ReleaseRev, ReleaseEdit, FileIdent, FileRev, FileEdit): assert cls.query.count() == 1 - # Ident only: assert cls.query.filter(is_live=True).count() == 1 + + for cls in (WorkIdent, + ContainerIdent, + CreatorIdent, + ReleaseIdent, + FileIdent): + assert cls.query.filter(cls.is_live==True).count() == 0 rv = self.app.post('/v0/editgroup/{}/accept'.format(editgroup_id), headers={"content-type": "application/json"}) - # XXX: assert rv.status_code == 200 - # XXX: assert ChangelogEntry.query.count() == 1 + assert rv.status_code == 200 + assert ChangelogEntry.query.count() == 1 for cls in (WorkIdent, WorkRev, WorkEdit, ContainerIdent, ContainerRev, ContainerEdit, @@ -284,4 +291,12 @@ class APITestCase(FatcatTestCase): ReleaseIdent, ReleaseRev, ReleaseEdit, FileIdent, FileRev, FileEdit): assert cls.query.count() == 1 - # Ident only: assert cls.query.filter(is_live=True).count() == 1 + + for cls in (WorkIdent, + ContainerIdent, + CreatorIdent, + ReleaseIdent, + FileIdent): + assert cls.query.filter(cls.is_live==True).count() == 1 + + # XXX: re-fetch and test that relations work |