-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
90 lines (75 loc) · 2.74 KB
/
model.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
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.sql import func
from passlib.apps import custom_app_context as pwd_context
from flask_login import UserMixin
Base = declarative_base()
class User(Base, UserMixin):
"""
Class user maps user properties to DB table
Attributes:
id: user unique id
username: username for login
email: user email
password: user hashed password
"""
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(String(32), index=True)
email = Column(String)
password = Column(String(64))
"""convert password to hashed password"""
def hash_password(self, password):
self.password = pwd_context.encrypt(password)
"""verify entered password against hashed password """
def verify_password(self, password):
return pwd_context.verify(password, self.password)
class Category(Base):
"""
Class category maps app categories to DB table
Attributes:
id: category unique id
name: a string contains category name
"""
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
name = Column(String, index=True)
# return category in serializable format
@property
def serialize(self):
return {
'id': self.id,
'name': self.name
}
class Item(Base):
"""
Class item maps different user items to DB table
Attributes:
id: unique id for item
title: a string contains item title
description: a string contains description about item
time_created: contains create date and time of a specific item
user_id: foreign key that specify which user created the item
category_id: foreign key specify which category item belongs to
"""
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
title = Column(String(32), nullable=False)
description = Column(String, nullable=False)
time_created = Column(DateTime(timezone=True), server_default=func.now())
user_id = Column(Integer, ForeignKey('user.id'))
category_id = Column(Integer, ForeignKey('category.id'))
user = relationship(User)
category = relationship(Category)
@property
def serialize(self):
return {
'id': self.id,
'title': self.title,
'description': self.description,
'category': self.category.name
}
engine = create_engine('sqlite:///catalog.db')
Base.metadata.create_all(engine)