-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
112 lines (83 loc) · 3.34 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
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
db = SQLAlchemy()
bcrypt = Bcrypt()
def connect_db(app):
'''Connect to database.'''
db.app = app
db.init_app(app)
class User(db.Model):
'''User Model'''
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
first_name = db.Column(db.String(30), nullable=False)
last_name = db.Column(db.String(30), nullable=False)
email = db.Column(db.Text, nullable=False, unique=True)
profile_picture = db.Column(
db.Text, nullable=False, default='https://www.pinclipart.com/picdir/big/148-1486972_mystery-man-avatar-circle-clipart.png')
password = db.Column(db.Text, nullable=False)
# Relationship to wishlist
wishlists = db.relationship(
'Wishlist', backref='user', cascade='all, delete-orphan')
# Relationship to product
products = db.relationship(
'Product', backref='user', cascade='all, delete-orphan')
# Properties
@property
def full_name(self):
'''Return full name of user.'''
return f'{self.first_name} {self.last_name}'
# Methods
@classmethod
def create_account(cls, first_name, last_name, email, profile_picture, password):
'''Create a new user account.'''
hashed_password = bcrypt.generate_password_hash(
password).decode('utf-8')
user = cls(first_name=first_name, last_name=last_name,
email=email, profile_picture=profile_picture, password=hashed_password)
return user
@classmethod
def authenticate(cls, email, password):
'''
Authenticate a user.
Return user if auth is successful, otherwise return False.
'''
user = cls.query.filter_by(email=email).first()
if user and bcrypt.check_password_hash(user.password, password):
return user
return False
class Wishlist(db.Model):
'''Wishlist Model'''
__tablename__ = 'wishlists'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(30), nullable=False, unique=True)
description = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# Relationship to product
products = db.relationship(
'Product', secondary='wishlist_products', backref='wishlists', cascade='all, delete')
def serialize(self):
'''Serialize wishlist data.'''
return {
'id': self.id,
'name': self.name,
'description': self.description,
'user_id': self.user_id
}
class WishlistProduct(db.Model):
'''WishlistProduct Model'''
__tablename__ = 'wishlist_products'
wishlist_id = db.Column(db.Integer, db.ForeignKey(
'wishlists.id'), primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey(
'products.id'), primary_key=True)
class Product(db.Model):
'''Product Model'''
__tablename__ = 'products'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.Text, nullable=False)
currency = db.Column(db.Text, nullable=False)
price = db.Column(db.Float, nullable=False)
image_url = db.Column(db.Text, nullable=False)
product_url = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))