Initial commit.
This commit is contained in:
commit
4e5ee195dd
10 changed files with 372 additions and 0 deletions
28
confarchive/__init__.py
Normal file
28
confarchive/__init__.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
durations = [
|
||||
"5 seconds",
|
||||
"25 seconds",
|
||||
"2 minutes",
|
||||
"10 minutes",
|
||||
"1 hour",
|
||||
"5 hours",
|
||||
"1 day",
|
||||
"5 days",
|
||||
"25 days",
|
||||
"4 months",
|
||||
]
|
||||
|
||||
|
||||
def rd(label):
|
||||
num, _, unit = label.partition(" ")
|
||||
if not unit.endswith("s"):
|
||||
unit += "s"
|
||||
return relativedelta(**{unit: int(num)})
|
||||
|
||||
|
||||
bins = (
|
||||
[{"label": None, "delta": None}]
|
||||
+ [{"label": label, "delta": rd(label)} for label in durations]
|
||||
+ [{"label": "Never", "delta": None}]
|
||||
)
|
||||
30
confarchive/database.py
Normal file
30
confarchive/database.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import flask
|
||||
import sqlalchemy
|
||||
from sqlalchemy import create_engine, func
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
session = scoped_session(sessionmaker())
|
||||
|
||||
|
||||
def init_db(db_url: str, echo: bool = False) -> None:
|
||||
"""Initialise databsae."""
|
||||
session.configure(bind=get_engine(db_url, echo=echo))
|
||||
|
||||
|
||||
def get_engine(db_url: str, echo: bool = False) -> sqlalchemy.engine.base.Engine:
|
||||
"""Create an engine object."""
|
||||
return create_engine(db_url, pool_recycle=3600, echo=echo)
|
||||
|
||||
|
||||
def init_app(app: flask.app.Flask, echo: bool = False) -> None:
|
||||
"""Initialise database connection within flask app."""
|
||||
db_url = app.config["DB_URL"]
|
||||
session.configure(bind=get_engine(db_url, echo=echo))
|
||||
|
||||
@app.teardown_appcontext
|
||||
def shutdown_session(exception: Exception | None = None) -> None:
|
||||
session.remove()
|
||||
|
||||
|
||||
def now_utc():
|
||||
return func.timezone("utc", func.now())
|
||||
101
confarchive/model.py
Normal file
101
confarchive/model.py
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
"""Database models."""
|
||||
|
||||
import sqlalchemy
|
||||
import sqlalchemy.orm.decl_api
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.orderinglist import ordering_list
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.schema import Column, ForeignKey
|
||||
from sqlalchemy.types import Date, DateTime, Integer, String
|
||||
|
||||
from .database import session
|
||||
|
||||
Base: sqlalchemy.orm.decl_api.DeclarativeMeta = declarative_base()
|
||||
Base.query = session.query_property()
|
||||
|
||||
|
||||
class TimeStampedModel(Base):
|
||||
"""Time stamped model."""
|
||||
|
||||
__abstract__ = True
|
||||
created = Column(DateTime, default=func.now())
|
||||
modified = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
|
||||
class Conference(TimeStampedModel):
|
||||
"""Conference."""
|
||||
|
||||
__tablename__ = "conference"
|
||||
id = Column(Integer, primary_key=True)
|
||||
title = Column(String, nullable=False)
|
||||
start = Column(Date)
|
||||
end = Column(Date)
|
||||
days = Column(Integer)
|
||||
timezone = Column(String)
|
||||
location = Column(String)
|
||||
country = Column(String)
|
||||
acronym = Column(String)
|
||||
url = Column(String)
|
||||
schedule_xml_url = Column(String)
|
||||
|
||||
|
||||
class Event(TimeStampedModel):
|
||||
"""Event."""
|
||||
|
||||
__tablename__ = "event"
|
||||
id = Column(Integer, primary_key=True)
|
||||
conference_id = Column(Integer, ForeignKey("conference.id"), nullable=False)
|
||||
event_date = Column(DateTime)
|
||||
day = Column(Integer)
|
||||
guid = Column(String)
|
||||
start = Column(String)
|
||||
duration = Column(String)
|
||||
room = Column(String)
|
||||
track = Column(String)
|
||||
slug = Column(String)
|
||||
title = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
event_type = Column(String)
|
||||
url = Column(String)
|
||||
|
||||
conference = relationship("Conference", backref="events")
|
||||
|
||||
people_association = relationship(
|
||||
"EventPerson",
|
||||
order_by="EventPerson.position",
|
||||
back_populates="event",
|
||||
collection_class=ordering_list("position"),
|
||||
)
|
||||
people = association_proxy(
|
||||
"people_association",
|
||||
"person",
|
||||
creator=lambda person: EventPerson(person=person),
|
||||
)
|
||||
|
||||
|
||||
class Person(TimeStampedModel):
|
||||
"""Person."""
|
||||
|
||||
__tablename__ = "person"
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String)
|
||||
wikidata_qid = Column(String)
|
||||
gender = Column(String)
|
||||
|
||||
events_association = relationship("EventPerson", back_populates="person")
|
||||
events = association_proxy("event_association", "event")
|
||||
|
||||
|
||||
class EventPerson(Base):
|
||||
"""Event person."""
|
||||
|
||||
__tablename__ = "event_person"
|
||||
event_id = Column(Integer, ForeignKey("event.id"), primary_key=True)
|
||||
person_id = Column(Integer, ForeignKey("person.id"), primary_key=True)
|
||||
position = Column(Integer, nullable=False)
|
||||
named_as = Column(String)
|
||||
|
||||
person = relationship("Person", back_populates="events_association")
|
||||
event = relationship("Event", back_populates="people_association")
|
||||
Loading…
Add table
Add a link
Reference in a new issue