aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-04-14 15:37:03 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-04-14 15:37:03 -0700
commit097479244bcfcd9e016b0f5a842672018c0a49b5 (patch)
treeaf8ee3171014d536a93b0f2f974a04ffa21150c1
parente6eecb51ec0904d954c343c1459643e251a7a1a4 (diff)
downloadfatcat-097479244bcfcd9e016b0f5a842672018c0a49b5.tar.gz
fatcat-097479244bcfcd9e016b0f5a842672018c0a49b5.zip
fix up some more relationships
-rw-r--r--fatcat/api.py17
-rw-r--r--fatcat/dummy.py4
-rw-r--r--fatcat/models.py9
-rw-r--r--fatcat/sql.py52
-rw-r--r--tests/test_backend.py10
5 files changed, 17 insertions, 75 deletions
diff --git a/fatcat/api.py b/fatcat/api.py
index 72676760..70346754 100644
--- a/fatcat/api.py
+++ b/fatcat/api.py
@@ -27,8 +27,6 @@ def get_or_create_edit_group():
def api_work_get(work_id):
if not work_id.isdigit():
return abort(404)
- #work = hydrate_work(work_id)
- #return jsonify(work)
entity = WorkIdent.query.filter(WorkIdent.id==work_id).first_or_404()
return work_schema.jsonify(entity)
@@ -37,22 +35,21 @@ def api_work_create():
"""
1. find or create edit_group
2. create work_edit, work_rev, work_ident
+
+ TODO: use marshmallow?
"""
- print(request)
params = request.get_json()
- print(params)
edit_group = get_or_create_edit_group()
- edit = WorkEdit(edit_group=edit_group)
rev = WorkRev(
title=params.get('title', None),
work_type=params.get('work_type', None),
)
ident = WorkIdent(is_live=False, rev=rev)
- edit.ident = ident
- edit.rev = rev
- db.session.add(edit)
- db.session.add(ident)
- db.session.add(rev)
+ edit = WorkEdit(edit_group=edit_group, ident=ident, rev=rev)
+ if params.get('extra', None):
+ ser = json.dumps(params['extra'], indent=False).encode('utf-8')
+ rev.extra_json = ExtraJson(json=ser, sha1=hashlib.sha1(ser).hexdigest())
+ db.session.add_all([edit, ident, rev])
db.session.commit()
return work_schema.jsonify(ident)
diff --git a/fatcat/dummy.py b/fatcat/dummy.py
index 7e09f9c9..f22c4dcb 100644
--- a/fatcat/dummy.py
+++ b/fatcat/dummy.py
@@ -25,7 +25,7 @@ def insert_example_works():
pi_contrib = ReleaseContrib(creator=n_elkies_id)
pi_release.creators.append(pi_contrib)
pi_release_id = ReleaseIdent(rev=pi_release)
- pi_work.primary_release = pi_release
+ pi_work.primary_release = pi_release_id
# TODO:
#pi_file = File(
@@ -94,7 +94,7 @@ def insert_random_works(count=100):
#work=work,
container=random.choice(container_ids))
release_id = ReleaseIdent(rev=release)
- work.primary_release = release
+ work.primary_release = release_id
authors.add(random.choice(author_ids))
release2 = ReleaseRev(
title=work.title + " (again)",
diff --git a/fatcat/models.py b/fatcat/models.py
index a03486c3..25791cd7 100644
--- a/fatcat/models.py
+++ b/fatcat/models.py
@@ -55,11 +55,13 @@ class FileRelease(db.Model):
class WorkRev(db.Model):
__tablename__ = 'work_rev'
id = db.Column(db.Integer, primary_key=True)
- extra_json = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True)
+ extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True)
+ extra_json = db.relationship("ExtraJson") # XXX: for all entities
title = db.Column(db.String)
work_type = db.Column(db.String)
primary_release_id = db.Column(db.ForeignKey('release_ident.id'), nullable=True)
+ primary_release = db.relationship('ReleaseIdent')
class WorkIdent(db.Model):
"""
@@ -161,10 +163,11 @@ class ContainerRev(db.Model):
extra_json = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True)
name = db.Column(db.String)
- #XXX: container_ident_id = db.Column(db.ForeignKey('container_ident.id'))
+ parent_id = db.Column(db.ForeignKey('container_ident.id', use_alter=True))
publisher = db.Column(db.String) # TODO: foreign key
sortname = db.Column(db.String)
issn = db.Column(db.String) # TODO: identifier table
+ parent = db.relationship("ContainerIdent", foreign_keys="ContainerRev.parent_id")
class ContainerIdent(db.Model):
__tablename__ = 'container_ident'
@@ -172,7 +175,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)
- rev = db.relationship("ContainerRev")
+ rev = db.relationship("ContainerRev", foreign_keys="ContainerIdent.rev_id")
class ContainerEdit(db.Model):
__tablename__ = 'container_edit'
diff --git a/fatcat/sql.py b/fatcat/sql.py
index 8e5c0151..fb713269 100644
--- a/fatcat/sql.py
+++ b/fatcat/sql.py
@@ -50,7 +50,7 @@ def add_crossref_via_model(meta):
volume=meta.get('volume', None),
pages=meta.get('page', None))
release_id = ReleaseIdent(rev=release)
- work.primary_release = release
+ work.primary_release = release_id
extra = json.dumps({
'crossref': {
'links': meta.get('link', []),
@@ -78,53 +78,3 @@ def add_crossref_via_model(meta):
db.session.add_all(author_ids)
db.session.commit()
-
-def hydrate_work(wid):
-
- wid = int(wid)
- work = WorkIdent.query.filter(WorkIdent.id==wid).first_or_404()
- hydro = {
- "_type": "work",
- "id": wid,
- "rev": work.rev_id,
- "is_live": work.is_live,
- "redirect_id": work.redirect_id,
- }
- if not work.rev:
- # TODO: look up edit id here from changelog?
- hydro["edit_id"] = None
- return hydro
-
- primary = None
- 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.rev.work_type,
- "title": work.rev.title,
- "primary": primary,
- "releases": releases,
- })
- return hydro
-
-def hydrate_release(rid):
-
- wid = int(rid)
- release = ReleaseIdent.query.filter(ReleaseIdent.id==rid).first_or_404()
-
- return {
- "_type": "release",
- "id": rid,
- "rev": release.rev_id,
- #"edit_id": release.rev.edit_id,
- "is_live": release.is_live,
-
- "work_id": release.rev.work_ident_id,
- "release_type": release.rev.release_type,
- "title": release.rev.title,
- "creators": [],
- "releases": [],
- "files": [],
- "references": [],
- }
diff --git a/tests/test_backend.py b/tests/test_backend.py
index 97b4ed31..873009f9 100644
--- a/tests/test_backend.py
+++ b/tests/test_backend.py
@@ -67,14 +67,6 @@ class ModelTestCase(FatcatTestCase):
for obj in raw:
fatcat.sql.add_crossref_via_model(obj)
- def test_hydrate_work(self):
- fatcat.dummy.insert_random_works()
- fatcat.sql.hydrate_work(1)
-
- def test_hydrate_release(self):
- fatcat.dummy.insert_random_works()
- fatcat.sql.hydrate_release(1)
-
def test_schema_release_rev(self):
assert ReleaseRev.query.count() == 0
e = {
@@ -178,7 +170,7 @@ class APITestCase(FatcatTestCase):
assert WorkRev.query.count() == 0
assert WorkEdit.query.count() == 0
rv = self.app.post('/v0/work',
- data=json.dumps(dict(title="dummy", work_type="thing")),
+ data=json.dumps(dict(title="dummy", work_type="thing", extra=dict(a=1, b="zing"))),
headers={"content-type": "application/json"})
print(rv)
assert rv.status_code == 200