Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
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)