Skip to content

Instantly share code, notes, and snippets.

@mazz
Created May 20, 2017 04:46
Show Gist options
  • Select an option

  • Save mazz/291f3daf78253f8e57ecc52ef6c95344 to your computer and use it in GitHub Desktop.

Select an option

Save mazz/291f3daf78253f8e57ecc52ef6c95344 to your computer and use it in GitHub Desktop.
import datetime
from uuid import uuid4
from sqlalchemy import Column, Unicode, String, Integer, ForeignKey, UnicodeText
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from websauna.system.model.meta import Base
from websauna.system.model.columns import UTCDateTime
from websauna.utils.time import now
class Book(Base):
#: The table in the database
__tablename__ = "book"
#: Database primary key for the row (running counter)
id = Column(Integer, autoincrement=True, primary_key=True)
#: Publicly exposed non-guessable
uuid = Column(UUID(as_uuid=True), default=uuid4)
#: book of Bible in base language -- English
basename = Column(Unicode(64), default=None)
#: Relationship mapping between book and chapter.
#: Each chapter can have only one book.
#: Deleting book deletes its chapters.
chapters = relationship("Chapter",
back_populates="book",
lazy="dynamic",
cascade="all, delete-orphan",
single_parent=True)
def __repr__(self):
return "#{}: {}".format(self.basename, self.uuid)
def __str__(self):
"""Python default and admin UI string presentation."""
return self.basename
class Chapter(Base):
#: The table in the database
__tablename__ = "chapter"
#: Database primary key for the row (running counter)
id = Column(Integer, autoincrement=True, primary_key=True)
#: Publicly exposed non-guessable id
uuid = Column(UUID(as_uuid=True), default=uuid4)
#: chapter of Bible in base language -- English
basename = Column(Unicode(64), default=None)
#: Which book this chapter is part of
book_id = Column(Integer, ForeignKey('book.id'))
book = relationship("Book", back_populates="chapters")
#: Relationship mapping between media and chapter.
#: Each media can have only one chapter.
#: Deleting chapter deletes its media.
media = relationship("Media",
back_populates="chapter",
lazy="dynamic",
cascade="all, delete-orphan",
single_parent=True)
def __repr__(self):
"""Shell and debugger presentation."""
return "{} {}".format(self.book.basename, self.id)
def __str__(self):
"""Python default and admin UI string presentation."""
if hasattr(self.book, 'basename'):
return "{} {}".format(self.book.basename, self.id)
else:
return "{} {}".format('(not set)', self.id)
class Media(Base):
#: The table in the database
__tablename__ = "media"
#: Database primary key for the row (running counter)
id = Column(Integer, autoincrement=True, primary_key=True)
#: Publicly exposed non-guessable
uuid = Column(UUID(as_uuid=True), default=uuid4)
#: chapter of Bible in localized language
localizedname = Column(Unicode(128), default=None)
#: url
url = Column(Unicode(384), default=None)
# full iso language-locale identifier i.e. zh-Hans-US
language_id = Column(String(16), default=None)
#: Which question this choice is part of
chapter_id = Column(Integer, ForeignKey('chapter.id'))
chapter = relationship("Chapter", back_populates="media")
def __repr__(self):
"""Shell and debugger presentation."""
return '{} ({}) {} <{}>'.format(self.localizedname, self.language_id, str(self.uuid), self.url)
def __str__(self):
"""Python default and admin UI string presentation."""
return '{} ({}) {} <{}>'.format(self.localizedname, self.language_id, str(self.uuid), self.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment