-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
130 lines (101 loc) · 3.69 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
from app import db, ma
from passlib.apps import custom_app_context as pwd_context
from marshmallow_sqlalchemy.fields import Nested
from marshmallow import fields
class User(db.Model):
userId = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
surname = db.Column(db.String(255))
email = db.Column(db.String(255), unique=True, nullable=False)
password = db.Column(db.Text, nullable=False)
github_username = db.Column(db.String(80))
created_date = db.Column(db.DateTime)
repos = db.relationship('Repo', backref='user', lazy=True)
def save_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
def add_repo(self, repo:'Repo'):
self.repos.append(repo)
db.session.commit()
def hash_password(self, password):
self.password = password
# self.password = pwd_context.encrypt(password)
def verify_password(self, password):
if self.password == password:
return True
else:
return False
# return pwd_context.verify(password, self.password)
def commit(self):
db.session.commit()
@classmethod
def find_by_email(cls, email):
return cls.query.filter_by(email=email).first()
@classmethod
def find_by_id(cls, _id):
return cls.query.filter_by(userId=_id).first()
class Repo(db.Model):
repoId = db.Column(db.Integer, primary_key=True)
userId = db.Column(db.Integer, db.ForeignKey('user.userId'), nullable=False)
url = db.Column(db.Text, nullable=False)
language = db.Column(db.String(80))
isChecked = db.Column(db.Boolean, nullable=False, default=False)
added_date = db.Column(db.DateTime)
updated_date = db.Column(db.DateTime)
checks = db.relationship('Check', backref='repo', lazy=True)
def save_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
def add_check(self, check:'Check'):
self.checks.append(check)
db.session.commit()
def commit(self):
db.session.commit()
@classmethod
def find_by_id(cls, _id):
return cls.query.filter_by(repoId=_id).first()
class Check(db.Model):
checkId = db.Column(db.Integer, primary_key=True)
repoId = db.Column(db.Integer, db.ForeignKey('repo.repoId'), nullable=False)
check_date = db.Column(db.DateTime)
inconsistency = db.Column(db.Text)
def save_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
def commit(self):
db.session.commit()
class CheckSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Check
load_instance = True
include_fk = True
exclude = ("check_date",)
class RepoSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Repo
include_fk = True
include_relationships = True
load_instance = True
exclude = ("added_date","language", "updated_date", )
checks = Nested(CheckSchema, many=True)
class UserSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = User
include_relationships = True
load_instance = True
exclude = ("password","created_date", "github_username")
repos = Nested(RepoSchema, many=True)
if __name__ == "__main__":
print("Creating database tables...")
# db.drop_all()
db.create_all()
print("Done!")