aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fatcat/api.py41
-rw-r--r--fatcat/models.py6
-rw-r--r--fatcat/routes.py2
-rw-r--r--fatcat/sql.py7
-rw-r--r--tests/test_backend.py21
5 files changed, 68 insertions, 9 deletions
diff --git a/fatcat/api.py b/fatcat/api.py
index b7da49d9..72676760 100644
--- a/fatcat/api.py
+++ b/fatcat/api.py
@@ -1,11 +1,26 @@
from flask import Flask, render_template, send_from_directory, request, \
- url_for, abort, g, redirect, jsonify
+ url_for, abort, g, redirect, jsonify, session
from fatcat import app, db
from fatcat.models import *
from fatcat.sql import *
+### Helpers #################################################################
+
+def get_or_create_edit_group():
+ editor = Editor.query.filter(Editor.id==1).first()
+ if editor.active_edit_group:
+ return editor.active_edit_group
+ else:
+ edit_group = EditGroup(editor=editor)
+ db.session.add(edit_group)
+ db.session.commit()
+ editor.active_edit_group = edit_group
+ db.session.add(editor)
+ db.session.commit()
+ return edit_group
+
### Views ###################################################################
@app.route('/v0/work/<work_id>', methods=['GET'])
@@ -17,6 +32,30 @@ def api_work_get(work_id):
entity = WorkIdent.query.filter(WorkIdent.id==work_id).first_or_404()
return work_schema.jsonify(entity)
+@app.route('/v0/work', methods=['POST'])
+def api_work_create():
+ """
+ 1. find or create edit_group
+ 2. create work_edit, work_rev, work_ident
+ """
+ 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)
+ db.session.commit()
+ return work_schema.jsonify(ident)
+
@app.route('/v0/work/random', methods=['GET'])
def api_work_random():
entity = WorkIdent.query.order_by(db.func.random()).first()
diff --git a/fatcat/models.py b/fatcat/models.py
index 49bdef22..a03486c3 100644
--- a/fatcat/models.py
+++ b/fatcat/models.py
@@ -84,6 +84,7 @@ class WorkEdit(db.Model):
redirect_id = db.Column(db.ForeignKey('work_ident.id'), nullable=True)
edit_group_id = db.Column(db.ForeignKey('edit_group.id'), nullable=True)
extra_json = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True)
+ ident = db.relationship("WorkIdent", foreign_keys="WorkEdit.ident_id") # XXX: add to all other entities
rev = db.relationship("WorkRev")
edit_group = db.relationship("EditGroup")
@@ -216,14 +217,17 @@ 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'), nullable=False)
+ editor_id = db.Column(db.ForeignKey('editor.id'), nullable=False)
description = db.Column(db.String)
+ editor = db.relationship('Editor', foreign_keys='EditGroup.editor_id')
class Editor(db.Model):
__tablename__ = 'editor'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String)
is_admin = db.Column(db.Boolean, nullable=False, default=False)
+ active_edit_group_id = db.Column(db.ForeignKey('edit_group.id', use_alter=True))
+ active_edit_group = db.relationship('EditGroup', foreign_keys='Editor.active_edit_group_id')
class ChangelogEntry(db.Model):
__tablename__= 'changelog'
diff --git a/fatcat/routes.py b/fatcat/routes.py
index 3c1822ce..d0d77ee7 100644
--- a/fatcat/routes.py
+++ b/fatcat/routes.py
@@ -1,7 +1,7 @@
import os
from flask import Flask, render_template, send_from_directory, request, \
- url_for, abort, g, redirect, jsonify
+ url_for, abort, g, redirect, jsonify, session
from fatcat import app, db
diff --git a/fatcat/sql.py b/fatcat/sql.py
index 61868ce9..8e5c0151 100644
--- a/fatcat/sql.py
+++ b/fatcat/sql.py
@@ -6,9 +6,11 @@ from fatcat import db
from fatcat.models import *
def populate_db():
- pass
+ admin_editor = Editor(id=1, username="admin", is_admin=True)
+ db.session.add(admin_editor)
+ db.session.commit()
-def add_crossref(meta):
+def add_crossref_via_model(meta):
title = meta['title'][0]
@@ -76,6 +78,7 @@ def add_crossref(meta):
db.session.add_all(author_ids)
db.session.commit()
+
def hydrate_work(wid):
wid = int(wid)
diff --git a/tests/test_backend.py b/tests/test_backend.py
index 1b470429..97b4ed31 100644
--- a/tests/test_backend.py
+++ b/tests/test_backend.py
@@ -43,6 +43,7 @@ class FatcatTestCase(unittest.TestCase):
def setUp(self):
fatcat.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
fatcat.app.testing = True
+ fatcat.app.debug = True
self.app = fatcat.app.test_client()
fatcat.db.session.remove()
fatcat.db.drop_all()
@@ -54,9 +55,6 @@ class FatcatTestCase(unittest.TestCase):
class ModelTestCase(FatcatTestCase):
- def test_populate(self):
- fatcat.sql.populate_db()
-
def test_example_works(self):
fatcat.dummy.insert_example_works()
@@ -67,7 +65,7 @@ class ModelTestCase(FatcatTestCase):
with open('./tests/files/crossref-works.2018-01-21.badsample.json', 'r') as f:
raw = [json.loads(l) for l in f.readlines() if len(l) > 3]
for obj in raw:
- fatcat.sql.add_crossref(obj)
+ fatcat.sql.add_crossref_via_model(obj)
def test_hydrate_work(self):
fatcat.dummy.insert_random_works()
@@ -174,3 +172,18 @@ class APITestCase(FatcatTestCase):
# Missing Id
rv = self.app.get('/v0/work/r3zga5b9cd7ef8gh084714iljk')
assert rv.status_code == 404
+
+ def test_api_work_create(self):
+ assert WorkIdent.query.count() == 0
+ 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")),
+ headers={"content-type": "application/json"})
+ print(rv)
+ assert rv.status_code == 200
+ assert WorkIdent.query.count() == 1
+ assert WorkRev.query.count() == 1
+ assert WorkEdit.query.count() == 1
+ # not alive yet
+ assert WorkIdent.query.filter(WorkIdent.is_live==True).count() == 0