conference-archive/confarchive/model.py

159 lines
4.6 KiB
Python
Raw Normal View History

2023-09-13 11:49:08 +01:00
"""Database models."""
import sqlalchemy
import sqlalchemy.orm.decl_api
from sqlalchemy import func
2023-09-15 19:04:41 +01:00
from sqlalchemy.dialects import postgresql
2023-09-13 11:49:08 +01:00
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)
2023-09-15 19:04:41 +01:00
short_name = Column(String, unique=True)
people_detail = relationship(
"ConferencePerson", lazy="dynamic", back_populates="conference"
)
people = association_proxy("people_detail", "person")
events = relationship(
"Event",
order_by="Event.event_date",
back_populates="conference",
lazy="dynamic",
)
class ConferencePerson(Base):
__tablename__ = "conference_person"
conference_id = Column(Integer, ForeignKey("conference.id"), primary_key=True)
person_id = Column(Integer, ForeignKey("person.id"), primary_key=True)
named_as = Column(String)
bio = Column(String)
slug = Column(String)
url = Column(String)
person = relationship("Person", back_populates="conferences_association")
conference = relationship("Conference", back_populates="people_detail")
2023-09-13 11:49:08 +01:00
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)
2023-09-15 19:04:41 +01:00
# day = Column(Integer)
2023-09-13 11:49:08 +01:00
guid = Column(String)
start = Column(String)
duration = Column(String)
room = Column(String)
track = Column(String)
slug = Column(String)
title = Column(String, nullable=False)
2023-09-15 19:04:41 +01:00
abstract = Column(String)
2023-09-13 11:49:08 +01:00
description = Column(String)
event_type = Column(String)
url = Column(String)
2023-09-15 19:04:41 +01:00
conference = relationship("Conference", back_populates="events")
2023-09-13 11:49:08 +01:00
2023-09-15 19:04:41 +01:00
people_detail = relationship(
2023-09-13 11:49:08 +01:00
"EventPerson",
order_by="EventPerson.position",
back_populates="event",
collection_class=ordering_list("position"),
)
people = association_proxy(
2023-09-15 19:04:41 +01:00
"people_detail",
2023-09-13 11:49:08 +01:00
"person",
2023-09-15 19:04:41 +01:00
creator=lambda i: EventPerson(person=i[0], named_as=i[1]),
2023-09-13 11:49:08 +01:00
)
class Person(TimeStampedModel):
"""Person."""
__tablename__ = "person"
id = Column(Integer, primary_key=True)
name = Column(String)
wikidata_qid = Column(String)
gender = Column(String)
2023-09-15 19:04:41 +01:00
wikidata_photo = Column(postgresql.ARRAY(String))
events_association = relationship(
"EventPerson",
back_populates="person",
lazy="dynamic",
)
events = association_proxy("events_association", "event")
conferences_association = relationship("ConferencePerson", back_populates="person")
conferences = association_proxy("conference_association", "conference")
2023-09-13 11:49:08 +01:00
2023-09-15 19:04:41 +01:00
# photos = relationship("PersonPhoto", back_populates="person")
def events_by_time(self):
q = (
session.query(Event)
.join(EventPerson)
.filter(EventPerson.person == self)
.order_by(Event.event_date.desc())
)
return q
# class PersonPhoto(TimeStampedModel):
# """Person photo."""
#
# __tablename__ = "person_photo"
# person_id = Column(Integer, ForeignKey("person.id"), primary_key=True)
# source_filename = Column(String)
#
# person = relationship("Person", back_populates="photos")
2023-09-13 11:49:08 +01:00
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)
person = relationship("Person", back_populates="events_association")
2023-09-15 19:04:41 +01:00
event = relationship("Event", back_populates="people_detail")