This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
70 lines (57 loc) · 2.63 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from os import listdir, path
from app import db, login_manager
from datetime import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
class UserAccount(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
display_name = db.Column(db.String(100), unique=True, nullable=False)
username = db.Column(db.String(30), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128))
works = db.relationship("Document", back_populates="author")
def __init__(self, dn, un, email, password):
self.display_name = dn
self.username = un
self.email = email
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def __repr__(self):
return "<User %r>" % self.username
class Document(db.Model):
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey('user_account.id'))
author = db.relationship("UserAccount", back_populates="works")
title = db.Column(db.String, unique=False, nullable=False)
date_published = db.Column(db.DateTime, unique=False, nullable=False)
path = db.Column(db.String, unique=True, nullable=False)
versions = db.relationship("Version", back_populates="document")
def __init__(self, author_id, title, path):
self.author_id = author_id
self.author = load_user(author_id)
self.title = title
self.path = path
self.date_published = datetime.utcnow()
def latest(self):
"""
Returns path to latest version
[::-1] reverses the sorted list of versions
[:1] removes all elements but the first
[0] extracts the first element
"""
return sorted(self.versions, key=lambda v: v.upload_date)[::-1][:1][0]
class Version(db.Model):
id = db.Column(db.Integer, primary_key=True)
document_id = db.Column(db.Integer, db.ForeignKey('document.id'), nullable=False)
document = db.relationship("Document", back_populates="versions")
upload_date = db.Column(db.DateTime, unique=False, nullable=False)
path = db.Column(db.String, unique=True, nullable=False)
def __init__(self, document_id, path, upload_date=datetime.utcnow()):
self.document_id = document_id
self.document = Document.query.get(document_id)
self.upload_date = upload_date
self.path = path
@login_manager.user_loader
def load_user(id):
return UserAccount.query.get(int(id))