-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_setup.py
82 lines (60 loc) · 2.34 KB
/
database_setup.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 sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from passlib.apps import custom_app_context as pwd_context
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
email = Column(String(250), nullable=False)
password_hash = Column(String(64))
def hash_password(self, password):
self.password_hash = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password_hash)
class Categories(Base):
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
class Products(Base):
__tablename__ = 'product'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
category = Column(String(100), nullable=False)
cat_id = Column(Integer, ForeignKey('categories.id'))
categories = relationship(Categories, backref=backref("product", cascade="all, delete-orphan"))
desc = Column(String(1000))
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship(User)
img = Column(String(100))
url = Column(String(100))
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'name': self.name,
'id': self.id,
'category': self.category,
'description': self.desc,
}
class Reviews(Base):
__tablename__ = 'reviews'
# "{:%H:%M.%S %B %d, %Y}".format(datetime.datetime.utcnow())
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship(User)
product_id = Column(Integer, ForeignKey('product.id'))
product = relationship(Products)
review = Column(String(1000), nullable=False)
@property
def serialize(self):
"""Return object data in easily serializeable format"""
return {
'user': self.user,
'product': self.product_id,
'review': self.review,
}
engine = create_engine('sqlite:///1_electronics_catalog.db')
Base.metadata.create_all(engine)