Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
38 lines (31 loc) · 1.54 KB
/
Copy pathmodels.py
File metadata and controls
38 lines (31 loc) · 1.54 KB
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
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
db = SQLAlchemy()
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, nullable=False)
password_hash = db.Column(db.String(128))
files = db.relationship('File', backref='owner', lazy='dynamic')
messages = db.relationship('Message', backref='author', lazy='dynamic')
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(256), nullable=False)
filepath = db.Column(db.String(256), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
upload_time = db.Column(db.DateTime, default=datetime.utcnow)
# Names only have to be unique per user, so two users can both send
# "report.pdf" without blocking (or overwriting) each other.
__table_args__ = (
db.UniqueConstraint('user_id', 'filename', name='uq_file_user_filename'),
)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))