2015-12-24 20:34:34 +01:00
|
|
|
"""
|
|
|
|
This file contains the database schema in SQLAlchemy format.
|
|
|
|
"""
|
2015-12-25 20:09:56 +01:00
|
|
|
from sqlalchemy import event
|
|
|
|
from sqlalchemy import Column, ForeignKey, Integer, String
|
2015-12-25 01:43:49 +01:00
|
|
|
from sqlalchemy.engine import Engine
|
2015-12-24 20:34:34 +01:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2015-12-25 20:09:56 +01:00
|
|
|
from sqlalchemy.orm import relationship as sqlalchemy_relationship
|
2015-12-24 20:34:34 +01:00
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
2015-12-25 01:43:49 +01:00
|
|
|
@event.listens_for(Engine, "connect")
|
|
|
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
|
|
|
"""
|
|
|
|
Auto enable foreign keys for SQLite.
|
|
|
|
"""
|
|
|
|
cursor = dbapi_connection.cursor()
|
|
|
|
cursor.execute("PRAGMA foreign_keys=ON")
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
2015-12-25 20:09:56 +01:00
|
|
|
# TODO: Backref
|
|
|
|
class Association(Base):
|
|
|
|
# Relationships are to be read "left RELATION right"
|
|
|
|
__tablename__ = "association"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
left_id = Column(Integer, ForeignKey("papers.id", ondelete="CASCADE"))
|
|
|
|
right_id = Column(Integer, ForeignKey("papers.id", ondelete="CASCADE"))
|
|
|
|
relationship_id = Column(Integer,
|
|
|
|
ForeignKey("relationships.id",
|
|
|
|
ondelete="CASCADE"))
|
|
|
|
right_paper = sqlalchemy_relationship("Paper", foreign_keys=right_id)
|
|
|
|
relationship = sqlalchemy_relationship("Relationship")
|
|
|
|
|
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
class Paper(Base):
|
2015-12-25 20:09:56 +01:00
|
|
|
__tablename__ = "papers"
|
2015-12-24 20:34:34 +01:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
doi = Column(String(), nullable=True, unique=True)
|
|
|
|
arxiv_id = Column(String(25), nullable=True, unique=True)
|
2015-12-25 20:09:56 +01:00
|
|
|
related = sqlalchemy_relationship("Association",
|
|
|
|
foreign_keys="Association.left_id")
|
2015-12-24 20:34:34 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Paper(id='%d', doi='%s', arxiv_id='%s')>" % (
|
|
|
|
self.id,
|
|
|
|
self.doi,
|
|
|
|
self.arxiv_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
def json_api_repr(self):
|
|
|
|
"""
|
|
|
|
Dict to dump for the JSON API.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
"types": self.__tablename__,
|
|
|
|
"id": self.id,
|
|
|
|
"attributes": {
|
|
|
|
"doi": self.doi,
|
|
|
|
"arxiv_id": self.arxiv_id,
|
|
|
|
},
|
|
|
|
"links": {
|
|
|
|
"self": "/papers/%d" % (self.id,)
|
|
|
|
},
|
|
|
|
"relationships": {
|
|
|
|
# TODO
|
|
|
|
}
|
|
|
|
}
|
2015-12-25 20:09:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Relationship(Base):
|
|
|
|
__tablename__ = "relationships"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String(), unique=True)
|
|
|
|
associations = sqlalchemy_relationship("Association",
|
|
|
|
back_populates="relationship")
|