diff options
-rw-r--r-- | fatcat/api.py | 22 | ||||
-rw-r--r-- | fatcat/models.py | 68 | ||||
-rw-r--r-- | fatcat/sql.py | 4 | ||||
-rw-r--r-- | tests/test_backend.py | 2 |
4 files changed, 80 insertions, 16 deletions
diff --git a/fatcat/api.py b/fatcat/api.py index 175d4d82..b7da49d9 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -12,10 +12,24 @@ from fatcat.sql import * def api_work_get(work_id): if not work_id.isdigit(): return abort(404) - work = hydrate_work(work_id) - return jsonify(work) + #work = hydrate_work(work_id) + #return jsonify(work) + entity = WorkIdent.query.filter(WorkIdent.id==work_id).first_or_404() + return work_schema.jsonify(entity) @app.route('/v0/work/random', methods=['GET']) def api_work_random(): - work = WorkIdent.query.order_by(db.func.random()).first() - return redirect('/v0/work/{}'.format(work.id)) + entity = WorkIdent.query.order_by(db.func.random()).first() + return redirect('/v0/work/{}'.format(entity.id)) + +@app.route('/v0/file/<file_id>', methods=['GET']) +def api_file_get(file_id): + if not file_id.isdigit(): + return abort(404) + entity = FileIdent.query.filter(FileIdent.id==file_id).first_or_404() + return file_schema.jsonify(entity) + +@app.route('/v0/file/random', methods=['GET']) +def api_file_random(): + entity = FileIdent.query.order_by(db.func.random()).first() + return redirect('/v0/file/{}'.format(entity.id)) diff --git a/fatcat/models.py b/fatcat/models.py index aeb3623b..00478b08 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -12,7 +12,7 @@ possible refactors: - use mixins for entities """ -from marshmallow import post_load +from marshmallow import post_dump from fatcat import db, ma @@ -71,7 +71,7 @@ class WorkIdent(db.Model): """ __tablename__ = 'work_ident' id = db.Column(db.Integer, primary_key=True, nullable=False) - live = db.Column(db.Boolean, nullable=False, default=False) + 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") @@ -112,7 +112,7 @@ class ReleaseRev(db.Model): class ReleaseIdent(db.Model): __tablename__ = 'release_ident' id = db.Column(db.Integer, primary_key=True) - live = db.Column(db.Boolean, nullable=False, default=False) + 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") @@ -139,7 +139,7 @@ class CreatorRev(db.Model): class CreatorIdent(db.Model): __tablename__ = 'creator_ident' id = db.Column(db.Integer, primary_key=True) - live = db.Column(db.Boolean, nullable=False, default=False) + 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") @@ -168,7 +168,7 @@ class ContainerRev(db.Model): class ContainerIdent(db.Model): __tablename__ = 'container_ident' id = db.Column(db.Integer, primary_key=True) - live = db.Column(db.Boolean, nullable=False, default=False) + 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") @@ -196,7 +196,7 @@ class FileRev(db.Model): class FileIdent(db.Model): __tablename__ = 'file_ident' id = db.Column(db.Integer, primary_key=True) - live = db.Column(db.Boolean, nullable=False, default=False) + is_live = db.Column(db.Boolean, nullable=False, default=False) rev_id = db.Column('revision', db.ForeignKey('file_rev.id')) redirect_id = db.Column(db.ForeignKey('file_ident.id'), nullable=True) revision = db.relationship("FileRev") @@ -216,15 +216,14 @@ class FileEdit(db.Model): class EditGroup(db.Model): __tablename__ = 'edit_group' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - editor = db.Column(db.ForeignKey('editor.id')) - description = db.Column(db.String) editor = db.Column(db.ForeignKey('editor.id'), nullable=False) + description = db.Column(db.String) class Editor(db.Model): __tablename__ = 'editor' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String) - group = db.Column(db.String) + is_admin = db.Column(db.Boolean, nullable=False, default=False) class ChangelogEntry(db.Model): __tablename__= 'changelog' @@ -239,3 +238,54 @@ class ExtraJson(db.Model): __tablename__ = 'extra_json' sha1 = db.Column(db.String, primary_key=True) json = db.Column(db.String) + + +### Marshmallow Wrappers #################################################### + +# TODO: base class for entities and edits + +class WorkRevSchema(ma.ModelSchema): + class Meta: + model = WorkRev + +class WorkSchema(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') + data['rev'] = rev_id + if not data.get('redirect_id', None): + data['redirect_id'] = None + + class Meta: + model = WorkIdent + + revision = ma.Nested(WorkRevSchema) + +work_schema = WorkSchema() + +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 Meta: + model = FileIdent + +file_schema = FileSchema() + +class FileEditSchema(ma.ModelSchema): + class Meta: + model = FileEdit + +class EditGroupSchema(ma.ModelSchema): + class Meta: + model = EditGroup + diff --git a/fatcat/sql.py b/fatcat/sql.py index c38e6f0e..3a9f0544 100644 --- a/fatcat/sql.py +++ b/fatcat/sql.py @@ -84,7 +84,7 @@ def hydrate_work(wid): "_type": "work", "id": wid, "rev": work.rev_id, - "is_live": work.live, + "is_live": work.is_live, "redirect_id": work.redirect_id, } if not work.revision: @@ -115,7 +115,7 @@ def hydrate_release(rid): "id": rid, "revision": release.rev_id, #"edit_id": release.revision.edit_id, - "is_live": release.live, + "is_live": release.is_live, "work_id": release.revision.work_ident_id, "release_type": release.revision.release_type, diff --git a/tests/test_backend.py b/tests/test_backend.py index 7a139ef7..4c9169f3 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -12,7 +12,7 @@ import tempfile ## Helpers ################################################################## def check_entity_fields(e): - for key in ('id', 'rev', 'redirect_id'): + for key in ('id', 'rev', 'is_live', 'redirect_id'): assert key in e for key in ('id',): assert e[key] is not None |