-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
128 lines (118 loc) · 3.1 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
117
118
119
120
121
122
123
124
125
126
127
128
from email.policy import default
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
db = SQLAlchemy()
class Entry(db.Model):
__tablename__ = "entries"
id = db.Column(
db.Integer,
primary_key = True,
)
name = db.Column(
db.String(200),
nullable=False,
unique=False
)
section = db.Column(
db.String(100),
nullable=False,
unique=False
)
score = db.Column(
db.Float(100),
nullable=False,
unique=False
)
exam = db.Column(
db.String(200),
nullable=False,
unique=False
)
subject_id = db.Column( #subject FK
db.Integer,
db.ForeignKey('subjects.id',ondelete = "CASCADE"),
nullable = False,
index = True
)
class Subject(db.Model):
__tablename__ = "subjects"
id = db.Column(
db.Integer,
primary_key=True
)
subject_name = db.Column(
db.String(300),
nullable = False,
unique = False
)
school_year = db.Column(
db.String(50),
nullable = False,
unique = False
)
pin = db.Column(
db.Integer,
nullable = False,
unique = False,
default = 1234
)
views = db.Column(
db.Integer,
nullable = False,
unique = False,
default = 0
)
teacher_id = db.Column(
db.Integer,
db.ForeignKey('users.id',ondelete = "CASCADE"),
nullable = False,
index = True
)
entry = db.relationship('Entry',backref = 'subject') #relationship to Entry
class User(UserMixin, db.Model):
"""User account model."""
__tablename__ = 'users'
id = db.Column(
db.Integer,
primary_key=True
)
username = db.Column(
db.String(100),
nullable=False,
unique=True
)
name = db.Column(
db.String(100),
nullable=False,
unique=False
)
password = db.Column(
db.String(200),
primary_key=False,
unique=False,
nullable=False
)
current_subject_id = db.Column( #current subject being edited by the user
db.Integer,
nullable = True,
unique = False
)
def set_password(self, password):
"""Create hashed password."""
self.password = generate_password_hash(
password,
method='sha256'
)
def check_password(self, password):
"""Check hashed password."""
return check_password_hash(self.password, password)
def __repr__(self):
return '<User {}>'.format(self.username)
def get_user_details(self):
current_sid = self.current_subject_id
current_sid = current_sid if not current_sid is None else "NULL"
return {"name":self.name,"username":self.username,"id":self.id,"current_subject_id":current_sid}
subject = db.relationship('Subject',backref = 'user') #relationship to subject