-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.py
400 lines (330 loc) · 12 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import json
from flask import Flask, make_response, request, url_for, jsonify, render_template, request, redirect, session
import MySQLdb
from flask.ext.httpauth import HTTPBasicAuth
import os
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash
from sqlalchemy import create_engine
from sqlalchemy import text
from functools import wraps
db = SQLAlchemy()
#User Model
class User(db.Model):
__tablename__ = 'db_user'
user_id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(30))
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(128))
link_to_dp = db.Column(db.String(1000))
type_flag = db.Column(db.Integer)
DOB = db.Column(db.String(1000))
def __init__(self, name, email, password, link_to_dp, type_flag_, dob):
self.name = name.title()
self.email = email.lower()
self.set_password(password)
self.link_to_dp = link_to_dp
self.type_flag = type_flag_
self.DOB = dob
def set_password(self, password_):
self.password = generate_password_hash(password_)
def check_password(self, password_):
return check_password_hash(self.password, password_)
@property
def is_authenticated(self):
return True
@property
def is_active(self):
return True
@property
def is_anonymous(self):
return False
def get_id(self):
try:
return unicode(self.user_id) # python 2
except NameError:
return str(self.user_id) # python 3
# Lecture Model
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'user_id' : self.user_id,
'name' : self.name,
'email' : self.email,
'password' : self.password,
'link_to_dp' : self.link_to_dp,
'type_flag' : self.type_flag,
'DOB' : self.DOB
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Student Model
class Student(db.Model):
__tablename__ = 'db_Student'
Student_Id = db.Column(db.Integer , db.ForeignKey(User.user_id), primary_key = True)
Performance_Sheet = db.relationship('Performance_Sheet' , backref = 'Student' , lazy = 'dynamic')
def __init__(self,s_id):
self.Student_Id = s_id
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'Student_Id' : self.Student_Id,
'Performance_Sheet' : self.Performance_Sheet
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Faculty Model
class Faculty(db.Model):
__tablename__ = 'db_Faculty'
Faculty_Id = db.Column(db.Integer , db.ForeignKey(User.user_id), primary_key = True)
def __init__(self,facid):
self.Faculty_Id = facid
def __init__(self,facid):
self.Faculty_Id = facid
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'Faculty_Id' : self.Faculty_Id,
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Admin Model
class Admin(db.Model):
__tablename__ = 'db_Admin'
Admin_Id = db.Column(db.Integer, db.ForeignKey(User.user_id), primary_key = True)
def __init__(self,admid):
self.Admin_Id = admid
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'Admin_Id' : self.Admin_Id
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Course Table
class Course(db.Model):
__tablename__= 'db_course'
course_id = db.Column(db.Integer, primary_key = True)
faculty = db.Column(db.Integer, db.ForeignKey(Faculty.Faculty_Id))
course_name = db.Column(db.String(30))
prereq = db.Column(db.Integer)
syllabus = db.Column(db.String(500))
notices = db.relationship('Notice', backref='Course', lazy='dynamic')
approved = db.Column(db.Integer) # 1 after admin approves the course
def __init__(self, fid, cid, cname, pre):
self.faculty = session['user_id']
self.course_id = cid
self.course_name = cname
self.prereq = pre
self.approved = 0 # Initially unapproved
def approve(self):
self.approve = 1
def setSyllabus(self, syl):
self.syllabus = syl
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'course_id' : self.course_id,
'faculty' : self.faculty,
'course_name' : self.course_name,
'prereq' : self.prereq,
'syllabus' : self.syllabus,
'approved' : self.approved
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Lecture Model
class Lecture(db.Model):
__tablename__ = 'db_Lecture'
Lecture_Id = db.Column(db.Integer, primary_key = True)
Course_Id = db.Column(db.Integer, db.ForeignKey(Course.course_id))
Notes = db.Column(db.Text)
Date_Time = db.Column(db.DateTime)
Topic = db.Column(db.Text)
Test = db.relationship('Test', backref = 'Lecture' , lazy = 'dynamic')
# Initialization func
def __init__ (self, course_id, notes , timestamp , topic):
self.Course_Id = course_id
self.Notes = notes
self.Date_Time = timestamp
self.Topic = topic
@property
def serialize(self):
return {
'Lecture_Id' : self.Lecture_Id,
'Notes' : self.Notes ,
'Date_Time' : self.Date_Time,
'Topic' : self.Topic,
}
@property
def serialize_many2many(self):
return [item.serialize for item in self.many2many]
# Test Model
class Test(db.Model):
__tablename__ = 'db_Test'
Test_Id = db.Column(db.Integer, primary_key = True)
Lecture_Id = db.Column(db.Integer, db.ForeignKey(Lecture.Lecture_Id))
Question_Paper = db.relationship('Question', backref = 'Test' , lazy = 'dynamic')
def __init__ (self, test_id , lec_id ):
self.Test_Id = test_id
self.Lecture_Id = lec_id
@property
def serialize(self):
return {
'Test_Id' : self.Test_Id,
'Lecture_Id' : self.Lecture_Id
}
@property
def serialize_many2many(self):
return [item.serialize for item in self.many2many]
# Question Model
class Question(db.Model):
__tablename__ = 'db_Questions'
Question_Id = db.Column(db.Integer, db.ForeignKey(Test.Lecture_Id))
text = db.Column(db.String(200) , primary_key = True)
answ = db.Column(db.String(200))
def __init__ (self , qtext ,qansw):
self.text = qtext
self.answ = qansw
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'Question_Id' : self.Question_Id,
'text' : self.text,
'answ' : self.answ
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
#Performance Model
class Performance_Sheet(db.Model):
__tablename__ = 'db_Performance_Sheet'
Student_Id = db.Column(db.Integer , db.ForeignKey(Student.Student_Id) , primary_key = True)
Test_Id = db.Column(db.Integer , db.ForeignKey(Test.Test_Id), primary_key = True)
Marks_Obtained = db.Column(db.Float)
Marks_Total = db.Column(db.Float)
def __init__(self, sid, tid, mark_obt, mark_tot):
self.Student_Id = sid
self.Test_Id = tid
self.Marks_Obtained = mark_obt
self.Marks_Total = mark_tot
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'Student_Id' : self.Student_Id,
'Test_Id' : self.Test_Id,
'Marks_Obtained' : self.Marks_Obtained,
'Marks_Total' : self.Marks_Total
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Enrolls relationship
class Enrolls(db.Model):
__tablename__= 'db_enrolls'
enrolls_id = db.Column(db.Integer, primary_key = True)
student_id = db.Column(db.Integer, db.ForeignKey(Student.Student_Id))
course_id = db.Column(db.Integer, db.ForeignKey(Course.course_id))
def __init__(self, sid, cid):
self.student_id = sid
self.course_id = cid
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'enrolls_id' : self.enrolls_id,
'student_id' : self.student_id,
'course_id' : self.course_id
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]
# Notice Table
class Notice(db.Model):
__tablename__= 'db_notice'
notice_id = db.Column(db.Integer, primary_key = True)
timestamp = db.Column(db.DateTime)
message = db.Column(db.String(500))
c_id = db.Column(db.Integer, db.ForeignKey(Course.course_id))
def __init__(self, cid, msg):
self.message = msg
self.c_id= cid
self.c_id = cid
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'notice_id' : self.notice_id,
'timestamp' : self.timestamp,
'message' : self.message,
'c_id' : self.c_id
# This is an example how to deal with Many2Many relations
# 'many2many' : self.serialize_many2many
}
@property
def serialize_many2many(self):
"""
Return object's relations in easily serializeable format.
NB! Calls many2many's serialize property.
"""
return [ item.serialize for item in self.many2many]