summaryrefslogtreecommitdiffstats
path: root/python/fatcat
diff options
context:
space:
mode:
Diffstat (limited to 'python/fatcat')
-rw-r--r--python/fatcat/__init__.py15
-rw-r--r--python/fatcat/api.py280
-rw-r--r--python/fatcat/api_client.py175
-rw-r--r--python/fatcat/dummy.py135
-rw-r--r--python/fatcat/models.py429
-rw-r--r--python/fatcat/routes.py129
-rw-r--r--python/fatcat/sql.py150
-rw-r--r--python/fatcat/static/robots.txt1
-rw-r--r--python/fatcat/templates/404.html6
-rw-r--r--python/fatcat/templates/about.html161
-rw-r--r--python/fatcat/templates/base.html70
-rw-r--r--python/fatcat/templates/container_add.html168
-rw-r--r--python/fatcat/templates/container_view.html14
-rw-r--r--python/fatcat/templates/creator_view.html10
-rw-r--r--python/fatcat/templates/editgroup_view.html49
-rw-r--r--python/fatcat/templates/editor_changelog.html17
-rw-r--r--python/fatcat/templates/editor_view.html9
-rw-r--r--python/fatcat/templates/file_view.html10
-rw-r--r--python/fatcat/templates/home.html29
-rw-r--r--python/fatcat/templates/release_changelog.html17
-rw-r--r--python/fatcat/templates/release_view.html31
-rw-r--r--python/fatcat/templates/work_add.html215
-rw-r--r--python/fatcat/templates/work_view.html37
23 files changed, 2157 insertions, 0 deletions
diff --git a/python/fatcat/__init__.py b/python/fatcat/__init__.py
new file mode 100644
index 00000000..a824d220
--- /dev/null
+++ b/python/fatcat/__init__.py
@@ -0,0 +1,15 @@
+
+from flask import Flask
+from flask_sqlalchemy import SQLAlchemy
+from flask_marshmallow import Marshmallow
+from flask_debugtoolbar import DebugToolbarExtension
+from config import Config
+
+toolbar = DebugToolbarExtension()
+app = Flask(__name__)
+app.config.from_object(Config)
+db = SQLAlchemy(app)
+ma = Marshmallow(app)
+toolbar = DebugToolbarExtension(app)
+
+from fatcat import routes, models, api, sql, dummy
diff --git a/python/fatcat/api.py b/python/fatcat/api.py
new file mode 100644
index 00000000..2c91533b
--- /dev/null
+++ b/python/fatcat/api.py
@@ -0,0 +1,280 @@
+
+from flask import Flask, render_template, send_from_directory, request, \
+ 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_editgroup(param=None):
+ if param != None:
+ editgroup = EditGroup.query.get_or_404(int(param))
+ return editgroup
+ editor = Editor.query.get_or_404(1)
+ if editor.active_editgroup:
+ return editor.active_editgroup
+
+ editgroup = EditGroup(editor=editor)
+ db.session.add(editgroup)
+ db.session.commit()
+ editor.active_editgroup = editgroup
+ db.session.add(editor)
+ db.session.commit()
+ return editgroup
+
+### Views ###################################################################
+
+@app.route('/v0/work/<int:ident>', methods=['GET'])
+def api_work_get(ident):
+ entity = WorkIdent.query.get_or_404(ident)
+ return work_schema.jsonify(entity)
+
+@app.route('/v0/work', methods=['POST'])
+def api_work_create(params=None):
+ # TODO: Special-case to pull out primary and create that?
+ if params == None:
+ params = request.get_json()
+ editgroup = get_or_create_editgroup(params.get('editgroup'))
+ rev = WorkRev(
+ title=params.get('title', None),
+ work_type=params.get('work_type', None),
+ )
+ ident = WorkIdent(is_live=False, rev=rev)
+ edit = WorkEdit(editgroup=editgroup, ident=ident, rev=rev)
+ if params.get('extra', None):
+ 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)
+
+@app.route('/v0/work/random', methods=['GET'])
+def api_work_random():
+ entity = WorkIdent.query.order_by(db.func.random()).first()
+ return redirect('/v0/work/{}'.format(entity.id))
+
+
+@app.route('/v0/release/<int:ident>', methods=['GET'])
+def api_release_get(ident):
+ entity = ReleaseIdent.query.get_or_404(ident)
+ return release_schema.jsonify(entity)
+
+@app.route('/v0/release', methods=['POST'])
+def api_release_create(params=None):
+ if params == None:
+ params = request.get_json()
+ editgroup = get_or_create_editgroup(params.get('editgroup'))
+ creators = params.get('creators', [])
+ creators = [CreatorIdent.query.get_or_404(c) for c in creators]
+ targets = [ref['target'] for ref in params.get('refs', []) if ref.get('target') != None]
+ targets = [ReleaseIdent.query.get_or_404(t) for t in targets]
+ work = params.get('work')
+ if work:
+ work = WorkIdent.query.get_or_404(work)
+ container = params.get('container')
+ if container:
+ container = ContainerIdent.query.get_or_404(container)
+ rev = ReleaseRev(
+ title=params.get('title', None),
+ release_type=params.get('release_type', None),
+ work=work,
+ container=container,
+ doi=params.get('doi', None),
+ )
+ contribs = [ReleaseContrib(release=rev, creator=c) for c in creators]
+ rev.creators = contribs
+ db.session.add_all(contribs)
+ refs = [ReleaseRef(release=rev, target=t) for t in targets]
+ rev.refs = refs
+ db.session.add_all(refs)
+ ident = ReleaseIdent(is_live=False, rev=rev)
+ edit = ReleaseEdit(editgroup=editgroup, ident=ident, rev=rev)
+ if params.get('extra', None):
+ 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)
+
+@app.route('/v0/release/<int:ident>/changelog', methods=['GET'])
+def api_release_changelog(ident):
+ entries = ChangelogEntry.query\
+ .join(ReleaseEdit.editgroup)\
+ .filter(ReleaseEdit.ident_id==ident)\
+ .all()
+ return changelogentry_schema.jsonify(entries, many=True)
+
+@app.route('/v0/release/random', methods=['GET'])
+def api_release_random():
+ entity = ReleaseIdent.query.order_by(db.func.random()).first()
+ return redirect('/v0/release/{}'.format(entity.id))
+
+@app.route('/v0/release/lookup', methods=['GET'])
+def api_release_lookup():
+ params = request.get_json()
+ doi = params['doi'].strip().lower()
+ # TODO: proper regex
+ if not (doi.startswith("10.") and len(doi.split('/')) == 2):
+ abort(400)
+ entity = ReleaseIdent.query\
+ .join(ReleaseIdent.rev)\
+ .filter(ReleaseRev.doi==doi)\
+ .first_or_404()
+ return release_schema.jsonify(entity)
+
+
+@app.route('/v0/creator/<int:ident>', methods=['GET'])
+def api_creator_get(ident):
+ entity = CreatorIdent.query.get_or_404(ident)
+ return creator_schema.jsonify(entity)
+
+@app.route('/v0/creator', methods=['POST'])
+def api_creator_create(params=None):
+ if params == None:
+ params = request.get_json()
+ editgroup = get_or_create_editgroup(params.get('editgroup'))
+ rev = CreatorRev(
+ name=params.get('name', None),
+ orcid=params.get('orcid', None),
+ )
+ ident = CreatorIdent(is_live=False, rev=rev)
+ edit = CreatorEdit(editgroup=editgroup, ident=ident, rev=rev)
+ if params.get('extra', None):
+ 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)
+
+@app.route('/v0/creator/lookup', methods=['GET'])
+def api_creator_lookup():
+ params = request.get_json()
+ orcid = params['orcid'].strip()
+ # TODO: proper regex
+ if not (len(orcid) == len("0000-0002-1825-0097") and len(orcid.split('-')) == 4):
+ abort(400)
+ entity = CreatorIdent.query\
+ .join(CreatorIdent.rev)\
+ .filter(CreatorRev.orcid==orcid)\
+ .first_or_404()
+ return creator_schema.jsonify(entity)
+
+
+@app.route('/v0/container/<int:ident>', methods=['GET'])
+def api_container_get(ident):
+ entity = ContainerIdent.query.get_or_404(ident)
+ return container_schema.jsonify(entity)
+
+@app.route('/v0/container', methods=['POST'])
+def api_container_create(params=None):
+ if params == None:
+ params = request.get_json()
+ editgroup = get_or_create_editgroup(params.get('editgroup'))
+ rev = ContainerRev(
+ name=params.get('name', None),
+ publisher=params.get('publisher', None),
+ issn=params.get('issn', None),
+ )
+ ident = ContainerIdent(is_live=False, rev=rev)
+ edit = ContainerEdit(editgroup=editgroup, ident=ident, rev=rev)
+ if params.get('extra', None):
+ 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)
+
+@app.route('/v0/container/lookup', methods=['GET'])
+def api_container_lookup():
+ params = request.get_json()
+ issn = params['issn'].strip()
+ # TODO: proper regex
+ if not (len(issn) == 9 and issn[0:4].isdigit() and issn[5:7].isdigit()):
+ abort(400)
+ entity = ContainerIdent.query\
+ .join(ContainerIdent.rev)\
+ .filter(ContainerRev.issn==issn)\
+ .first_or_404()
+ return container_schema.jsonify(entity)
+
+
+@app.route('/v0/file/<int:ident>', methods=['GET'])
+def api_file_get(ident):
+ entity = FileIdent.query.get_or_404(ident)
+ return file_schema.jsonify(entity)
+
+@app.route('/v0/file', methods=['POST'])
+def api_file_create(params=None):
+ if params == None:
+ params = request.get_json()
+ editgroup = get_or_create_editgroup(params.get('editgroup'))
+ releases = params.get('releases', [])
+ releases = [ReleaseIdent.query.get_or_404(r) for r in releases]
+ rev = FileRev(
+ sha1=params.get('sha1', None),
+ size=params.get('size', None),
+ url=params.get('url', None),
+ )
+ file_releases = [FileRelease(file=rev, release=r) for r in releases]
+ rev.releases = file_releases
+ db.session.add_all(file_releases)
+ ident = FileIdent(is_live=False, rev=rev)
+ edit = FileEdit(editgroup=editgroup, ident=ident, rev=rev)
+ if params.get('extra', None):
+ 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)
+
+
+@app.route('/v0/editgroup/<int:ident>', methods=['GET'])
+def api_editgroup_get(ident):
+ entity = EditGroup.query\
+ .join(EditGroup.editor)\
+ .filter(EditGroup.id==ident)\
+ .first_or_404()
+ rv = editgroup_schema.dump(entity).data
+ rv['work_edits'] = work_edit_schema.dump(
+ WorkEdit.query.filter(EditGroup.id==ident).all(), many=True).data
+ rv['release_edits'] = release_edit_schema.dump(
+ ReleaseEdit.query.filter(EditGroup.id==ident).all(), many=True).data
+ rv['creator_edits'] = creator_edit_schema.dump(
+ CreatorEdit.query.filter(EditGroup.id==ident).all(), many=True).data
+ rv['container_edits'] = container_edit_schema.dump(
+ ContainerEdit.query.filter(EditGroup.id==ident).all(), many=True).data
+ rv['file_edits'] = file_edit_schema.dump(
+ FileEdit.query.filter(EditGroup.id==ident).all(), many=True).data
+ return jsonify(rv)
+
+@app.route('/v0/editgroup', methods=['POST'])
+def api_editgroup_create(params=None):
+ if params == None:
+ params = request.get_json()
+ eg = EditGroup(
+ editor_id=1,
+ description=params.get('description', None),
+ )
+ if params.get('extra', None):
+ eg.extra_json = json.dumps(params['extra'], indent=False).encode('utf-8')
+ db.session.add(eg)
+ db.session.commit()
+ return editgroup_schema.jsonify(eg)
+
+@app.route('/v0/editgroup/<int:ident>/accept', methods=['POST'])
+def api_editgroup_accept(ident):
+ entity = EditGroup.query.get_or_404(ident)
+ accept_editgroup(entity)
+ return jsonify({'success': True})
+
+
+@app.route('/v0/editor/<username>', methods=['GET'])
+def api_editor_get(username):
+ entity = Editor.query.filter(Editor.username==username).first_or_404()
+ return editor_schema.jsonify(entity)
+
+@app.route('/v0/editor/<username>/changelog', methods=['GET'])
+def api_editor_changelog(username):
+ entries = ChangelogEntry.query\
+ .join(ChangelogEntry.editgroup)\
+ .join(EditGroup.editor)\
+ .filter(Editor.username==username)\
+ .all()
+ return changelogentry_schema.jsonify(entries, many=True)
diff --git a/python/fatcat/api_client.py b/python/fatcat/api_client.py
new file mode 100644
index 00000000..f2fd6a1d
--- /dev/null
+++ b/python/fatcat/api_client.py
@@ -0,0 +1,175 @@
+
+import sys
+import json
+import requests
+
+
+class FatCatApiClient:
+
+ def __init__(self, host_url):
+ self.host_url = host_url
+ self.session = requests.Session()
+ self._issn_map = dict()
+
+ def get(self, path, data=None):
+ headers = {"content-type": "application/json"}
+ return self.session.get(self.host_url + path, json=data,
+ headers=headers)
+
+ def post(self, path, data=None):
+ headers = {"content-type": "application/json"}
+ return self.session.post(self.host_url + path, json=data,
+ headers=headers)
+
+ def new_editgroup(self):
+ rv = self.post('/v0/editgroup', data=dict(
+ editor=1))
+ assert rv.status_code == 200
+ editgroup_id = rv.json()['id']
+ return editgroup_id
+
+ def accept_editgroup(self, eg):
+ rv = self.post('/v0/editgroup/{}/accept'.format(eg))
+ assert rv.status_code == 200
+ return rv
+
+ def lookup_issn(self, issn):
+ assert len(issn) == 9 and issn[4] == '-'
+ if issn in self._issn_map:
+ return self._issn_map[issn]
+ rv = self.get('/v0/container/lookup', data=dict(issn=issn))
+ container_id = None
+ if rv.status_code == 200:
+ container_id = rv.json()['id']
+ else:
+ # only other valid response is a 404; otherwise we had an error
+ assert rv.status_code == 404
+ self._issn_map[issn] = container_id
+ return container_id
+
+ def import_crossref_file(self, json_file, create_containers=False, batchsize=100):
+ eg = self.new_editgroup()
+ i = 0
+ with open(json_file, 'r') as file:
+ for line in file:
+ if i % batchsize == 0:
+ sys.stdout.write('\n{}: '.format(i))
+ if (i+1) % 20 == 0:
+ sys.stdout.write('.')
+ i = i + 1
+ obj = json.loads(line)
+ if not ("author" in obj and "title" in obj):
+ continue
+ try:
+ self.import_crossref_dict(obj, editgroup=eg,
+ create_containers=create_containers)
+ except Exception as e:
+ print("ERROR: {}".format(e))
+ if i % batchsize == 0:
+ self.accept_editgroup(eg)
+ eg = self.new_editgroup()
+ if i % batchsize != 0:
+ self.accept_editgroup(eg)
+ print("done!")
+
+ def import_crossref_dict(self, meta, editgroup=None,
+ create_containers=False):
+
+ # creators
+ creators = []
+ for am in meta['author']:
+ c = dict(name="{} {}".format(am['given'], am['family']),
+ sortname="{}, {}".format(am['family'], am['given']),
+ orcid=None)
+ creators.append(c)
+
+ # container
+ issn = meta.get('ISSN', [None])[0]
+ container_id = self.lookup_issn(issn)
+ container = dict(
+ issn=issn,
+ name=meta['container-title'][0],
+ container=container_id,
+ #sortname=meta['short-container-title'][0])
+ publisher=meta['publisher'])
+
+ if container_id is None and create_containers and issn != None:
+ rv = self.post('/v0/container', data=dict(
+ issn=container['issn'],
+ publisher=container['publisher']))
+ assert rv.status_code == 200
+ container_id = rv.json()['id']
+ print("created container: {}".format(issn))
+ container['id'] = container_id
+ self._issn_map[issn] = container_id
+
+ # references
+ refs = []
+ for i, rm in enumerate(meta.get('reference', [])):
+ ref = dict(
+ doi=rm.get("DOI", None),
+ index=i+1,
+ # TODO: how to generate a proper stub here from k/v metadata?
+ stub="| ".join(rm.values()))
+ refs.append(ref)
+
+ # work and release
+ title = meta['title'][0]
+ rv = self.post('/v0/work',
+ data=dict(title=title, editgroup=editgroup)) #work_type="book"
+ assert rv.status_code == 200
+ work_id = rv.json()['id']
+
+ extra = dict(crossref={
+ 'links': meta.get('link', []),
+ 'subject': meta.get('subject'),
+ 'type': meta['type'],
+ 'alternative-id': meta.get('alternative-id', [])})
+
+ rv = self.post('/v0/release', data=dict(
+ title=title,
+ work=work_id,
+ # XXX: creators=creators,
+ # XXX: refs=refs,
+ # XXX: container=container_id,
+ release_type=meta['type'],
+ doi=meta['DOI'],
+ date=meta['created']['date-time'],
+ license=meta.get('license', [dict(URL=None)])[0]['URL'] or None,
+ issue=meta.get('issue', None),
+ volume=meta.get('volume', None),
+ pages=meta.get('page', None),
+ editgroup=editgroup,
+ extra=extra))
+ assert rv.status_code == 200
+ release_id = rv.json()['id']
+
+ def import_issn_file(self, json_file, create_containers=False, batchsize=100):
+ eg = self.new_editgroup()
+ i = 0
+ with open(json_file, 'r') as file:
+ for line in file:
+ if i % batchsize == 0:
+ sys.stdout.write('\n{}: '.format(i))
+ if (i+1) % 20 == 0:
+ sys.stdout.write('.')
+ i = i + 1
+ obj = json.loads(line)
+ if not ("author" in obj and "title" in obj):
+ continue
+ try:
+ self.import_crossref_dict(obj, editgroup=eg,
+ create_containers=create_containers)
+ except Exception as e:
+ print("ERROR: {}".format(e))
+ if i % batchsize == 0:
+ self.accept_editgroup(eg)
+ eg = self.new_editgroup()
+ if i % batchsize != 0:
+ self.accept_editgroup(eg)
+ print("done!")
+
+ def health(self):
+ rv = self.get("/health")
+ assert rv.status_code == 200
+ return rv.json()
diff --git a/python/fatcat/dummy.py b/python/fatcat/dummy.py
new file mode 100644
index 00000000..f22c4dcb
--- /dev/null
+++ b/python/fatcat/dummy.py
@@ -0,0 +1,135 @@
+
+import random
+import hashlib
+from fatcat import db
+from fatcat.models import *
+
+def insert_example_works():
+ """
+ TODO: doesn't create an edit trail (yet)
+ """
+
+ n_elkies = CreatorRev(
+ name="Noam D. Elkies",
+ sortname="Elkies, N",
+ orcid=None)
+ 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(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(rev=pi_release)
+ pi_work.primary_release = pi_release_id
+
+ # TODO:
+ #pi_file = File(
+ # sha1="efee52e46c86691e2b892dbeb212f3b92e92e9d3",
+ # url="http://www.math.harvard.edu/~elkies/Misc/pi10.pdf")
+ db.session.add_all([n_elkies, n_elkies_id, pi_work, pi_work_id, pi_release,
+ pi_release_id])
+
+ # TODO:
+ #ligo_collab = CreatorRev(name="LIGO Scientific Collaboration")
+ #ligo_paper = ReleaseRev(
+ # title="Full Band All-sky Search for Periodic Gravitational Waves in the O1 LIGO Data")
+ db.session.commit()
+
+
+def insert_random_works(count=100):
+ """
+ TODO: doesn't create an edit trail (yet)
+ """
+
+ first_names = ("Sarah", "Robin", "Halko", "Jefferson", "Max", "桃井",
+ "Koizumi", "Rex", "Billie", "Tenzin")
+ last_names = ("Headroom", "はるこ", "Jun'ichirō", "Wong", "Smith")
+
+ author_revs = []
+ author_ids = []
+ for _ in range(count):
+ first = random.choice(first_names)
+ last = random.choice(last_names)
+ ar = CreatorRev(
+ name="{} {}".format(first, last),
+ sortname="{}, {}".format(last, first[0]),
+ orcid=None)
+ author_revs.append(ar)
+ author_ids.append(CreatorIdent(rev=ar))
+
+ container_revs = []
+ container_ids = []
+ for _ in range(5):
+ cr = ContainerRev(
+ name="The Fake Journal of Stuff",
+ #container_id=None,
+ publisher="Big Paper",
+ sortname="Fake Journal of Stuff",
+ issn="1234-5678")
+ container_revs.append(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")
+ title_ends = ("Humankind", "Bees", "Democracy", "Avocados", "«küßî»", "“ЌύБЇ”")
+ work_revs = []
+ work_ids = []
+ release_revs = []
+ release_ids = []
+ file_revs = []
+ file_ids = []
+ for _ in range(count):
+ title = "{} {}".format(random.choice(title_start), random.choice(title_ends))
+ work = WorkRev(title=title)
+ 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(rev=release)
+ work.primary_release = release_id
+ authors.add(random.choice(author_ids))
+ release2 = ReleaseRev(
+ title=work.title + " (again)",
+ creators=[ReleaseContrib(creator=a) for a in list(authors)],
+ #work=work,
+ container=random.choice(container_ids))
+ release_id2 = ReleaseIdent(rev=release2)
+ work_revs.append(work)
+ work_ids.append(work_id)
+ release_revs.append(release)
+ release_revs.append(release2)
+ release_ids.append(release_id)
+ release_ids.append(release_id2)
+
+ file_content = str(random.random()) * random.randint(3,100)
+ file_sha = hashlib.sha1(file_content.encode('utf-8')).hexdigest()
+ file_rev = FileRev(
+ sha1=file_sha,
+ size=len(file_content),
+ url="http://archive.invalid/{}".format(file_sha),
+ releases=[FileRelease(release=release_id), FileRelease(release=release_id2)],
+ )
+ file_id = FileIdent(rev=file_rev)
+ file_revs.append(file_rev)
+ file_ids.append(file_id)
+
+ db.session.add_all(author_revs)
+ db.session.add_all(author_ids)
+ db.session.add_all(work_revs)
+ db.session.add_all(work_ids)
+ db.session.add_all(release_revs)
+ db.session.add_all(release_ids)
+ db.session.add_all(container_revs)
+ db.session.add_all(container_ids)
+ db.session.add_all(file_revs)
+ db.session.add_all(file_ids)
+
+ db.session.commit()
diff --git a/python/fatcat/models.py b/python/fatcat/models.py
new file mode 100644
index 00000000..c35e541f
--- /dev/null
+++ b/python/fatcat/models.py
@@ -0,0 +1,429 @@
+
+"""
+states for identifiers:
+- pre-live: points to a rev (during edit/accept period)
+- live: points to a rev
+- redirect: live, points to upstream rev, also points to redirect id
+ => if live and redirect non-null, all other fields copied from redirect target
+- deleted: live, but doesn't point to a rev
+
+possible refactors:
+- '_rev' instead of '_rev'
+- use mixins for entities
+"""
+
+import json
+import hashlib
+from marshmallow import post_dump, pre_load
+from fatcat import db, ma
+
+
+### Inter-Entity Relationships ###############################################
+
+class ReleaseContrib(db.Model):
+ __tablename__ = "release_contrib"
+ release_rev = db.Column(db.ForeignKey('release_rev.id'), nullable=False, primary_key=True)
+ creator_ident_id = db.Column(db.ForeignKey('creator_ident.id'), nullable=False, primary_key=True)
+ stub = db.Column(db.String, nullable=True)
+ type = db.Column(db.String, nullable=True)
+ # TODO: index (int)?
+
+ creator = db.relationship("CreatorIdent")
+ release = db.relationship("ReleaseRev")
+
+class ReleaseRef(db.Model):
+ __tablename__ = "release_ref"
+ id = db.Column(db.Integer, primary_key=True, nullable=False)
+ release_rev = db.Column(db.ForeignKey('release_rev.id'), nullable=False)
+ target_release_ident_id = db.Column(db.ForeignKey('release_ident.id'), nullable=True)
+ index = db.Column(db.Integer, nullable=True)
+ stub = db.Column(db.String, nullable=True)
+ doi = db.Column(db.String, nullable=True)
+
+ release = db.relationship("ReleaseRev")
+ target = db.relationship("ReleaseIdent")
+
+class FileRelease(db.Model):
+ __tablename__ = "file_release"
+ id = db.Column(db.Integer, primary_key=True, nullable=False)
+ file_rev= db.Column(db.ForeignKey('file_rev.id'), nullable=False)
+ release_ident_id = db.Column(db.ForeignKey('release_ident.id'), nullable=False)
+
+ release = db.relationship("ReleaseIdent")
+ file = db.relationship("FileRev")
+
+
+### Entities #################################################################
+
+class WorkRev(db.Model):
+ __tablename__ = 'work_rev'
+ id = db.Column(db.Integer, primary_key=True)
+ extra_json = db.Column(db.String, nullable=True)
+
+ 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):
+ """
+ If rev_id is null, this was deleted.
+ If redirect_id is not null, this has been merged with the given id. In this
+ case rev_id is a "cached" copy of the redirect's rev_id, as
+ an optimization. If the merged work is "deleted", rev_id can be
+ null and redirect_id not-null.
+ """
+ __tablename__ = 'work_ident'
+ id = db.Column(db.Integer, primary_key=True, nullable=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)
+ rev = db.relationship("WorkRev")
+
+class WorkEdit(db.Model):
+ __tablename__ = 'work_edit'
+ id = db.Column(db.Integer, primary_key=True)
+ ident_id = db.Column(db.ForeignKey('work_ident.id'), nullable=True)
+ 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 = db.Column(db.String, nullable=True)
+ ident = db.relationship("WorkIdent", foreign_keys="WorkEdit.ident_id")
+ rev = db.relationship("WorkRev")
+ editgroup = db.relationship("EditGroup")
+
+
+class ReleaseRev(db.Model):
+ __tablename__ = 'release_rev'
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
+ 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)
+ title = db.Column(db.String, nullable=False)
+ license = db.Column(db.String, nullable=True) # TODO: oa status foreign key
+ release_type = db.Column(db.String) # TODO: foreign key
+ date = db.Column(db.String, nullable=True) # TODO: datetime
+ doi = db.Column(db.String, nullable=True) # TODO: identifier table
+ volume = db.Column(db.String, nullable=True)
+ pages = db.Column(db.String, nullable=True)
+ issue = db.Column(db.String, nullable=True)
+
+ work = db.relationship("WorkIdent", lazy='subquery', foreign_keys="ReleaseRev.work_ident_id")
+ container = db.relationship("ContainerIdent", lazy='subquery')
+ creators = db.relationship('ReleaseContrib', lazy='subquery')
+ refs = db.relationship('ReleaseRef', lazy='subquery')
+
+class ReleaseIdent(db.Model):
+ __tablename__ = 'release_ident'
+ id = db.Column(db.Integer, primary_key=True)
+ 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)
+ rev = db.relationship("ReleaseRev")
+
+class ReleaseEdit(db.Model):
+ __tablename__ = 'release_edit'
+ id = db.Column(db.Integer, primary_key=True)
+ ident_id = db.Column(db.ForeignKey('release_ident.id'), nullable=True)
+ 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 = db.Column(db.String, nullable=True)
+ ident = db.relationship("ReleaseIdent", foreign_keys="ReleaseEdit.ident_id")
+ rev = db.relationship("ReleaseRev")
+ editgroup = db.relationship("EditGroup")
+
+
+class CreatorRev(db.Model):
+ __tablename__ = 'creator_rev'
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
+ extra_json = db.Column(db.String, nullable=True)
+
+ name = db.Column(db.String)
+ sortname = db.Column(db.String)
+ orcid = db.Column(db.String) # TODO: identifier table
+
+class CreatorIdent(db.Model):
+ __tablename__ = 'creator_ident'
+ id = db.Column(db.Integer, primary_key=True)
+ 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)
+ rev = db.relationship("CreatorRev")
+
+class CreatorEdit(db.Model):
+ __tablename__ = 'creator_edit'
+ id = db.Column(db.Integer, primary_key=True)
+ ident_id = db.Column(db.ForeignKey('creator_ident.id'), nullable=True)
+ 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 = db.Column(db.String, nullable=True)
+ ident = db.relationship("CreatorIdent", foreign_keys="CreatorEdit.ident_id")
+ rev = db.relationship("CreatorRev")
+ editgroup = db.relationship("EditGroup")
+
+
+class ContainerRev(db.Model):
+ __tablename__ = 'container_rev'
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
+ 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))
+ 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'
+ id = db.Column(db.Integer, primary_key=True)
+ 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", foreign_keys="ContainerIdent.rev_id")
+
+class ContainerEdit(db.Model):
+ __tablename__ = 'container_edit'
+ id = db.Column(db.Integer, primary_key=True)
+ ident_id = db.Column(db.ForeignKey('container_ident.id'), nullable=True)
+ 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 = db.Column(db.String, nullable=True)
+ ident = db.relationship("ContainerIdent", foreign_keys="ContainerEdit.ident_id")
+ rev = db.relationship("ContainerRev")
+ editgroup = db.relationship("EditGroup")
+
+
+class FileRev(db.Model):
+ __tablename__ = 'file_rev'
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
+ 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?
+ url = db.Column(db.Integer) # TODO: URL table
+ releases = db.relationship('FileRelease', lazy='subquery')
+
+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(db.ForeignKey('file_rev.id'))
+ redirect_id = db.Column(db.ForeignKey('file_ident.id'), nullable=True)
+ rev = db.relationship("FileRev")
+
+class FileEdit(db.Model):
+ __tablename__ = 'file_edit'
+ id = db.Column(db.Integer, primary_key=True)
+ ident_id = db.Column(db.ForeignKey('file_ident.id'), nullable=True)
+ 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 = db.Column(db.String, nullable=True)
+ ident = db.relationship("FileIdent", foreign_keys="FileEdit.ident_id")
+ rev = db.relationship("FileRev")
+ editgroup = db.relationship("EditGroup")
+
+
+### Editing #################################################################
+
+class EditGroup(db.Model):
+ __tablename__ = 'editgroup'
+ 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 = db.Column(db.String, nullable=True)
+
+ 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, nullable=False, unique=True)
+ is_admin = db.Column(db.Boolean, nullable=False, default=False)
+ active_editgroup_id = db.Column(db.ForeignKey('editgroup.id', use_alter=True))
+ active_editgroup = db.relationship('EditGroup', foreign_keys='Editor.active_editgroup_id')
+
+class ChangelogEntry(db.Model):
+ __tablename__= 'changelog'
+ id = db.Column(db.Integer, primary_key=True, autoincrement=True)
+ editgroup_id = db.Column(db.ForeignKey('editgroup.id'))
+ timestamp = db.Column(db.Integer)
+ editgroup = db.relationship("EditGroup")
+
+
+### Marshmallow Wrappers ####################################################
+
+class ExtraJsonSchema(ma.ModelSchema):
+
+ @post_dump(pass_many=False)
+ 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 json_flatten(self, data):
+ extra = data.pop('extra', None)
+ if extra != None:
+ extra = json.dumps(extra)
+ data['extra_json'] = extra
+
+class EntitySchema(ExtraJsonSchema):
+
+ @post_dump(pass_many=False)
+ def merge_rev(self, 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
+
+class ReleaseContribSchema(ma.ModelSchema):
+ class Meta:
+ model = ReleaseContrib
+ creator = db.relationship("CreatorIdent")
+ release = db.relationship("ReleaseRev")
+
+class ReleaseRefSchema(ma.ModelSchema):
+ class Meta:
+ model = ReleaseRef
+ release = db.relationship("ReleaseRev")
+ target = db.relationship("ReleaseIdent")
+
+class FileReleaseSchema(ma.ModelSchema):
+ class Meta:
+ model = FileRelease
+ release = db.relationship("ReleaseIdent")
+ file = db.relationship("FileRev")
+
+class WorkRevSchema(ma.ModelSchema):
+ class Meta:
+ model = WorkRev
+ include_fk = True
+
+class WorkSchema(EntitySchema):
+ class Meta:
+ model = WorkIdent
+ include_fk = True
+ rev = ma.Nested(WorkRevSchema)
+
+class WorkEditSchema(ma.ModelSchema):
+ class Meta:
+ model = WorkEdit
+
+work_rev_schema = WorkRevSchema()
+work_schema = WorkSchema()
+work_edit_schema = WorkEditSchema()
+
+
+class ReleaseRevSchema(ma.ModelSchema):
+ class Meta:
+ model = ReleaseRev
+ include_fk = True
+ work = ma.Nested('WorkSchema')
+ container = ma.Nested('ContainerSchema')
+ creators = ma.Nested(ReleaseContribSchema, many=True)
+ refs = ma.Nested(ReleaseRefSchema, many=True)
+
+class ReleaseSchema(EntitySchema):
+ class Meta:
+ model = ReleaseIdent
+ include_fk = True
+ rev = ma.Nested(ReleaseRevSchema)
+ # XXX: files = ma.Nested('FileSchema', many=True)
+
+class ReleaseEditSchema(ma.ModelSchema):
+ class Meta:
+ model = ReleaseEdit
+
+release_rev_schema = ReleaseRevSchema()
+release_schema = ReleaseSchema()
+release_edit_schema = ReleaseEditSchema()
+
+
+class CreatorRevSchema(ma.ModelSchema):
+ class Meta:
+ model = CreatorRev
+ include_fk = True
+
+class CreatorSchema(EntitySchema):
+ class Meta:
+ model = CreatorIdent
+ include_fk = True
+ rev = ma.Nested(CreatorRevSchema)
+
+class CreatorEditSchema(ma.ModelSchema):
+ class Meta:
+ model = CreatorEdit
+
+creator_rev_schema = CreatorRevSchema()
+creator_schema = CreatorSchema()
+creator_edit_schema = CreatorEditSchema()
+
+
+class ContainerRevSchema(ma.ModelSchema):
+ class Meta:
+ model = ContainerRev
+ include_fk = True
+
+class ContainerSchema(EntitySchema):
+ class Meta:
+ model = ContainerIdent
+ include_fk = True
+ rev = ma.Nested(ContainerRevSchema)
+
+class ContainerEditSchema(ma.ModelSchema):
+ class Meta:
+ model = ContainerEdit
+
+container_rev_schema = ContainerRevSchema()
+container_schema = ContainerSchema()
+container_edit_schema = ContainerEditSchema()
+
+
+class FileRevSchema(ma.ModelSchema):
+ class Meta:
+ model = FileRev
+ include_fk = True
+
+ releases = ma.Nested(FileReleaseSchema, many=True)
+
+class FileSchema(EntitySchema):
+ class Meta:
+ model = FileIdent
+ include_fk = True
+ rev = ma.Nested(FileRevSchema)
+
+class FileEditSchema(ma.ModelSchema):
+ class Meta:
+ model = FileEdit
+
+file_rev_schema = FileRevSchema()
+file_schema = FileSchema()
+file_edit_schema = FileEditSchema()
+
+
+class EditorSchema(ma.ModelSchema):
+ class Meta:
+ model = Editor
+
+class EditGroupSchema(ma.ModelSchema):
+ class Meta:
+ model = EditGroup
+ editor = ma.Nested(EditorSchema)
+
+editor_schema = EditorSchema()
+editgroup_schema = EditGroupSchema()
+
+class ChangelogEntrySchema(ma.ModelSchema):
+ class Meta:
+ model = ChangelogEntry
+
+changelogentry_schema = ChangelogEntrySchema()
diff --git a/python/fatcat/routes.py b/python/fatcat/routes.py
new file mode 100644
index 00000000..0c86bd78
--- /dev/null
+++ b/python/fatcat/routes.py
@@ -0,0 +1,129 @@
+
+import os
+import json
+from flask import Flask, render_template, send_from_directory, request, \
+ url_for, abort, g, redirect, jsonify, session
+from fatcat import app, db, api
+
+
+### Views ###################################################################
+
+@app.route('/work/create', methods=['GET'])
+def work_create():
+ return render_template('work_add.html')
+
+@app.route('/work/random', methods=['GET'])
+def work_random():
+ rv = api.api_work_random()
+ ident = rv.location.split('/')[-1]
+ return redirect("/work/{}".format(ident))
+
+@app.route('/work/<int:ident>', methods=['GET'])
+def work_view(ident):
+ rv = api.api_work_get(ident)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('work_view.html', work=entity)
+
+@app.route('/release/<int:ident>', methods=['GET'])
+def release_view(ident):
+ rv = api.api_release_get(ident)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('release_view.html', release=entity)
+
+@app.route('/release/<int:ident>/changelog', methods=['GET'])
+def release_changelog(ident):
+ rv = api.api_release_get(ident)
+ release = json.loads(rv.data.decode('utf-8'))
+ rv = api.api_release_changelog(ident)
+ changelog_entries = json.loads(rv.data.decode('utf-8'))
+ return render_template('release_changelog.html', release=release,
+ changelog_entries=changelog_entries)
+
+@app.route('/release/random', methods=['GET'])
+def release_random():
+ rv = api.api_release_random()
+ ident = rv.location.split('/')[-1]
+ return redirect("/release/{}".format(ident))
+
+@app.route('/container/create', methods=['GET'])
+def container_create_view():
+ return render_template('container_add.html')
+
+@app.route('/container/create', methods=['POST'])
+def container_create():
+ params = dict()
+ for k in request.form:
+ if k.startswith('container_'):
+ params[k[10:]] = request.form[k]
+ rv = api.api_container_create(params=params)
+ container = json.loads(rv.data.decode('utf-8'))
+ return redirect("/container/{}".format(container['id']))
+
+@app.route('/creator/<int:ident>', methods=['GET'])
+def creator_view(ident):
+ rv = api.api_creator_get(ident)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('creator_view.html', creator=entity)
+
+@app.route('/container/<int:ident>', methods=['GET'])
+def container_view(ident):
+ rv = api.api_container_get(ident)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('container_view.html', container=entity)
+
+@app.route('/file/<int:ident>', methods=['GET'])
+def file_view(ident):
+ rv = api.api_file_get(ident)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('file_view.html', file=entity)
+
+@app.route('/editgroup/<int:ident>', methods=['GET'])
+def editgroup_view(ident):
+ rv = api.api_editgroup_get(ident)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('editgroup_view.html', editgroup=entity)
+
+@app.route('/editgroup/current', methods=['GET'])
+def editgroup_current():
+ eg = api.get_or_create_editgroup()
+ return redirect('/editgroup/{}'.format(eg.id))
+
+@app.route('/editor/<username>', methods=['GET'])
+def editor_view(username):
+ rv = api.api_editor_get(username)
+ entity = json.loads(rv.data.decode('utf-8'))
+ return render_template('editor_view.html', editor=entity)
+
+@app.route('/editor/<username>/changelog', methods=['GET'])
+def editor_changelog(username):
+ rv = api.api_editor_get(username)
+ editor = json.loads(rv.data.decode('utf-8'))
+ rv = api.api_editor_changelog(username)
+ changelog_entries = json.loads(rv.data.decode('utf-8'))
+ return render_template('editor_changelog.html', editor=editor,
+ changelog_entries=changelog_entries)
+
+
+### Static Routes ###########################################################
+
+@app.errorhandler(404)
+def page_not_found(e):
+ return render_template('404.html'), 404
+
+@app.route('/', methods=['GET'])
+def homepage():
+ return render_template('home.html')
+
+@app.route('/about', methods=['GET'])
+def aboutpage():
+ return render_template('about.html')
+
+@app.route('/robots.txt', methods=['GET'])
+def robots():
+ return send_from_directory(os.path.join(app.root_path, 'static'),
+ 'robots.txt',
+ mimetype='text/plain')
+
+@app.route('/health', methods=['GET'])
+def health():
+ return jsonify({'ok': True})
diff --git a/python/fatcat/sql.py b/python/fatcat/sql.py
new file mode 100644
index 00000000..9b1922ba
--- /dev/null
+++ b/python/fatcat/sql.py
@@ -0,0 +1,150 @@
+
+import json
+import time
+import random
+import hashlib
+from sqlalchemy.orm.session import make_transient
+from fatcat import db
+import fatcat.api
+from fatcat.models import *
+
+def populate_db():
+ admin_editor = Editor(id=1, username="admin", is_admin=True)
+ db.session.add(admin_editor)
+ db.session.commit()
+
+def add_crossref_via_model(meta):
+
+ title = meta['title'][0]
+
+ # authors
+ author_revs = []
+ author_ids = []
+ for am in meta['author']:
+ ar = CreatorRev(
+ name="{} {}".format(am['given'], am['family']),
+ sortname="{}, {}".format(am['family'], am['given']),
+ orcid=None)
+ author_revs.append(ar)
+ author_ids.append(CreatorIdent(rev=ar))
+
+ # container
+ container = ContainerRev(
+ issn=meta['ISSN'][0],
+ name=meta['container-title'][0],
+ #container_id=None,
+ publisher=meta['publisher'],
+ sortname=meta['short-container-title'][0])
+ container_id = ContainerIdent(rev=container)
+
+ # work and release
+ work = WorkRev(title=title)
+ work_id = WorkIdent(rev=work)
+ release = ReleaseRev(
+ title=title,
+ creators=[ReleaseContrib(creator=a) for a in author_ids],
+ # XXX: work=work,
+ container=container_id,
+ release_type=meta['type'],
+ doi=meta['DOI'],
+ date=meta['created']['date-time'],
+ license=meta.get('license', [dict(URL=None)])[0]['URL'] or None,
+ issue=meta.get('issue', None),
+ volume=meta.get('volume', None),
+ pages=meta.get('page', None))
+ release_id = ReleaseIdent(rev=release)
+ work.primary_release = release_id
+ release.extra_json = json.dumps({
+ 'crossref': {
+ 'links': meta.get('link', []),
+ 'subject': meta['subject'],
+ 'type': meta['type'],
+ 'alternative-id': meta.get('alternative-id', []),
+ }
+ }, indent=None).encode('utf-8')
+
+ # references
+ for i, rm in enumerate(meta.get('reference', [])):
+ ref = ReleaseRef(
+ release_rev=release,
+ doi=rm.get("DOI", None),
+ index=i+1,
+ # TODO: how to generate a proper stub here from k/v metadata?
+ stub="| ".join(rm.values()))
+ release.refs.append(ref)
+
+ db.session.add_all([work, work_id, release, release_id, container,
+ container_id])
+ db.session.add_all(author_revs)
+ db.session.add_all(author_ids)
+ db.session.commit()
+
+def accept_editgroup(eg):
+
+ # check if already accepted
+ # XXX: add a test for this
+ assert ChangelogEntry.query.filter(ChangelogEntry.editgroup_id==eg.id).count() == 0
+
+ # start transaction (TODO: explicitly?)
+
+ # for each entity type:
+ for cls in (WorkEdit, ReleaseEdit, CreatorEdit, ContainerEdit, FileEdit):
+ edits = cls.query.filter(cls.editgroup_id==eg.id).all()
+ # for each entity edit->ident:
+ for edit in edits:
+ # update entity ident state (activate, redirect, delete)
+ edit.ident.is_live = True
+ edit.ident.rev_id = edit.rev_id
+ edit.ident.redirect_id = edit.redirect_id
+ db.session.add(edit.ident)
+
+ # append log/changelog row
+ cle = ChangelogEntry(
+ editgroup_id=eg.id,
+ # TODO: is this UTC?
+ timestamp=int(time.time()))
+ db.session.add(cle)
+
+ # update edit group state
+ db.session.add(eg)
+
+ # no longer "active"
+ eg.editor.active_editgroup = None
+ db.session.add(eg.editor)
+
+ db.session.commit()
+
+def merge_works(left_id, right_id, editgroup=None):
+ """Helper to merge two works together."""
+ left = WorkIdent.query.get_or_404(left_id)
+ right = WorkIdent.query.get_or_404(right_id)
+ assert left.is_live and right.is_live
+ assert left.rev and right.rev
+ assert (left.redirect_id is None) and (right.redirect_id is None)
+
+ if editgroup is None:
+ editgroup = fatcat.api.get_or_create_editgroup()
+
+ releases = ReleaseIdent.query\
+ .join(ReleaseIdent.rev)\
+ .filter(ReleaseRev.work_ident_id==right_id)\
+ .filter(ReleaseIdent.is_live==True)\
+ .all()
+
+ # update all right releases to point to left
+ for release_ident in releases:
+ rev = release_ident.rev
+ old_id = rev.id
+ db.session.expunge(rev)
+ make_transient(rev)
+ rev.id = None
+ rev.parent = old_id
+ rev.work_ident_id = left.id
+ re = ReleaseEdit(editgroup=editgroup, ident=release_ident, rev=rev)
+ db.session.add_all([rev, re])
+
+ # redirect right id to left (via editgroup)
+ neww = WorkEdit(editgroup=editgroup, ident=right,
+ rev=left.rev, redirect_id=left.id)
+
+ db.session.add_all([neww])
diff --git a/python/fatcat/static/robots.txt b/python/fatcat/static/robots.txt
new file mode 100644
index 00000000..a168f11b
--- /dev/null
+++ b/python/fatcat/static/robots.txt
@@ -0,0 +1 @@
+# Hello friends!
diff --git a/python/fatcat/templates/404.html b/python/fatcat/templates/404.html
new file mode 100644
index 00000000..c8fbfeac
--- /dev/null
+++ b/python/fatcat/templates/404.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>404: Not Found</h1>
+
+{% endblock %}
diff --git a/python/fatcat/templates/about.html b/python/fatcat/templates/about.html
new file mode 100644
index 00000000..ce194099
--- /dev/null
+++ b/python/fatcat/templates/about.html
@@ -0,0 +1,161 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>About fatcat!</h1>
+
+<p>fatcat is a half-baked idea to build an open, independent, collaboratively editable bibliographic database of most written works, with a focus on published research outputs like journal articles, pre-prints, and conference proceedings.</p>
+<h2 id="technical-architecture">Technical Architecture</h2>
+<p>The canonical backend datastore would be a very large transactional SQL server. A relatively simple and stable back-end daemon would expose an API (could be REST, GraphQL, gRPC, etc). As little &quot;application logic&quot; as possible would be embedded in this back-end; as much as possible would be pushed to bots which could be authored and operated by anybody. A separate web interface project would talk to the API backend and could be developed more rapidly.</p>
+<p>A cronjob would make periodic database dumps, both in &quot;full&quot; form (all tables and all edit history, removing only authentication credentials) and &quot;flat&quot; form (with only the most recent version of each entity, using only persistent IDs between entities).</p>
+<p>A goal is to be linked-data/RDF/JSON-LD/semantic-web &quot;compatible&quot;, but not necessarily &quot;first&quot;. It should be possible to export the database in a relatively clean RDF form, and to fetch data in a variety of formats, but internally fatcat would not be backed by a triple-store, and would not be bound to a specific third party ontology or schema.</p>
+<p>Microservice daemons should be able to proxy between the primary API and standard protocols like ResourceSync and OAI-PMH, and bots could consume external databases in those formats.</p>
+<h2 id="licensing">Licensing</h2>
+<p>The core fatcat database should only contain verifyable factual statements (which isn't to say that all statements are &quot;true&quot;), not creative or derived content.</p>
+<p>The goal is to have a very permissively licensed database: CC-0 (no rights reserved) if possible. Under US law, it should be possible to scrape and pull in factual data from other corpuses without adopting their licenses. The goal here isn't to avoid all attibution (progeny information will be included, and a large sources and acknowledgements statement should be maintained), but trying to manage the intersection of all upstream source licenses seems untenable, and creates burdens for downstream users.</p>
+<p>Special care will need to be taken around copyright and original works. I would propose either not accepting abstracts at all, or including them in a partitioned database to prevent copyright contamination. Likewise, even simple user-created content like lists, reviews, ratings, comments, discussion, documentation, etc should go in separate services.</p>
+<h2 id="basic-editing-workflow-and-bots">Basic Editing Workflow and Bots</h2>
+<p>Both human editors and bots would have edits go through the same API, with humans using either the default web interface or arbitrary integrations or client software.</p>
+<p>The usual workflow would be to create edits (or creations, merges, deletions) to individual entities one at a time, all under a single &quot;edit group&quot; of related edits (eg, correcting authorship info for multiple works related to a single author). When ready, the editor would &quot;submit&quot; the edit group for review. During the review period, humans could vote (or veto/approve if they have higher permissions), and bots can perform automated checks. During this period the editor can make tweaks if necessary. After some fixed time period (72 hours?) with no changes and no blocking issues, the edit group would be auto-accepted, if no auto-resolvable merge-conflicts have arisen. This process balances editing labor (reviews are easy, but optional) against quality (cool-down period makes it easier to detect and prevent spam or out-of-control bots). Advanced permissions could allow some trusted human and bot editors to push through edits more rapidly.</p>
+<p>Bots would need to be tuned to have appropriate edit group sizes (eg, daily batches, instead of millions of works in a single edit) to make human QA and reverts possible.</p>
+<p>Data progeny and citation would be left to the edit history. In the case of importing external databases, the expectation would be that special-purpose bot accounts would be used. Human editors would leave edit messages to clarify their sources.</p>
+<p>A style guide (wiki), chat room, and discussion forum would be hosted as separate stand-alone services for editors to propose projects and debate process or scope changes. It would be best if these could use federated account authorization (oauth?) to have consistent account IDs across mediums.</p>
+<h2 id="edit-log">Edit Log</h2>
+<p>As part of the process of &quot;accepting&quot; an edit group, a row would be written to an immutable, append-only log table (which internally could be a SQL table) documenting each identifier change. This log establishes a monotonically increasing version number for the entire corpus, and should make interaction with other systems easier (eg, search engines, replicated databases, alternative storage backends, notification frameworks, etc).</p>
+<h2 id="itentifiers">Itentifiers</h2>
+<p>A fixed number of first class &quot;entities&quot; would be definied, with common behavior and schema layouts. These would all be semantic entities like &quot;work&quot;, &quot;release&quot;, &quot;container&quot;, and &quot;person&quot;.</p>
+<p>fatcat identifiers would be semanticly meaningless fixed length random numbers, usually represented in case-insensitive base32 format. Each entity type would have it's own identifier namespace. Eg, 96 bit identifiers would have 20 characters and look like:</p>
+<pre><code>fcwork_rzga5b9cd7efgh04iljk
+https://fatcat.org/work/rzga5b9cd7efgh04iljk</code></pre>
+<p>128-bit (UUID size) would have 26 characters:</p>
+<pre><code>fcwork_rzga5b9cd7efgh04iljk8f3jvz
+https://fatcat.org/work/rzga5b9cd7efgh04iljk8f3jvz</code></pre>
+<p>A 64 bit namespace is probably plenty though, and would work with most databse Integer columns:</p>
+<pre><code>fcwork_rzga5b9cd7efg
+https://fatcat.org/work/rzga5b9cd7efg</code></pre>
+<p>The idea would be to only have fatcat identifiers be used to interlink between databases, <em>not</em> to supplant DOIs, ISBNs, handle, ARKs, and other &quot;registered&quot; persistant identifiers.</p>
+<h2 id="entities-and-internal-schema">Entities and Internal Schema</h2>
+<p>Internally, identifiers would be lightweight pointers to actual metadata objects, which can be thought of as &quot;versions&quot;. The metadata objects themselves would be immutable once commited; the edit process is one of creating new objects and, if the edit is approved, pointing the identifier to the new version. Entities would reference between themselves by identifier.</p>
+<p>Edit objects represent a change to a single entity; edits get batched together into edit groups (like &quot;commits&quot; and &quot;pull requests&quot; in git parlance).</p>
+<p>SQL tables would probably look something like the following, though be specific to each entity type (eg, there would be an actual <code>work_revision</code> table, but not an actual <code>entity_revision</code> table):</p>
+<pre><code>entity_id
+ uuid
+ current_revision
+
+entity_revision
+ entity_id (bi-directional?)
+ previous: entity_revision or none
+ state: normal, redirect, deletion
+ redirect_entity_id: optional
+ extra: json blob
+ edit_id
+
+edit
+ mutable: boolean
+ edit_group
+ editor
+
+edit_group</code></pre>
+<p>Additional type-specific columns would hold actual metadata. Additional tables (which would reference both <code>entity_revision</code> and <code>entity_id</code> foreign keys as appropriate) would represent things like external identifiers, ordered author/work relationships, citations between works, etc. Every revision of an entity would require duplicating all of these associated rows, which could end up being a large source of inefficiency, but is necessary to represent the full history of an object.</p>
+<h2 id="scope">Scope</h2>
+<p>Want the &quot;scholarly web&quot;: the graph of works that cite other works. Certainly every work that is cited more than once and every work that both cites and is cited; &quot;leaf nodes&quot; and small islands might not be in scope.</p>
+<p>Focusing on written works, with some exceptions. Expect core media (for which we would pursue &quot;completeness&quot;) to be:</p>
+<pre><code>journal articles
+books
+conference proceedings
+technical memos
+dissertations</code></pre>
+<p>Probably in scope:</p>
+<pre><code>reports
+magazine articles
+published poetry
+essays
+government documents
+conference
+presentations (slides, video)
+datasets</code></pre>
+<p>Probably not:</p>
+<pre><code>patents
+court cases and legal documents
+manuals
+datasheets
+courses</code></pre>
+<p>Definitely not:</p>
+<pre><code>audio recordings
+tv show episodes
+musical scores
+advertisements</code></pre>
+<p>Author, citation, and work disambiguation would be core tasks. Linking pre-prints to final publication is in scope.</p>
+<p>I'm much less interested in altmetrics, funding, and grant relationships than most existing databases in this space.</p>
+<p>fatcat would not include any fulltext content itself, even for cleanly licensed (open access) works, but would have &quot;strong&quot; (verified) links to fulltext content, and would include file-level metadata (like hashes and fingerprints) to help discovery and identify content from any source. Typed file-level links should make fatcat more useful for both humans and machines to quickly access fulltext content of a given mimetype than existing redirect or landing page systems.</p>
+<h2 id="ontology">Ontology</h2>
+<p>Loosely following FRBR, but removing the &quot;manifestation&quot; abstraction, and favoring files (digital artifacts) over physical items, the primary entities are:</p>
+<pre><code>work
+ type
+ &lt;has&gt; contributors
+ &lt;about&gt; subject/category
+ &lt;has-primary&gt; release
+
+release (aka &quot;edition&quot;, &quot;variant&quot;)
+ title
+ volume/pages/issue/chapter
+ open-access status
+ &lt;published&gt; date
+ &lt;of a&gt; work
+ &lt;published-by&gt; publisher
+ &lt;published in&gt; container
+ &lt;has&gt; contributors
+ &lt;citation&gt; citetext &lt;to&gt; release
+ &lt;has&gt; identifier
+
+file (aka &quot;digital artifact&quot;)
+ &lt;of a&gt; release
+ &lt;has&gt; hashes
+ &lt;found at&gt; URLs
+ &lt;held-at&gt; institution &lt;with&gt; accession
+
+contributor
+ name
+ &lt;has&gt; aliases
+ &lt;has&gt; affiliation &lt;for&gt; date span
+ &lt;has&gt; identifier
+
+container
+ name
+ open-access policy
+ peer-review policy
+ &lt;has&gt; aliases, acronyms
+ &lt;about&gt; subject/category
+ &lt;has&gt; identifier
+ &lt;published in&gt; container
+ &lt;published-by&gt; publisher
+
+publisher
+ name
+ &lt;has&gt; aliases, acronyms
+ &lt;has&gt; identifier</code></pre>
+<h2 id="controlled-vocabularies">Controlled Vocabularies</h2>
+<p>Some special namespace tables and enums would probably be helpful; these should live in the database (not requiring a database migration to update), but should have more controlled editing workflow... perhaps versioned in the codebase:</p>
+<ul>
+<li>identifier namespaces (DOI, ISBN, ISSN, ORCID, etc)</li>
+<li>subject categorization</li>
+<li>license and open access status</li>
+<li>work &quot;types&quot; (article vs. book chapter vs. proceeding, etc)</li>
+<li>contributor types (author, translator, illustrator, etc)</li>
+<li>human languages</li>
+<li>file mimetypes</li>
+</ul>
+<h2 id="unresolved-questions">Unresolved Questions</h2>
+<p>How to handle translations of, eg, titles and author names? To be clear, not translations of works (which are just separate releases).</p>
+<p>Are bi-directional links a schema anti-pattern? Eg, should &quot;work&quot; point to a primary &quot;release&quot; (which itself points back to the work), or should &quot;release&quot; have a &quot;is-primary&quot; flag?</p>
+<p>Should <code>identifier</code> and <code>citation</code> be their own entities, referencing other entities by UUID instead of by revision? This could save a ton of database space and chunder.</p>
+<p>Should contributor/author contact information be retained? It could be very useful for disambiguation, but we don't want to build a huge database for spammers or &quot;innovative&quot; start-up marketing.</p>
+<p>Would general purpose SQL databases like Postgres or MySQL scale well enough told hold several tables with billions of entries? Right from the start there are hundreds of millions of works and releases, many of which having dozens of citations, many authors, and many identifiers, and then we'll have potentially dozens of edits for each of these, which multiply out to <code>1e8 * 2e1 * 2e1 = 4e10</code>, or 40 billion rows in the citation table. If each row was 32 bytes on average (uncompressed, not including index size), that would be 1.3 TByte on it's own, larger than common SSD disk. I think a transactional SQL datastore is the right answer. In my experience locking and index rebuild times are usually the biggest scaling challenges; the largely-immutable architecture here should mitigate locking. Hopefully few indexes would be needed in the primary database, as user interfaces could rely on secondary read-only search engines for more complex queries and views.</p>
+<p>I see a tension between focus and scope creep. If a central database like fatcat doesn't support enough fields and metadata, then it will not be possible to completely import other corpuses, and this becomes &quot;yet another&quot; partial bibliographic database. On the other hand, accepting arbitrary data leads to other problems: sparseness increases (we have more &quot;partial&quot; data), potential for redundancy is high, humans will start editing content that might be bulk-replaced, etc.</p>
+<p>There might be a need to support &quot;stub&quot; references between entities. Eg, when adding citations from PDF extraction, the cited works are likely to be ambiguous. Could create &quot;stub&quot; works to be merged/resolved later, or could leave the citation hanging. Same with authors, containers (journals), etc.</p>
+<h2 id="references-and-previous-work">References and Previous Work</h2>
+<p>The closest overall analog of fatcat is <a href="https://musicbrainz.org">MusicBrainz</a>, a collaboratively edited music database. <a href="https://openlibrary.org">Open Library</a> is a very similar existing service, which exclusively contains book metadata.</p>
+<p><a href="https://wikidata.org">Wikidata</a> seems to be the most successful and actively edited/developed open bibliographic database at this time (early 2018), including the <a href="https://meta.wikimedia.org/wiki/WikiCite_2017">wikicite</a> conference and related Wikimedia/Wikipedia projects. Wikidata is a general purpose semantic database of entities, facts, and relationships; bibliographic metadata has become a large fraction of all content in recent years. The focus there seems to be linking knowledge (statements) to specific sources unambigiously. Potential advantages fatcat would have would be a focus on a specific scope (not a general purpose database of entities) and a goal of completeness (capturing as many works and relationships as rapidly as possible). However, it might be better to just pitch in to the wikidata efforts.</p>
+<p>The technical design of fatcat is loosely inspired by the git branch/tag/commit/tree architecture, and specifically inspired by Oliver Charles' &quot;New Edit System&quot; <a href="https://ocharles.org.uk/blog/posts/2012-07-10-nes-does-it-better-1.html">blog posts</a> from 2012.</p>
+<p>There are a whole bunch of proprietary, for-profit bibliographic databases, including Web of Science, Google Scholar, Microsoft Academic Graph, aminer, Scopus, and Dimensions. There are excellent field-limited databases like dblp, MEDLINE, and Semantic Scholar. There are some large general-purpose databases that are not directly user-editable, including the OpenCitation corpus, CORE, BASE, and CrossRef. I don't know of any large (more than 60 million works), open (bulk-downloadable with permissive or no license), field agnostic, user-editable corpus of scholarly publication bibliographic metadata.</p>
+
+{% endblock %}
diff --git a/python/fatcat/templates/base.html b/python/fatcat/templates/base.html
new file mode 100644
index 00000000..697705c3
--- /dev/null
+++ b/python/fatcat/templates/base.html
@@ -0,0 +1,70 @@
+<!DOCTYPE html>
+<html lang="en" style="position: relative; min-height: 100%; height: auto;">
+<head>
+ <meta charset="utf-8" />
+ <meta name="viewport" content="width=device-width">
+
+ <title>{% block title %}fatcat!{% endblock %}</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.2.13/dist/semantic.min.css">
+ <script
+ src="https://code.jquery.com/jquery-3.1.1.min.js"
+ integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
+ crossorigin="anonymous"></script>
+ <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.2.13/dist/semantic.min.js"></script>
+
+</head>
+<body style="margin-bottom: 100px; height: auto;">
+
+<header class="ui fixed inverted menu">
+ <div class="ui container">
+ <a href="/" class="header item">
+ <!-- <img class="logo" src="assets/images/logo.png"> -->
+ fatcat!
+ </a>
+ <a href="/about" class="item">About</a>
+ <a href="#" class="item">Guide</a>
+ <div class="right menu">
+ <div class="item">
+ <div class="ui transparent inverted icon input">
+ <i class="search icon"></i>
+ <input type="text" placeholder="Search...">
+ </div>
+ </div>
+ <div class="ui simple dropdown item">
+ acidburn <i class="dropdown icon"></i>
+ <div class="menu">
+ <a class="item" href="/editgroup/current">Open Submissions</a>
+ <a class="item" href="/editor/admin/changelog">Edit History</a>
+ <div class="divider"></div>
+ <a class="item" href="/editor/admin">Account</a>
+ <a class="item" href="/logout">Logout</a>
+ </div>
+ </div>
+
+ </div>
+ </div>
+</header>
+
+<main class="ui main text container" style="margin-top: 4em; margin-bottom: 2em;">
+{% block body %}Nothing to see here.{% endblock %}
+</main>
+
+
+<footer class="ui inverted vertical footer segment" style="margin-top: 2em; padding-top: 2em; padding-bottom:2em; position: absolute; bottom: 0px; width: 100%;">
+ <div class="ui center aligned container">
+ <div class="ui horizontal inverted small divided link list">
+ <span class="item">fatcat!</span>
+ <a class="item" href="/about">About</a>
+ <a class="item" href="#">Sources</a>
+ <a class="item" href="#">Status</a>
+ <a class="item" href="#">Datasets</a>
+ <a class="item" href="https://git.bnewbold.net/fatcat/">Source Code</a>
+ </div>
+ </div>
+</footer>
+
+{% block postscript %}{% endblock %}
+
+</body>
+</html>
diff --git a/python/fatcat/templates/container_add.html b/python/fatcat/templates/container_add.html
new file mode 100644
index 00000000..15288142
--- /dev/null
+++ b/python/fatcat/templates/container_add.html
@@ -0,0 +1,168 @@
+{% extends "base.html" %}
+{% block body %}
+<div class="ui segment">
+<h1 class="ui header">Adding a New Container</h1>
+
+<p>A "container" is a anything that groups publications together. For example,
+a journal (eg, "New England Journal of Medicine"), conference proceedings, a
+book series, or a blog.
+
+<p>Not all publications are in a container.
+
+<form class="ui form" id="add_container_form" method="post" action="/container/create">
+
+ <h3 class="ui dividing header">The Basics</h3>
+
+ <div class="ui huge field required">
+ <label>Name or Title</label>
+ <input name="container_name" type="text" placeholder="Title of Container (in English)">
+ </div>
+
+ <div class="ui field required">
+ <label>Type of Container</label>
+ <select class="ui dropdown" id="container_type">
+ <option value="">Primary Type</option>
+ <option value="journal">Journal</option>
+ <option value="book-series">Book Series</option>
+ <option value="conference">Conference Proceedings</option>
+ <option value="blog">Blog</option>
+ <option value="other">Other</option>
+ </select>
+ </div>
+
+ <!-- Publisher -->
+ <div class="ui huge field required">
+ <label>Name of Publisher</label>
+ <input name="container_publisher" type="text" placeholder="Name of Publisher">
+ </div>
+
+ <!-- Identifier -->
+ <div class="ui huge field required">
+ <label>ISSN Number</label>
+ <input name="container_issn" type="text" placeholder="eg, 1234-567X">
+ </div>
+
+ <!-- Primary/Original Language -->
+ <div class="field">
+ <label>Primary Language</label>
+ <select class="ui search select dropdown" id="language-select">
+ <option value="">Select if Appropriate</option>
+ <option value="en">English</option>
+ <option value="es">Spanish</option>
+ <option value="">Russian</option>
+ <option value="">Thai</option>
+ <option value="">Indonesian</option>
+ <option value="">Chinese</option>
+ </select>
+ </div>
+
+ <!-- Subject / Categorization / Tags -->
+ <div class="field">
+ <label>Subject</label>
+ <select multiple="" class="ui dropdown" id="subjects">
+ <option value="">Select Subject/Tags</option>
+ <option value="AF">Natural Sciences</option>
+ <option value="AX">Humanities</option>
+ <option value="AL">Arts</option>
+ <option value="AL">Engineering</option>
+ <option value="AL">Other</option>
+ </select>
+ </div>
+
+ <!-- Date -->
+ <!-- Container / Part-Of -->
+ <!-- Region -->
+
+ <!-- Anything Else? -->
+ <h3 class="ui dividing header">Anything Else?</h3>
+
+<div class="ui submit button">Create container</div>
+
+<p><i>Entity will be created as part of the current edit group, which needs to be
+submited and approved before the entity will formally be included in the
+catalog.</i>
+
+</form>
+
+</div>
+{% endblock %}
+
+{% block postscript %}
+<script>
+<!-- Form validation code -->
+$(document).ready(function() {
+
+ $('#add_container_form')
+ .form({
+ fields: {
+ name: {
+ identifier: 'name',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please enter your name'
+ }
+ ]
+ },
+ skills: {
+ identifier: 'skills',
+ rules: [
+ {
+ type : 'minCount[2]',
+ prompt : 'Please select at least two skills'
+ }
+ ]
+ },
+ gender: {
+ identifier: 'gender',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please select a gender'
+ }
+ ]
+ },
+ username: {
+ identifier: 'username',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please enter a username'
+ }
+ ]
+ },
+ password: {
+ identifier: 'password',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please enter a password'
+ },
+ {
+ type : 'minLength[6]',
+ prompt : 'Your password must be at least {ruleValue} characters'
+ }
+ ]
+ },
+ terms: {
+ identifier: 'terms',
+ rules: [
+ {
+ type : 'checked',
+ prompt : 'You must agree to the terms and conditions'
+ }
+ ]
+ }
+ }
+ })
+ ;
+
+ $('#container_type').dropdown();
+ $('#subjects').dropdown();
+ $('#language-select').dropdown();
+
+ console.log("Page loaded");
+
+});
+</script>
+{% endblock %}
diff --git a/python/fatcat/templates/container_view.html b/python/fatcat/templates/container_view.html
new file mode 100644
index 00000000..483886b5
--- /dev/null
+++ b/python/fatcat/templates/container_view.html
@@ -0,0 +1,14 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Container: {{ container.name }}</h1>
+
+<p>ID: {{ container.id }}
+<p>ISSN: {{ container.issn }}
+<p>Publisher: {{ container.publisher }}
+
+<p>TODO:
+
+<pre>{{ container }}</pre>
+
+{% endblock %}
diff --git a/python/fatcat/templates/creator_view.html b/python/fatcat/templates/creator_view.html
new file mode 100644
index 00000000..f7be9f2c
--- /dev/null
+++ b/python/fatcat/templates/creator_view.html
@@ -0,0 +1,10 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Creator: {{ creator.id }}</h1>
+
+TODO:
+
+<pre>{{ creator }}</pre>
+
+{% endblock %}
diff --git a/python/fatcat/templates/editgroup_view.html b/python/fatcat/templates/editgroup_view.html
new file mode 100644
index 00000000..4ed08501
--- /dev/null
+++ b/python/fatcat/templates/editgroup_view.html
@@ -0,0 +1,49 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Edit Group: #{{ editgroup.id}}</h1>
+
+<p>Editor: <a href="/editor/{{ editgroup.editor.username }}">{{ editgroup.editor.username }}</a>
+<p>Description: {{ editgroup.description }}
+
+<h3>Work Edits ({{ editgroup.work_edits|count }})</h3>
+<ul>
+{% for edit in editgroup.work_edits %}
+ <li><a href="/work/edit/{{ edit.id }}">Edit #{{ edit.id }}</a>:
+ <a href="/work/{{ edit.ident }}">{{ edit.ident }}</a> to rev {{ edit.rev }}
+{% endfor %}
+</ul>
+
+<h3>Release Edits ({{ editgroup.release_edits|count }})</h3>
+<ul>
+{% for edit in editgroup.release_edits %}
+ <li><a href="/release/edit/{{ edit.id }}">Edit #{{ edit.id }}</a>
+ <a href="/release/{{ edit.ident }}">{{ edit.ident }}</a> to rev {{ edit.rev }}
+{% endfor %}
+</ul>
+
+<h3>Container Edits ({{ editgroup.container_edits|count }})</h3>
+<ul>
+{% for edit in editgroup.container_edits %}
+ <li><a href="/container/edit/{{ edit.id }}">Edit #{{ edit.id }}</a>
+ <a href="/container/{{ edit.ident }}">{{ edit.ident }}</a> to rev {{ edit.rev }}
+{% endfor %}
+</ul>
+
+<h3>Creator Edits ({{ editgroup.creator_edits|count }})</h3>
+<ul>
+{% for edit in editgroup.creator_edits %}
+ <li><a href="/creator/edit/{{ edit.id }}">Edit #{{ edit.id }}</a>
+ <a href="/creator/{{ edit.ident }}">{{ edit.ident }}</a> to rev {{ edit.rev }}
+{% endfor %}
+</ul>
+
+<h3>File Edits ({{ editgroup.file_edits|count }})</h3>
+<ul>
+{% for edit in editgroup.file_edits %}
+ <li><a href="/file/edit/{{ edit.id }}">Edit #{{ edit.id }}</a>
+ <a href="/file/{{ edit.ident }}">{{ edit.ident }}</a> to rev {{ edit.rev }}
+{% endfor %}
+</ul>
+
+{% endblock %}
diff --git a/python/fatcat/templates/editor_changelog.html b/python/fatcat/templates/editor_changelog.html
new file mode 100644
index 00000000..e1410874
--- /dev/null
+++ b/python/fatcat/templates/editor_changelog.html
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Editor Changelog: {{ editor.username }}</h1>
+
+<p>Editor: <a href="/editor/{{ editor.username }}">{{ editor.username }}</a>
+
+<p>Changes accepted (aka, merged editgroups):
+<ul>
+{% for entry in changelog_entries %}
+ <li><a href="/editgroup/{{ entry.editgroup }}">Edit Group #{{ entry.editgroup }}</a> (on {{ entry.timestamp }})
+{% else %}
+NONE
+{% endfor %}
+</ul>
+
+{% endblock %}
diff --git a/python/fatcat/templates/editor_view.html b/python/fatcat/templates/editor_view.html
new file mode 100644
index 00000000..e0625c42
--- /dev/null
+++ b/python/fatcat/templates/editor_view.html
@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Editor: {{ editor.username }}</h1>
+
+<p>Is Admin? {{ editor.is_admin }}
+<p><a href="/editor/{{ editor.username }}/changelog">Changelog</a>
+
+{% endblock %}
diff --git a/python/fatcat/templates/file_view.html b/python/fatcat/templates/file_view.html
new file mode 100644
index 00000000..ff55e21c
--- /dev/null
+++ b/python/fatcat/templates/file_view.html
@@ -0,0 +1,10 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>File: {{ file.id }}</h1>
+
+TODO:
+
+<pre>{{ file }}</pre>
+
+{% endblock %}
diff --git a/python/fatcat/templates/home.html b/python/fatcat/templates/home.html
new file mode 100644
index 00000000..cea4f687
--- /dev/null
+++ b/python/fatcat/templates/home.html
@@ -0,0 +1,29 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Salutations!</h1>
+
+Just mockups for now...
+
+<ul>
+ <li><b>Work:</b>
+ <a href="/work/create">Create</a>,
+ <a href="/work/random">Random</a>
+ <li><b>Release:</b>
+ <a href="/release/create">Create</a>,
+ <a href="/release/random">Random</a>
+ <li><b><strike>File:</strike></b>
+ <a href="/file/create">Create</a>,
+ <a href="/file/random">Random</a>
+ <li><b><strike>Contributor:</strike></b>
+ <a href="/contrib/create">Create</a>,
+ <a href="/contrib/random">Random</a>
+ <li><b><strike>Container:</strike></b>
+ <a href="/container/create">Create</a>,
+ <a href="/container/random">Random</a>
+ <li>Edit groups...
+ <li>Changelog...
+ <li>Login/Signup...
+</ul>
+
+{% endblock %}
diff --git a/python/fatcat/templates/release_changelog.html b/python/fatcat/templates/release_changelog.html
new file mode 100644
index 00000000..706a5642
--- /dev/null
+++ b/python/fatcat/templates/release_changelog.html
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>Release Changelog: {{ release.id }}</h1>
+
+<p>release: <a href="/release/{{ release.id }}">{{ release.id }}</a>
+
+<p>Changelog:
+<ul>
+{% for entry in changelog_entries %}
+ <li><a href="/editgroup/{{ entry.editgroup }}">Edit Group #{{ entry.editgroup }}</a> (on {{ entry.timestamp }})
+{% else %}
+NONE
+{% endfor %}
+</ul>
+
+{% endblock %}
diff --git a/python/fatcat/templates/release_view.html b/python/fatcat/templates/release_view.html
new file mode 100644
index 00000000..ee68161c
--- /dev/null
+++ b/python/fatcat/templates/release_view.html
@@ -0,0 +1,31 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>{{ release.title }}</h1>
+
+<p>Release type: {{ release.type }}
+<p><a href="/release/{{ release.id }}/history">History</a>
+<p>Contributors:
+{% for c in release.contributors %} {{ c.name }}; {% endfor %}
+
+<p>Title: {{ release.title }}
+<p>Date: {{ release.date }}
+
+{% if release.container %}
+<p>Container: <a href="/container/{{ release.container.id }}">{{ release.container.title }}</a>
+{% endif %}
+
+{% if release.doi %}
+<p>DOI: <a href="https://dx.doi.org/{{ release.doi }}">{{ release.doi }}</a>
+{% endif %}
+
+{% if releases %}
+<ul>
+{% for r in releases %}
+ <ul><a href="/release/{{ r.id }}">{{ r.title }}</a> ({{ y.date }} - {{ y.release_type }})
+{% endfor %}
+</ul>
+{% else %}
+{% endif %}
+
+{% endblock %}
diff --git a/python/fatcat/templates/work_add.html b/python/fatcat/templates/work_add.html
new file mode 100644
index 00000000..ac8a8169
--- /dev/null
+++ b/python/fatcat/templates/work_add.html
@@ -0,0 +1,215 @@
+{% extends "base.html" %}
+{% block body %}
+<div class="ui segment">
+<h1 class="ui header">Adding a New Thing</h1>
+
+<form class="ui form" id="add_work_form">
+
+ <h3 class="ui dividing header">The Basics</h3>
+
+ <div class="ui huge field required">
+ <label>Title</label>
+ <input name="work_title" type="text" placeholder="Title of Work (in English)">
+ </div>
+
+ <div class="ui field required">
+ <label>Type of Work</label>
+ <select class="ui dropdown" id="work_type">
+ <option value="">Primary Type</option>
+ <option value="journal-article">Journal Article</option>
+ <option value="book">Book</option>
+ <option value="book-chapter">Book Chapter</option>
+ <option value="dataset">Dataset</option>
+ <option value="dissertation">Thesis or Dissertation</option>
+ <option value="monograph">Monograph</option>
+ <option value="proceedings-article">Conference Proceeding</option>
+ <option value="report">Report</option>
+ <option value="other">Other</option>
+ </select>
+ </div>
+
+ <!-- Primary Creators/Authors -->
+ <div class="ui field search" id="work_creators">
+ <label>Primary Creator(s)</label>
+ <div class="ui icon input">
+ <input class="prompt" type="text" placeholder="Search...">
+ <i class="search icon"></i>
+ </div>
+ <div class="results"></div>
+ </div>
+
+ <!-- Description (not an abstract) -->
+ <div class="ui field">
+ <label>Description</label>
+ <div class="field">
+ <label>Not an abstract...</label>
+ <textarea rows="2"></textarea>
+ </div>
+ </div>
+
+ <!-- Primary/Original Language -->
+ <div class="field">
+ <label>Primary Language</label>
+ <select class="ui search select dropdown" id="language-select">
+ <option value="">Select if Appropriate</option>
+ <option value="en">English</option>
+ <option value="es">Spanish</option>
+ </select>
+ </div>
+
+ <!-- Subject / Categorization / Tags -->
+ <div class="field">
+ <label>Subject</label>
+ <select multiple="" class="ui dropdown" id="subjects">
+ <option value="">Select Subject/Tags</option>
+ <option value="AF">Afghanistan</option>
+ <option value="AX">Åland Islands</option>
+ <option value="AL">Albania</option>
+ <option value="DZ">Algeria</option>
+ <option value="AS">American Samoa</option>
+ <option value="AD">Andorra</option>
+ <option value="AO">Angola</option>
+ </select>
+ </div>
+
+
+ <h3 class="ui dividing header">Primary Release / Edition</h3>
+
+ <!-- Contributors (and how) -->
+ <div class="ui field search" id="release_creators">
+ <label>Primary Creator(s)</label>
+ <div class="ui icon input">
+ <input class="prompt" type="text" placeholder="Search...">
+ <i class="search icon"></i>
+ </div>
+ <div class="results"></div>
+ </div>
+
+ <!-- Date -->
+ <!-- Container / Part-Of -->
+ <!-- Publisher -->
+ <!-- Identifier -->
+ <!-- Language -->
+ <!-- Type / Media -->
+ <!-- Issue / Volume / Pages / Chapter -->
+
+ <!-- Anything Else? -->
+ <h3 class="ui dividing header">Anything Else?</h3>
+
+ <!-- File / Copy / URL -->
+ <!-- Citations -->
+
+<div class="ui submit button">Create Work</div>
+</form>
+
+</div>
+{% endblock %}
+
+{% block postscript %}
+<script>
+<!-- Form validation code -->
+$(document).ready(function() {
+
+ $('#add_work_form')
+ .form({
+ fields: {
+ name: {
+ identifier: 'name',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please enter your name'
+ }
+ ]
+ },
+ skills: {
+ identifier: 'skills',
+ rules: [
+ {
+ type : 'minCount[2]',
+ prompt : 'Please select at least two skills'
+ }
+ ]
+ },
+ gender: {
+ identifier: 'gender',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please select a gender'
+ }
+ ]
+ },
+ username: {
+ identifier: 'username',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please enter a username'
+ }
+ ]
+ },
+ password: {
+ identifier: 'password',
+ rules: [
+ {
+ type : 'empty',
+ prompt : 'Please enter a password'
+ },
+ {
+ type : 'minLength[6]',
+ prompt : 'Your password must be at least {ruleValue} characters'
+ }
+ ]
+ },
+ terms: {
+ identifier: 'terms',
+ rules: [
+ {
+ type : 'checked',
+ prompt : 'You must agree to the terms and conditions'
+ }
+ ]
+ }
+ }
+ })
+ ;
+
+ var example_authors = [
+ { title: 'Andorra' },
+ { title: 'United Arab Emirates' },
+ { title: 'Afghanistan' },
+ { title: 'Antigua' },
+ { title: 'Anguilla' },
+ { title: 'Albania' },
+ { title: 'Armenia' },
+ { title: 'Netherlands Antilles' },
+ { title: 'Angola' },
+ { title: 'Argentina' },
+ { title: 'American Samoa' },
+ { title: 'Austria' },
+ { title: 'Australia' },
+ { title: 'Aruba' },
+ ];
+
+ $('#work_creators')
+ .search({
+ source: example_authors
+ })
+ ;
+
+ $('#release_creators')
+ .search({
+ source: example_authors
+ })
+ ;
+
+ $('#work_type').dropdown();
+ $('#subjects').dropdown();
+ $('#language-select').dropdown();
+
+ console.log("Page loaded");
+
+});
+</script>
+{% endblock %}
diff --git a/python/fatcat/templates/work_view.html b/python/fatcat/templates/work_view.html
new file mode 100644
index 00000000..8c5e955d
--- /dev/null
+++ b/python/fatcat/templates/work_view.html
@@ -0,0 +1,37 @@
+{% extends "base.html" %}
+{% block body %}
+
+<h1>{{ work.title }}</h1>
+
+<p>Work type: {{ work.type }}
+<p><a href="/work/{{ work.id }}/history">History</a>
+<p>Contributors:
+{% for c in work.contributors %} {{ c.name }}; {% endfor %}
+
+{% if primary %}
+<h2>Primary Release/Edition</h2>
+<p>Title: {{ primary.title }}
+<p>Date: {{ primary.date }}
+
+{% if primary.container %}
+<p>Container: <a href="/container/{{ primary.container.id }}">{{ primary.container.title }}</a>
+{% endif %}
+
+{% if primary.doi %}
+<p>DOI: <a href="https://dx.doi.org/{{ primary.doi }}">{{ primary.doi }}</a>
+{% endif %}
+
+{% else %}
+<p>No primary release
+{% endif %}
+
+{% if releases %}
+<ul>
+{% for r in releases %}
+ <ul><a href="/release/{{ r.id }}">{{ r.title }}</a> ({{ y.date }} - {{ y.release_type }})
+{% endfor %}
+</ul>
+{% else %}
+{% endif %}
+
+{% endblock %}