diff options
-rw-r--r-- | fatcat/dummy.py | 18 | ||||
-rw-r--r-- | fatcat/models.py | 106 | ||||
-rw-r--r-- | fatcat/sql.py | 30 |
3 files changed, 101 insertions, 53 deletions
diff --git a/fatcat/dummy.py b/fatcat/dummy.py index b547cf16..7e09f9c9 100644 --- a/fatcat/dummy.py +++ b/fatcat/dummy.py @@ -13,18 +13,18 @@ def insert_example_works(): name="Noam D. Elkies", sortname="Elkies, N", orcid=None) - n_elkies_id = CreatorIdent(revision=n_elkies) + n_elkies_id = CreatorIdent(rev=n_elkies) pi_work = WorkRev( title="Why is π^2 so close to 10?", work_type="journal-article") - pi_work_id = WorkIdent(revision=pi_work) + pi_work_id = WorkIdent(rev=pi_work) pi_release = ReleaseRev( title=pi_work.title, work_ident_id=pi_work.id, release_type="journal-article") pi_contrib = ReleaseContrib(creator=n_elkies_id) pi_release.creators.append(pi_contrib) - pi_release_id = ReleaseIdent(revision=pi_release) + pi_release_id = ReleaseIdent(rev=pi_release) pi_work.primary_release = pi_release # TODO: @@ -60,7 +60,7 @@ def insert_random_works(count=100): sortname="{}, {}".format(last, first[0]), orcid=None) author_revs.append(ar) - author_ids.append(CreatorIdent(revision=ar)) + author_ids.append(CreatorIdent(rev=ar)) container_revs = [] container_ids = [] @@ -72,7 +72,7 @@ def insert_random_works(count=100): sortname="Fake Journal of Stuff", issn="1234-5678") container_revs.append(cr) - container_ids.append(ContainerIdent(revision=cr)) + container_ids.append(ContainerIdent(rev=cr)) title_start = ("All about ", "When I grow up I want to be", "The final word on", "Infinity: ", "The end of") @@ -86,14 +86,14 @@ def insert_random_works(count=100): for _ in range(count): title = "{} {}".format(random.choice(title_start), random.choice(title_ends)) work = WorkRev(title=title) - work_id = WorkIdent(revision=work) + work_id = WorkIdent(rev=work) authors = set(random.sample(author_ids, 5)) release = ReleaseRev( title=work.title, creators=[ReleaseContrib(creator=a) for a in list(authors)], #work=work, container=random.choice(container_ids)) - release_id = ReleaseIdent(revision=release) + release_id = ReleaseIdent(rev=release) work.primary_release = release authors.add(random.choice(author_ids)) release2 = ReleaseRev( @@ -101,7 +101,7 @@ def insert_random_works(count=100): creators=[ReleaseContrib(creator=a) for a in list(authors)], #work=work, container=random.choice(container_ids)) - release_id2 = ReleaseIdent(revision=release2) + release_id2 = ReleaseIdent(rev=release2) work_revs.append(work) work_ids.append(work_id) release_revs.append(release) @@ -117,7 +117,7 @@ def insert_random_works(count=100): url="http://archive.invalid/{}".format(file_sha), releases=[FileRelease(release=release_id), FileRelease(release=release_id2)], ) - file_id = FileIdent(revision=file_rev) + file_id = FileIdent(rev=file_rev) file_revs.append(file_rev) file_ids.append(file_id) diff --git a/fatcat/models.py b/fatcat/models.py index 00478b08..c145b6a7 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -74,7 +74,7 @@ class WorkIdent(db.Model): is_live = db.Column(db.Boolean, nullable=False, default=False) rev_id = db.Column(db.ForeignKey('work_rev.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('work_ident.id'), nullable=True) - revision = db.relationship("WorkRev") + rev = db.relationship("WorkRev") class WorkEdit(db.Model): __tablename__ = 'work_edit' @@ -115,7 +115,7 @@ class ReleaseIdent(db.Model): is_live = db.Column(db.Boolean, nullable=False, default=False) rev_id = db.Column(db.ForeignKey('release_rev.id')) redirect_id = db.Column(db.ForeignKey('release_ident.id'), nullable=True) - revision = db.relationship("ReleaseRev") + rev = db.relationship("ReleaseRev") class ReleaseEdit(db.Model): __tablename__ = 'release_edit' @@ -142,7 +142,7 @@ class CreatorIdent(db.Model): is_live = db.Column(db.Boolean, nullable=False, default=False) rev_id = db.Column(db.ForeignKey('creator_rev.id')) redirect_id = db.Column(db.ForeignKey('creator_ident.id'), nullable=True) - revision = db.relationship("CreatorRev") + rev = db.relationship("CreatorRev") class CreatorEdit(db.Model): __tablename__ = 'creator_edit' @@ -171,7 +171,7 @@ class ContainerIdent(db.Model): is_live = db.Column(db.Boolean, nullable=False, default=False) rev_id = db.Column(db.ForeignKey('container_rev.id')) redirect_id = db.Column(db.ForeignKey('container_ident.id'), nullable=True) - revision = db.relationship("ContainerRev") + rev = db.relationship("ContainerRev") class ContainerEdit(db.Model): __tablename__ = 'container_edit' @@ -197,9 +197,9 @@ class FileIdent(db.Model): __tablename__ = 'file_ident' id = db.Column(db.Integer, primary_key=True) is_live = db.Column(db.Boolean, nullable=False, default=False) - rev_id = db.Column('revision', db.ForeignKey('file_rev.id')) + rev_id = db.Column(db.ForeignKey('file_rev.id')) redirect_id = db.Column(db.ForeignKey('file_ident.id'), nullable=True) - revision = db.relationship("FileRev") + rev = db.relationship("FileRev") class FileEdit(db.Model): __tablename__ = 'file_edit' @@ -242,50 +242,98 @@ class ExtraJson(db.Model): ### Marshmallow Wrappers #################################################### -# TODO: base class for entities and edits - -class WorkRevSchema(ma.ModelSchema): - class Meta: - model = WorkRev - -class WorkSchema(ma.ModelSchema): +class EntitySchema(ma.ModelSchema): @post_dump(pass_many=False) def merge_rev(self, data): - if data.get('revision', None): - rev_id = data['revision'].pop('id') - data.update(data['revision']) - data.pop('revision') + print(data) + if data.get('rev', None) != None: + rev_id = data['rev'].pop('id') + data.update(data['rev']) data['rev'] = rev_id + else: + data['rev'] = None + # TODO: should be able to set as an allow_none field somewhere if not data.get('redirect_id', None): data['redirect_id'] = None + print(data) + +class WorkRevSchema(ma.ModelSchema): + class Meta: + model = WorkRev + +class WorkSchema(EntitySchema): class Meta: model = WorkIdent + rev = ma.Nested(WorkRevSchema) - revision = ma.Nested(WorkRevSchema) +class WorkEditSchema(ma.ModelSchema): + class Meta: + model = WorkEdit work_schema = WorkSchema() +work_edit_schema = WorkEditSchema() -class FileSchema(ma.ModelSchema): - @post_dump(pass_many=False) - def merge_rev(self, data): - if data['revision']: - data['revision'].pop('id') - data.update(data['revision']) - data.pop('revision') +class ReleaseRevSchema(ma.ModelSchema): + class Meta: + model = ReleaseRev + +class ReleaseSchema(EntitySchema): + class Meta: + model = ReleaseIdent + rev = ma.Nested(ReleaseRevSchema) +class ReleaseEditSchema(ma.ModelSchema): class Meta: - model = FileIdent + model = ReleaseEdit + +release_schema = ReleaseSchema() +release_edit_schema = ReleaseEditSchema() + + +class CreatorRevSchema(ma.ModelSchema): + class Meta: + model = CreatorRev + +class CreatorSchema(EntitySchema): + class Meta: + model = CreatorIdent + rev = ma.Nested(CreatorRevSchema) + +class CreatorEditSchema(ma.ModelSchema): + class Meta: + model = CreatorEdit + +creator_schema = CreatorSchema() +creator_edit_schema = CreatorEditSchema() + + +class ContainerRevSchema(ma.ModelSchema): + class Meta: + model = ContainerRev + +class ContainerSchema(EntitySchema): + class Meta: + model = ContainerIdent + rev = ma.Nested(ContainerRevSchema) + +class ContainerEditSchema(ma.ModelSchema): + class Meta: + model = ContainerEdit + +container_schema = ContainerSchema() +container_edit_schema = ContainerEditSchema() -file_schema = FileSchema() -class FileEditSchema(ma.ModelSchema): +class Editor(ma.ModelSchema): class Meta: - model = FileEdit + model = Editor class EditGroupSchema(ma.ModelSchema): class Meta: model = EditGroup +editor_schema = EditGroupSchema() +edit_group_schema = EditGroupSchema() diff --git a/fatcat/sql.py b/fatcat/sql.py index 3a9f0544..61868ce9 100644 --- a/fatcat/sql.py +++ b/fatcat/sql.py @@ -21,7 +21,7 @@ def add_crossref(meta): sortname="{}, {}".format(am['family'], am['given']), orcid=None) author_revs.append(ar) - author_ids.append(CreatorIdent(revision=ar)) + author_ids.append(CreatorIdent(rev=ar)) # container container = ContainerRev( @@ -30,11 +30,11 @@ def add_crossref(meta): #container_id=None, publisher=meta['publisher'], sortname=meta['short-container-title'][0]) - container_id = ContainerIdent(revision=container) + container_id = ContainerIdent(rev=container) # work and release work = WorkRev(title=title) - work_id = WorkIdent(revision=work) + work_id = WorkIdent(rev=work) release = ReleaseRev( title=title, creators=[ReleaseContrib(creator=a) for a in author_ids], @@ -47,7 +47,7 @@ def add_crossref(meta): issue=meta.get('issue', None), volume=meta.get('volume', None), pages=meta.get('page', None)) - release_id = ReleaseIdent(revision=release) + release_id = ReleaseIdent(rev=release) work.primary_release = release extra = json.dumps({ 'crossref': { @@ -87,19 +87,19 @@ def hydrate_work(wid): "is_live": work.is_live, "redirect_id": work.redirect_id, } - if not work.revision: + if not work.rev: # TODO: look up edit id here from changelog? hydro["edit_id"] = None return hydro primary = None - if work.revision.primary_release_id: - primary = hydrate_release(work.revision.primary_release_id) - #releases = [r.id for r in ReleaseIdent.query.filter(ReleaseIdent.revision.work_id==work.id).all()] + if work.rev.primary_release_id: + primary = hydrate_release(work.rev.primary_release_id) + #releases = [r.id for r in ReleaseIdent.query.filter(ReleaseIdent.rev.work_id==work.id).all()] releases = [] hydro.update({ - "work_type": work.revision.work_type, - "title": work.revision.title, + "work_type": work.rev.work_type, + "title": work.rev.title, "primary": primary, "releases": releases, }) @@ -113,13 +113,13 @@ def hydrate_release(rid): return { "_type": "release", "id": rid, - "revision": release.rev_id, - #"edit_id": release.revision.edit_id, + "rev": release.rev_id, + #"edit_id": release.rev.edit_id, "is_live": release.is_live, - "work_id": release.revision.work_ident_id, - "release_type": release.revision.release_type, - "title": release.revision.title, + "work_id": release.rev.work_ident_id, + "release_type": release.rev.release_type, + "title": release.rev.title, "creators": [], "releases": [], "files": [], |