-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
136 lines (105 loc) · 5.22 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
131
132
133
134
135
136
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
db = SQLAlchemy()
# Association table for many-to-many relationship between Movie and Genre
movie_genre = db.Table('movie_genre',
db.Column('movie_id', db.Integer, db.ForeignKey('movie.id'), primary_key=True),
db.Column('genre_id', db.Integer, db.ForeignKey('genre.id'), primary_key=True)
)
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(200), nullable=False)
gender = db.Column(db.String(10), nullable=True) # Optional field for gender
profile_picture = db.Column(db.String(200), nullable=True) # Optional field for profile picture URL
password_update_date = db.Column(db.DateTime, nullable=True)
join_date = db.Column(db.DateTime, default=db.func.current_timestamp())
movies = db.relationship('Movie', backref='user', lazy=True)
favorites = db.relationship('Favorite', back_populates='user', lazy=True)
admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'), nullable=True)
def set_password(self, password):
"""
Set the password for the user and update the password change timestamp.
Parameters:
password (str): The new password to be set. It should be a string of at least 8 characters.
Returns:
None
"""
self.password = generate_password_hash(password)
self.password_update_date = datetime.now() # Update password change timestamp
def check_password(self, password):
"""
Check if the provided password matches the hashed password stored in the User object.
Parameters:
password (str): The password provided by the user.
Returns:
bool: True if the provided password matches the hashed password, False otherwise.
"""
return check_password_hash(self.password, password)
class Director(db.Model):
__tablename__ = 'director'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False, unique=True)
# Relationships
movies = db.relationship('Movie', backref='director', lazy=True)
class Movie(db.Model):
__tablename__ = 'movie'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
year = db.Column(db.Integer, nullable=False)
rating = db.Column(db.Float, nullable=False)
poster = db.Column(db.String(255)) # Optional field to store movie posters
plot = db.Column(db.Text) # Optional field to store the plot
imdbID = db.Column(db.String(255), nullable=True) # IMDb ID or link
trailer = db.Column(db.String(255), nullable=True) # New field to store YouTube trailer URL
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
director_id = db.Column(db.Integer, db.ForeignKey('director.id'), nullable=False) # Foreign key to Director
genres = db.relationship('Genre', secondary=movie_genre, backref='movies')
favorites = db.relationship('Favorite', back_populates='movie', lazy=True) # Add this line
class Genre(db.Model):
__tablename__ = 'genre'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
class Favorite(db.Model):
__tablename__ = 'favorite'
id = db.Column(db.Integer, primary_key=True)
movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
user = db.relationship('User', back_populates='favorites')
movie = db.relationship('Movie', back_populates='favorites')
# movie = db.relationship('Movie', backref='favorites', lazy=True)
class Admin(db.Model):
__tablename__ = 'admin'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(200), nullable=False)
def set_password(self, password):
"""
Set the password for the admin.
Parameters:
password (str): The new password to be set.
Returns:
None
"""
self.password = generate_password_hash(password)
def check_password(self, password):
"""
Check if the provided password matches the hashed password stored in the User object.
Parameters:
password (str): The password provided by the user.
Returns:
bool: True if the provided password matches the hashed password, False otherwise.
"""
return check_password_hash(self.password, password)
class Contact(db.Model):
__tablename__ = 'contact'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False)
message = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())