diff options
-rw-r--r-- | fatcat/api.py | 18 | ||||
-rw-r--r-- | fatcat/models.py | 69 | ||||
-rw-r--r-- | fatcat/sql.py | 6 | ||||
-rw-r--r-- | tests/api.py | 2 |
4 files changed, 30 insertions, 65 deletions
diff --git a/fatcat/api.py b/fatcat/api.py index 24ec8011..0712081e 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -43,8 +43,7 @@ def api_work_create(): ident = WorkIdent(is_live=False, rev=rev) edit = WorkEdit(editgroup=editgroup, 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()) + rev.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8') db.session.add_all([edit, ident, rev]) db.session.commit() return work_schema.jsonify(ident) @@ -90,8 +89,7 @@ def api_release_create(): ident = ReleaseIdent(is_live=False, rev=rev) edit = ReleaseEdit(editgroup=editgroup, 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()) + rev.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8') db.session.add_all([edit, ident, rev]) db.session.commit() return release_schema.jsonify(ident) @@ -139,8 +137,7 @@ def api_creator_create(): ident = CreatorIdent(is_live=False, rev=rev) edit = CreatorEdit(editgroup=editgroup, 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()) + rev.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8') db.session.add_all([edit, ident, rev]) db.session.commit() return creator_schema.jsonify(ident) @@ -176,8 +173,7 @@ def api_container_create(): ident = ContainerIdent(is_live=False, rev=rev) edit = ContainerEdit(editgroup=editgroup, 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()) + rev.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8') db.session.add_all([edit, ident, rev]) db.session.commit() return container_schema.jsonify(ident) @@ -218,8 +214,7 @@ def api_file_create(): ident = FileIdent(is_live=False, rev=rev) edit = FileEdit(editgroup=editgroup, 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()) + rev.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8') db.session.add_all([edit, ident, rev]) db.session.commit() return file_schema.jsonify(ident) @@ -242,8 +237,7 @@ def api_editgroup_create(): description=params.get('description', None), ) if params.get('extra', None): - ser = json.dumps(params['extra'], indent=False).encode('utf-8') - eg.extra_json = ExtraJson(json=ser, sha1=hashlib.sha1(ser).hexdigest()) + eg.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8') db.session.add(eg) db.session.commit() return editgroup_schema.jsonify(eg) diff --git a/fatcat/models.py b/fatcat/models.py index 1bb648c8..c35e541f 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -58,8 +58,7 @@ class FileRelease(db.Model): class WorkRev(db.Model): __tablename__ = 'work_rev' id = db.Column(db.Integer, primary_key=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) title = db.Column(db.String) work_type = db.Column(db.String) @@ -88,8 +87,7 @@ class WorkEdit(db.Model): rev_id = db.Column(db.ForeignKey('work_rev.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('work_ident.id'), nullable=True) editgroup_id = db.Column(db.ForeignKey('editgroup.id'), nullable=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) ident = db.relationship("WorkIdent", foreign_keys="WorkEdit.ident_id") rev = db.relationship("WorkRev") editgroup = db.relationship("EditGroup") @@ -98,8 +96,7 @@ class WorkEdit(db.Model): class ReleaseRev(db.Model): __tablename__ = 'release_rev' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) 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) @@ -132,8 +129,7 @@ class ReleaseEdit(db.Model): rev_id = db.Column(db.ForeignKey('release_rev.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('release_ident.id'), nullable=True) editgroup_id = db.Column(db.ForeignKey('editgroup.id'), nullable=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) ident = db.relationship("ReleaseIdent", foreign_keys="ReleaseEdit.ident_id") rev = db.relationship("ReleaseRev") editgroup = db.relationship("EditGroup") @@ -142,8 +138,7 @@ class ReleaseEdit(db.Model): class CreatorRev(db.Model): __tablename__ = 'creator_rev' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) name = db.Column(db.String) sortname = db.Column(db.String) @@ -164,8 +159,7 @@ class CreatorEdit(db.Model): rev_id = db.Column(db.ForeignKey('creator_rev.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('creator_ident.id'), nullable=True) editgroup_id = db.Column(db.ForeignKey('editgroup.id'), nullable=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) ident = db.relationship("CreatorIdent", foreign_keys="CreatorEdit.ident_id") rev = db.relationship("CreatorRev") editgroup = db.relationship("EditGroup") @@ -174,8 +168,7 @@ class CreatorEdit(db.Model): class ContainerRev(db.Model): __tablename__ = 'container_rev' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) name = db.Column(db.String) parent_id = db.Column(db.ForeignKey('container_ident.id', use_alter=True)) @@ -199,8 +192,7 @@ class ContainerEdit(db.Model): rev_id = db.Column(db.ForeignKey('container_rev.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('container_ident.id'), nullable=True) editgroup_id = db.Column(db.ForeignKey('editgroup.id'), nullable=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) ident = db.relationship("ContainerIdent", foreign_keys="ContainerEdit.ident_id") rev = db.relationship("ContainerRev") editgroup = db.relationship("EditGroup") @@ -209,8 +201,7 @@ class ContainerEdit(db.Model): class FileRev(db.Model): __tablename__ = 'file_rev' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) size = db.Column(db.Integer) sha1 = db.Column(db.String) # TODO: hash table... only or in addition? @@ -232,8 +223,7 @@ class FileEdit(db.Model): rev_id = db.Column(db.ForeignKey('file_rev.id'), nullable=True) redirect_id = db.Column(db.ForeignKey('file_ident.id'), nullable=True) editgroup_id = db.Column(db.ForeignKey('editgroup.id'), nullable=True) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) ident = db.relationship("FileIdent", foreign_keys="FileEdit.ident_id") rev = db.relationship("FileRev") editgroup = db.relationship("EditGroup") @@ -246,8 +236,7 @@ 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) - extra_json_id = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True) - extra_json = db.relationship("ExtraJson") + extra_json = db.Column(db.String, nullable=True) editor = db.relationship("Editor", foreign_keys="EditGroup.editor_id") @@ -267,37 +256,25 @@ class ChangelogEntry(db.Model): editgroup = db.relationship("EditGroup") -### Other ################################################################### - -class ExtraJson(db.Model): - __tablename__ = 'extra_json' - sha1 = db.Column(db.String, primary_key=True, nullable=False) - json = db.Column(db.String, nullable=False) - - ### Marshmallow Wrappers #################################################### class ExtraJsonSchema(ma.ModelSchema): - class Meta: - model = ExtraJson - @post_dump(pass_many=False) - def unflatten(self, data): - assert hashlib.sha1sum(data['json']).hexdigest() == data['sha1'] - data.pop('sha1') - raw = data.pop('json') - data.update(json.loads(raw)) + def json_unflatten(self, data): + extra = data.pop('extra_json', None) + if extra != None: + extra = json.loads(extra) + data['extra'] = extra @pre_load(pass_many=False) - def flatten(self, data): - raw = json.dumps(data, indent=None) - for k in list(data.keys()): - data.pop(k) - data['sha1'] = hashlib.sha1sum(raw).hexdigest() - data['json'] = raw + def json_flatten(self, data): + extra = data.pop('extra', None) + if extra != None: + extra = json.dumps(extra) + data['extra_json'] = extra -class EntitySchema(ma.ModelSchema): +class EntitySchema(ExtraJsonSchema): @post_dump(pass_many=False) def merge_rev(self, data): @@ -308,8 +285,6 @@ class EntitySchema(ma.ModelSchema): else: data['rev'] = None - extra_json = ma.Nested(ExtraJsonSchema) - class ReleaseContribSchema(ma.ModelSchema): class Meta: model = ReleaseContrib diff --git a/fatcat/sql.py b/fatcat/sql.py index 08630c98..9b1922ba 100644 --- a/fatcat/sql.py +++ b/fatcat/sql.py @@ -54,7 +54,7 @@ def add_crossref_via_model(meta): pages=meta.get('page', None)) release_id = ReleaseIdent(rev=release) work.primary_release = release_id - extra = json.dumps({ + release.extra_json = json.dumps({ 'crossref': { 'links': meta.get('link', []), 'subject': meta['subject'], @@ -62,8 +62,6 @@ def add_crossref_via_model(meta): 'alternative-id': meta.get('alternative-id', []), } }, indent=None).encode('utf-8') - extra_json = ExtraJson(json=extra, sha1=hashlib.sha1(extra).hexdigest()) - release.extra_json_id = extra_json.sha1 # references for i, rm in enumerate(meta.get('reference', [])): @@ -76,7 +74,7 @@ def add_crossref_via_model(meta): release.refs.append(ref) db.session.add_all([work, work_id, release, release_id, container, - container_id, extra_json]) + container_id]) db.session.add_all(author_revs) db.session.add_all(author_ids) db.session.commit() diff --git a/tests/api.py b/tests/api.py index bb7007c1..02875f64 100644 --- a/tests/api.py +++ b/tests/api.py @@ -42,7 +42,6 @@ def test_api_work_create(app): assert WorkIdent.query.count() == 0 assert WorkRev.query.count() == 0 assert WorkEdit.query.count() == 0 - assert ExtraJson.query.count() == 0 rv = app.post('/v0/work', data=json.dumps(dict(title="dummy", work_type="thing", extra=dict(a=1, b="zing"))), headers={"content-type": "application/json"}) @@ -51,7 +50,6 @@ def test_api_work_create(app): assert WorkIdent.query.count() == 1 assert WorkRev.query.count() == 1 assert WorkEdit.query.count() == 1 - assert ExtraJson.query.count() == 1 # not alive yet assert WorkIdent.query.filter(WorkIdent.is_live==True).count() == 0 |