-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
67 lines (51 loc) · 1.78 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
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
group_number = db.Column(db.String)
__table_args__ = (db.UniqueConstraint('name', 'group_number', name='Name_Group_UC'),)
def __init__(self, name, group_number):
self.name = name
self.group_number = group_number
def __repr__(self):
return '<Student: %s, %s>' % (self.name, self.group_number)
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'group_number': self.group_number
}
def save(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
class Visit(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date)
pair_num = db.Column(db.Integer)
student_id = db.Column(db.Integer, db.ForeignKey('student.id'))
student = db.relationship('Student')
__table_args__ = (db.UniqueConstraint('date', 'pair_num', 'student_id', name='Stud_Date_Pair_UC'),)
def __init__(self, date, pair_num, student):
self.date = date
self.pair_num = pair_num
self.student = student
def __repr__(self):
return '<Visit: %s, %s, %s>' % (self.student, self.date, self.pair_num)
def to_dict(self):
return {
'id': self.id,
'date': self.date.strftime('%Y.%m.%d'),
'pair_num': self.pair_num,
'student_id': self.student_id
}
def save(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()