-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
82 lines (64 loc) · 2.54 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
from flask_login import UserMixin
from sqlalchemy.orm import relationship
from app import db
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True)
login = db.Column(db.String, nullable=True, unique=True)
email = db.Column(db.String, nullable=True, unique=True)
password = db.Column(db.String, nullable=True)
is_admin = db.Column(db.Boolean, nullable=True, default=False)
@staticmethod
def add(login, email, password):
user = User()
user.login = login
user.email = email
user.password = password
db.session.add(user)
db.session.commit()
class Good(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True)
name = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=False)
category = db.Column(db.String, nullable=False)
manufacturer = db.Column(db.String, nullable=False)
price = db.Column(db.Integer, nullable=False)
photo = db.Column(db.String, nullable=False)
likes = relationship("Like")
@staticmethod
def add(name, description, category, manufacturer, price, photo):
good = Good()
good.name = name
good.description = description
good.category = category
good.manufacturer = manufacturer
good.price = price
good.photo = photo
db.session.add(good)
db.session.commit()
class Like(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
good_id = db.Column(db.Integer(), db.ForeignKey('good.id'))
score = db.Column(db.Integer())
@staticmethod
def add(user_id, good_id, score):
like = Like()
like.user_id = user_id
like.good_id = good_id
like.score = score
db.session.add(like)
db.session.commit()
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, unique=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
good_id = db.Column(db.Integer(), db.ForeignKey('good.id'))
comment = db.Column(db.String(), nullable=False)
user = db.relationship('User', backref='User')
@staticmethod
def add(user_id, good_id, comment):
comment_model = Comment()
comment_model.user_id = user_id
comment_model.good_id = good_id
comment_model.comment = comment
db.session.add(comment_model)
db.session.commit()