-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
116 lines (92 loc) · 3.79 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from sqlalchemy.ext.hybrid import hybrid_property
from flask_login import UserMixin
import time
from app_manager import db, bcrypt, serializer
users = db.Table("users",
db.Column("user_id", db.Integer, db.ForeignKey("user.id"),
primary_key=True),
db.Column("nom_id", db.Integer, db.ForeignKey("nomination.id"),
primary_key=True)
)
def default_email(context):
# Andrew email should be used, but it could be different
if context is None:
# flask_admin's create interface will pass None for context
return "[email protected]"
return context.get_current_parameters()["username"] + "@andrew.cmu.edu"
def default_password():
# decode string to avoid invalid salt error with postgres
return bcrypt.generate_password_hash("password").decode("utf-8")
def default_token():
return int(time.time())
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(16), unique=True, nullable=False)
_password = db.Column(db.String(128), nullable=False,
default=default_password)
email = db.Column(db.String(128), nullable=False, default=default_email)
email_confirmed = db.Column(db.Boolean, default=False, nullable=False)
sessTokenTime = db.Column(db.Integer, nullable=False, default=default_token)
banned = db.Column(db.Boolean, default=False, nullable=False)
is_admin = db.Column(db.Boolean, default=False, nullable=False)
@hybrid_property
def password(self):
return self._password
@password.setter
def password(self, plaintext):
self._password = \
bcrypt.generate_password_hash(plaintext).decode("utf-8")
self.reset_token() # reset the token when you change the password
def is_correct_password(self, plaintext):
return bcrypt.check_password_hash(self._password, plaintext)
def reset_token(self):
self.sessTokenTime = int(time.time())
def ban(self):
self.banned = True
self.reset_token() # invalidate their sessions
def unban(self):
self.banned = False
def give_admin(self):
self.is_admin = True
def take_admin(self):
self.is_admin = False
# Flask-Login properties
@property
def is_authenticated(self):
return True
@property
def is_active(self):
return not self.banned and self.email_confirmed
def get_id(self):
return serializer.dumps([self.id, self.sessTokenTime], salt="session")
def __repr__(self):
return "<User %r>" % self.username
class Award(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
nominations = db.relationship("Nomination", backref="award")
order = db.Column(db.Integer, nullable=False, default=1000)
def __repr__(self):
return "<Award %r>" % self.name
class Nomination(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), nullable=False)
award_id = db.Column(db.Integer, db.ForeignKey("award.id"), nullable=False)
creator = db.relationship("User", backref="entries")
creator_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
voters = db.relationship("User", secondary=users,
backref=db.backref("selections"))
@property
def votes(self):
return len(self.voters)
def __repr__(self):
return "<Nomination %r>" % self.id
class State(db.Model):
id = db.Column(db.Integer, primary_key=True)
# 0 is static, 1 is nominations, 2 is voting
phase = db.Column(db.Integer, nullable=False)
dtnom = db.Column(db.DateTime)
dtvote = db.Column(db.DateTime)
dtstatic = db.Column(db.DateTime)
def __repr__(self):
return "<State %r>" % self.id