diff options
-rw-r--r-- | fatcat/api.py | 21 | ||||
-rw-r--r-- | fatcat/models.py | 19 | ||||
-rw-r--r-- | fatcat/sql.py | 1 | ||||
-rw-r--r-- | tests/test_backend.py | 21 |
4 files changed, 44 insertions, 18 deletions
diff --git a/fatcat/api.py b/fatcat/api.py index 2fe50156..e5b473bb 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -69,14 +69,24 @@ def api_release_get(ident): def api_release_create(): params = request.get_json() edit_group = get_or_create_edit_group(params.get('editgroup')) + creators = params.get('creators', []) + creators = [CreatorIdent.query.filter(CreatorIdent.id==c).first_or_404() for c in creators] + work = params.get('work') + if work: + work = WorkIdent.query.filter(WorkIdent.id==work).first_or_404() + container = params.get('container') + if container: + container = ContainerIdent.query.filter(ContainerIdent.id==container).first_or_404() rev = ReleaseRev( title=params.get('title', None), release_type=params.get('release_type', None), - creators=params.get('creators', []), - #work=params.get('work', None), - container=params.get('container', None), + work=work, + container=container, doi=params.get('doi', None), ) + contribs = [ReleaseContrib(release=rev, creator=c) for c in creators] + rev.creators = contribs + db.session.add_all(contribs) ident = ReleaseIdent(is_live=False, rev=rev) edit = ReleaseEdit(edit_group=edit_group, ident=ident, rev=rev) if params.get('extra', None): @@ -142,11 +152,16 @@ def api_file_get(ident): def api_file_create(): params = request.get_json() edit_group = get_or_create_edit_group(params.get('editgroup')) + releases = params.get('releases', []) + releases = [ReleaseIdent.query.filter(ReleaseIdent.id==r).first_or_404() for r in releases] rev = FileRev( sha1=params.get('sha1', None), size=params.get('size', None), url=params.get('url', None), ) + file_releases = [FileRelease(file=rev, release=r) for r in releases] + rev.releases = file_releases + db.session.add_all(file_releases) ident = FileIdent(is_live=False, rev=rev) edit = FileEdit(edit_group=edit_group, ident=ident, rev=rev) if params.get('extra', None): diff --git a/fatcat/models.py b/fatcat/models.py index d10ec1d9..e785e125 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -101,7 +101,7 @@ class ReleaseRev(db.Model): extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) extra_json = db.relationship("ExtraJson") - work_ident_id = db.ForeignKey('work_ident.id') + work_ident_id = db.Column(db.ForeignKey('work_ident.id', use_alter=True), nullable=True) # XXX: nullable=False container_ident_id = db.Column(db.ForeignKey('container_ident.id'), nullable=True) title = db.Column(db.String, nullable=False) license = db.Column(db.String, nullable=True) # TODO: oa status foreign key @@ -112,7 +112,7 @@ class ReleaseRev(db.Model): pages = db.Column(db.String, nullable=True) issue = db.Column(db.String, nullable=True) - #work = db.relationship("WorkIdent", lazy='subquery') + work = db.relationship("WorkIdent", lazy='subquery', foreign_keys="ReleaseRev.work_ident_id") container = db.relationship("ContainerIdent", lazy='subquery') creators = db.relationship('ReleaseContrib', lazy='subquery') refs = db.relationship('ReleaseRef', lazy='subquery') @@ -314,20 +314,20 @@ class EntitySchema(ma.ModelSchema): class ReleaseContribSchema(ma.ModelSchema): class Meta: model = ReleaseContrib - #creator = db.relationship("CreatorIdent") - #release = db.relationship("ReleaseRev") + creator = db.relationship("CreatorIdent") + release = db.relationship("ReleaseRev") class ReleaseRefSchema(ma.ModelSchema): class Meta: model = ReleaseRef - #release = db.relationship("ReleaseRev") - #target = db.relationship("ReleaseIdent") + release = db.relationship("ReleaseRev") + target = db.relationship("ReleaseIdent") class FileReleaseSchema(ma.ModelSchema): class Meta: model = FileRelease - #release = db.relationship("ReleaseIdent") - #file = db.relationship("FileRev") + release = db.relationship("ReleaseIdent") + file = db.relationship("FileRev") class WorkRevSchema(ma.ModelSchema): class Meta: @@ -350,6 +350,7 @@ work_edit_schema = WorkEditSchema() class ReleaseRevSchema(ma.ModelSchema): class Meta: model = ReleaseRev + work = ma.Nested('WorkSchema') container = ma.Nested('ContainerSchema') creators = ma.Nested(ReleaseContribSchema, many=True) refs = ma.Nested(ReleaseRefSchema, many=True) @@ -409,6 +410,8 @@ class FileRevSchema(ma.ModelSchema): class Meta: model = FileRev + releases = ma.Nested(FileReleaseSchema, many=True) + class FileSchema(EntitySchema): class Meta: model = FileIdent diff --git a/fatcat/sql.py b/fatcat/sql.py index 7dd02b71..83c9e182 100644 --- a/fatcat/sql.py +++ b/fatcat/sql.py @@ -90,7 +90,6 @@ def accept_editgroup(eg): # 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) diff --git a/tests/test_backend.py b/tests/test_backend.py index 373cceb9..25421560 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -240,10 +240,9 @@ class APITestCase(FatcatTestCase): data=json.dumps(dict( title="dummy work", work_type="book", - # XXX: - #work=work_id, - #container=container_id, - #creators=[creator_id], + work=work_id, + container=container_id, + creators=[creator_id], doi="10.1234/5678", editgroup=editgroup_id, refs=[ @@ -259,12 +258,13 @@ class APITestCase(FatcatTestCase): data=json.dumps(dict( sha1="deadbeefdeadbeef", size=1234, - release=release_id, + 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'] for cls in (WorkIdent, WorkRev, WorkEdit, ContainerIdent, ContainerRev, ContainerEdit, @@ -299,4 +299,13 @@ class APITestCase(FatcatTestCase): FileIdent): assert cls.query.filter(cls.is_live==True).count() == 1 - # XXX: re-fetch and test that relations work + # Test that foreign key relations worked + release_rv = json.loads(self.app.get('/v0/release/{}'.format(release_id)).data.decode('utf-8')) + print(release_rv) + assert(release_rv['creators'][0]['creator'] == creator_id) + assert(release_rv['container']['id'] == container_id) + assert(release_rv['work']['id'] == work_id) + + file_rv = json.loads(self.app.get('/v0/file/{}'.format(file_id)).data.decode('utf-8')) + print(file_rv) + assert(file_rv['releases'][0]['release'] == release_id) |