class Archive(db.Model): __tablename__ = "archives" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), unique=True) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) boxes = db.relationship("Box", backref="archive") def __init__(self, name, notes=""): db.Model.__init__(self) self.name = name self.notes = notes @staticmethod def from_dict(data, archive=None): data = data.get("archive") if not is_ok(data): raise TypeError("Missing data: archive") if not is_ok(data.get("name")): raise TypeError("Missing data: archive.name") if (Archive.query.filter_by(name=data["name"]).first() or len(data["name"]) >= 128): raise TypeError("Invalid data: archive.name") return Archive(name=data["name"], notes=data.get("notes", "")) def from_form(self, form): self.name = form.get("name", self.name) self.notes = form.get("notes", self.notes) def get_form(self, form=None): rv = ArchiveForm() if not form else form rv.name.data = self.name rv.notes = self.notes return rv def get_url(self, param=None, full=False): return url_for("kron.get_archive", id=self.id, param=param, _external=full) def __repr__(self): return "<Archive {id}>".format(id=self.id)
class Topic(db.Model): __tablename__ = "topics" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), unique=True) dates = db.Column(db.UnicodeText) citations = db.Column(db.UnicodeText) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) def __init__(self, name, dates=None, citations=None, notes=None): db.Model.__init__(self) self.name = name self.dates = dates self.citations = citations self.notes = notes @staticmethod def from_dict(data): data = data.get("topic") if not is_ok(data): raise TypeError("Missing data: topic") if not is_ok(data.get("name")): raise TypeError("Missing data: topic.name") return Topic(name=data["name"], dates=data.get("dates"), citations=data.get("citations"), notes=data.get("notes")) def get_url(self, full=False): return url_for("kron.get_topic", id=self.id, _external=full) def __repr__(self): return "<Topic {id}>".format(id=self.id)
class Tag(db.Model): __tablename__ = "tags" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) def __init__(self, name): db.Model.__init__(self) self.name = name @staticmethod def from_dict(data): data = data.get("tag") if not is_ok(data): raise TypeError("Missing data: tag") if not is_ok(data.get("name")): raise TypeError("Missing data: tag.name") return Tag(name=data["name"]) def update_from_dict(self, data): data = data.get("tag") if not is_ok(data): raise TypeError("Missing data: tag") if is_ok(data.get("name")): self.name == data["name"] def to_dict(self): rv = dict(tag=dict(id=self.id, name=self.name, posts=[dict(url=p.get_url()) for p in self.posts])) for key in list(rv["tag"]): if not is_ok(rv["tag"][key]): rv["tag"].pop(key, None) return rv def get_url(self, full=False): return url_for("blog.get_tag", id=self.id, _external=full) def __repr__(self): return "<Tag {id}>".format(id=self.id)
class Document(db.Model): __tablename__ = "documents" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), unique=True) dates = db.Column(db.UnicodeText) citations = db.Column(db.UnicodeText) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) box_id = db.Column(db.Integer, db.ForeignKey("boxes.id")) authors = db.relationship("Person", secondary=documents_authors, backref=db.backref("documents_by", lazy="dynamic")) people = db.relationship("Person", secondary=documents_people, backref=db.backref("documents_in", lazy="dynamic")) topics = db.relationship("Topic", secondary=documents_topics, backref="documents") def __init__(self, title, dates=None, citations=None, notes=None): db.Model.__init__(self) self.title = title self.dates = dates self.citations = citations self.notes = notes @staticmethod def from_dict(data): data = data.get("document") if not is_ok(data): raise TypeError("Missing data: document") if not is_ok(data.get("title")): raise TypeError("Missing data: document.title") if (Document.query.filter_by(title=data["title"]).first() or len(data["title"]) >= 128): raise TypeError("Invalid data: document.title") return Document(title=data["title"], dates=data.get("dates"), citations=data.get("citations"), notes=data.get("notes")) def get_url(self, full=False): return url_for("kron.get_document", id=self.id, _external=full) def __repr__(self): return "<Document {id}>".format(id=self.id)
class Person(db.Model): __tablename__ = "people" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), unique=True) dates = db.Column(db.UnicodeText) citations = db.Column(db.UnicodeText) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) topics = db.relationship("Topic", secondary=people_topics, backref="people") def __init__(self, name, dates=None, citations=None, notes=None): db.Model.__init__(self) self.name = name self.dates = dates self.citations = citations self.notes = notes @staticmethod def from_dict(data): data = data.get("person") if not is_ok(data): raise TypeError("Missing data: person") if not is_ok(data.get("name")): raise TypeError("Missing data: person.name") if (Person.query.filter_by(name=data["name"]).first() or len(data["name"]) >= 128): raise TypeError("Invalid data: person.name") return Person(name=data["name"], dates=data.get("dates"), citations=data.get("citations"), notes=data.get("notes")) def get_url(self, full=False): return url_for("kron.get_person", id=self.id, _external=full) def __repr__(self): return "<Person {id}>".format(id=self.id)
class Box(db.Model): __tablename__ = "boxes" id = db.Column(db.Integer, primary_key=True) number = db.Column(db.Integer, unique=True) dates = db.Column(db.UnicodeText) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) archive_id = db.Column(db.Integer, db.ForeignKey("archives.id")) documents = db.relationship("Document", backref="box") people = db.relationship("Person", secondary=boxes_people, backref="boxes") topics = db.relationship("Topic", secondary=boxes_topics, backref="boxes") def __init__(self, number, dates=None, notes=None): db.Model.__init__(self) self.number = number self.dates = dates self.notes = notes @staticmethod def from_dict(data): data = data.get("box") if not is_ok(data): raise TypeError("Missing data: box") if not is_ok(data.get("number")): raise TypeError("Missing data: box.number") if Box.query.filter_by(number=data["number"]).first(): raise TypeError("Invalid data: box.number") return Box(number=data["number"], dates=data.get("dates"), notes=data.get("notes")) def get_url(self, full=False): return url_for("kron.get_box", id=self.id, _external=full) def __repr__(self): return "<Box {id}>".format(id=self.id)
from datetime import datetime from flask import url_for from kron import db, is_ok, ModelEventListeners boxes_people = db.Table( "boxes_people", db.Column("box_id", db.Integer, db.ForeignKey("boxes.id")), db.Column("person_id", db.Integer, db.ForeignKey("people.id"))) boxes_topics = db.Table( "boxes_topics", db.Column("box_id", db.Integer, db.ForeignKey("boxes.id")), db.Column("topic_id", db.Integer, db.ForeignKey("topics.id"))) class Box(db.Model): __tablename__ = "boxes" id = db.Column(db.Integer, primary_key=True) number = db.Column(db.Integer, unique=True) dates = db.Column(db.UnicodeText) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) archive_id = db.Column(db.Integer, db.ForeignKey("archives.id")) documents = db.relationship("Document", backref="box") people = db.relationship("Person", secondary=boxes_people, backref="boxes") topics = db.relationship("Topic", secondary=boxes_topics, backref="boxes") def __init__(self, number, dates=None, notes=None): db.Model.__init__(self) self.number = number self.dates = dates
from datetime import datetime from flask import url_for from kron import db, is_ok, ModelEventListeners documents_authors = db.Table( "documents_authors", db.Column("document_id", db.Integer, db.ForeignKey("documents.id")), db.Column("person_id", db.Integer, db.ForeignKey("people.id"))) documents_people = db.Table( "documents_people", db.Column("document_id", db.Integer, db.ForeignKey("documents.id")), db.Column("person_id", db.Integer, db.ForeignKey("people.id"))) documents_topics = db.Table( "documents_topics", db.Column("document_id", db.Integer, db.ForeignKey("documents.id")), db.Column("topic_id", db.Integer, db.ForeignKey("topics.id"))) class Document(db.Model): __tablename__ = "documents" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), unique=True) dates = db.Column(db.UnicodeText) citations = db.Column(db.UnicodeText) notes = db.Column(db.UnicodeText) last_update = db.Column(db.DateTime) box_id = db.Column(db.Integer, db.ForeignKey("boxes.id"))
class Post(db.Model): __tablename__ = "posts" id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime) title = db.Column(db.String(128), unique=True, index=True) body = db.Column(db.UnicodeText) tags = db.relationship("Tag", secondary=tags_posts, backref="posts") def __init__(self, title, body): db.Model.__init__(self) self.timestamp = datetime.utcnow() self.title = title self.body = body @staticmethod def from_dict(data): data = data["post"] if not is_ok(data): raise TypeError("Missing data: post") if not is_ok(data.get("title")): raise TypeError("Missing data: post.title") if not is_ok(data.get("body")): raise TypeError("Missing data: post.body") return Post(title=data["title"], body=data["body"]) def update_from_dict(self, data): data = data["post"] if not is_ok(data): raise TypeError("Missing data: post") if is_ok(data.get("title")): self.title = data["title"] if is_ok(data.get("body")): self.body = data["body"] def to_dict(self): rv = dict(post=dict(id=self.id, title=self.title, body=self.body, html=self.html, timestamp=datetime.strftime( self.timestamp, '%b %d %Y %I:%M%p'), tags=[dict(url=t.get_url()) for t in self.tags])) for key in list(rv["post"]): if not is_ok(rv["post"][key]): rv["post"].pop(key, None) return rv def get_html(self): allowed_tags = [ "a", "abbr", "acronym", "b", "blockquote", "code", "em", "i", "li", "ol", "pre", "strong", "ul", "h1", "h2", "h3", "p" ] html = bleach.clean(markdown(self.body, output_format="html"), tags=allowed_tags, strip=True) return bleach.linkify(html) def get_url(self, full=False): return url_for("blog.get_post", id=self.id, _external=full) def __repr__(self): return "<Post {id}>".format(id=self.id)
from datetime import datetime from flask import url_for import bleach from markdown import markdown from kron import db, is_ok, ModelEventListeners tags_posts = db.Table( "tags_posts", db.Column("tag_id", db.Integer, db.ForeignKey("tags.id")), db.Column("post_id", db.Integer, db.ForeignKey("posts.id"))) class Tag(db.Model): __tablename__ = "tags" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) def __init__(self, name): db.Model.__init__(self) self.name = name @staticmethod def from_dict(data): data = data.get("tag") if not is_ok(data): raise TypeError("Missing data: tag") if not is_ok(data.get("name")): raise TypeError("Missing data: tag.name") return Tag(name=data["name"])