-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount.py
81 lines (69 loc) · 3.34 KB
/
account.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
"""The Account model.
Also contains the function to load the user for the login manager."""
from flask_login.mixins import UserMixin
from sqlalchemy.dialects.postgresql import JSONB
from innopoints.extensions import db, login_manager
from innopoints.models.notification import NotificationGroup
from innopoints.models.transaction import Transaction
DEFAULT_NOTIFICATIONS = {
NotificationGroup.innostore: 'off',
NotificationGroup.volunteering: 'off',
NotificationGroup.project_creation: 'off',
NotificationGroup.administration: 'off',
NotificationGroup.service: 'email',
}
class Account(UserMixin, db.Model):
"""Represents an account of a logged in user."""
__tablename__ = 'accounts'
full_name = db.Column(db.String(256), nullable=False)
group = db.Column(db.String(64), nullable=True)
email = db.Column(db.String(128), primary_key=True)
telegram_username = db.Column(db.String(32), nullable=True)
is_admin = db.Column(db.Boolean, nullable=False)
created_projects = db.relationship('Project',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='creator')
notification_settings = db.Column(JSONB, nullable=False, default=DEFAULT_NOTIFICATIONS)
moderated_projects = db.relationship('Project',
secondary='project_moderation',
back_populates='moderators')
stock_changes = db.relationship('StockChange',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='account')
transactions = db.relationship('Transaction',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='account')
applications = db.relationship('Application',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='applicant')
static_files = db.relationship('StaticFile',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='owner')
reports = db.relationship('VolunteeringReport',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='reporter')
notifications = db.relationship('Notification',
cascade='all, delete-orphan',
passive_deletes=True,
back_populates='recipient')
def get_id(self):
"""Return the user's e-mail."""
return self.email
@property
def balance(self):
"""Return the user's innopoints balance."""
return db.session.query(
db.func.sum(Transaction.change)
).filter(
Transaction.account_email == self.email
).scalar() or 0
@login_manager.user_loader
def load_user(email):
"""Return a user instance by the e-mail."""
return Account.query.get(email)