-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
executable file
·85 lines (69 loc) · 2.79 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
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
import bcrypt
from flask_login import UserMixin
import datetime
db = SQLAlchemy()
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
email = db.Column(db.String(150), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
plan = db.Column(db.String(50), nullable=False)
is_admin = db.Column(db.Boolean, default=False)
is_adult = db.Column(db.Boolean, default=False)
def set_password(self, password):
self.password_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def check_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'), self.password_hash.encode('utf-8'))
def get_id(self):
return self.id
@property
def is_active(self):
return True
@property
def is_authenticated(self):
return True
@property
def is_anonymous(self):
return False
class Movie(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(500), nullable=False)
genre = db.Column(db.String(500), nullable=True)
youtube_link = db.Column(db.String(200), nullable=False)
image_url = db.Column(db.String(200), nullable=False)
def to_dict(self):
return {
'id': self.id,
'title': self.title,
'genre': self.genre,
'description': self.description,
'youtube_link': self.youtube_link,
'image_url': self.image_url
}
class MovieStats(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
time_frame = db.Column(db.String(50), nullable=False)
count = db.Column(db.Integer, nullable=False)
date = db.Column(db.Date, nullable=False, default=datetime.datetime.utcnow)
genre = db.Column(db.String(50), nullable=False)
user = db.relationship('User', backref=db.backref('movie_stats', lazy=True))
def to_dict(self):
return {
'id': self.id,
'user_id': self.user_id,
'time_frame': self.time_frame,
'count': self.count,
'date': self.date.isoformat(),
'genre': self.genre
}
class BlogPost(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(150), nullable=False)
content = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
def __repr__(self):
return f'<BlogPost {self.title}>'