-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
185 lines (149 loc) · 4.8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
from sqlalchemy import (
Column,
String,
Integer,
ForeignKey,
Boolean,
DateTime,
create_engine
)
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from dotenv import load_dotenv
# load .env file in backend dir using python-dotenv lib
path = os.path.dirname(os.path.dirname(__file__))
env_file = os.path.join(path, '.env')
load_dotenv(env_file)
# read variables from .env file
db_name = os.getenv('DB_NAME')
db_host = os.getenv('DB_HOST')
db_user = os.getenv('DB_USER')
db_passwd = os.getenv('DB_PASS')
# connect to senpais_log db:
database_path = f'postgresql://{db_user}:{db_passwd}@{db_host}/{db_name}'
db = SQLAlchemy()
def setup_db(app, database_path=database_path):
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
# database schema:
class User(db.Model):
'''User - Senpai's Log
This class represents the table for anime users
'''
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String, nullable=False)
email = Column(String(255), nullable=False)
password = Column(String, nullable=False)
def __init__(self, username, email, password):
'''constructor'''
self.username = username
self.email = email
self.password = password
# CRUD helper functions;
def insert(self):
'''INSERT SQLAlchemy session'''
db.session.add(self)
db.session.commit()
def update(self):
'''UPDATE SQLAlchemy session'''
db.session.commit()
def delete(self):
'''DELETE/REMOVE SQLAlchemy session object'''
db.session.delete(self)
db.session.commit()
def format(self):
'''format and return JSON object'''
return {
'id': self.id,
'username': self.username,
'email': self.email,
'password': self.password,
}
class Anime(db.Model):
'''Anime - Senpai's Log
This class represents the table for anime titles
'''
__tablename__ = 'anime'
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False)
description = Column(String)
genre = Column(String)
release_date = Column(DateTime, default=datetime.utcnow)
image_url = Column(String)
watched = Column(Boolean)
def __init__(self, title, description, genre, release_date, image_url, watched):
'''constructor'''
self.title = title
self.description = description
self.genre = genre
self.release_date = release_date
self.image_url = image_url
self.watched = watched
# CRUD helper functions;
def insert(self):
'''INSERT SQLAlchemy session'''
db.session.add(self)
db.session.commit()
def update(self):
'''UPDATE SQLAlchemy session'''
db.session.commit()
def delete(self):
'''DELETE/REMOVE SQLAlchemy session object'''
db.session.delete(self)
db.session.commit()
def format(self):
'''format and return JSON object'''
return {
'id': self.id,
'title': self.title,
'description': self.description,
'genre': self.genre,
'release_date': self.release_date,
'image_url': self.image_url,
'watched': self.watched,
}
class AnimeLog(db.Model):
'''AnimeLog - Senpai's Log
This class represents the table for anime watchlists
'''
__tablename__ = 'anime_logs'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
anime_id = Column(Integer, ForeignKey('anime.id'), nullable=False)
watched = Column(Boolean)
users = db.relationship('User', backref=db.backref('anime_logs', lazy=True))
anime = db.relationship('Anime', backref=db.backref('anime_logs', lazy=True))
def __init__(self, user_id, anime_id, watched):
'''constructor'''
self.user_id = user_id
self.anime_id = anime_id
self.watched = watched
# CRUD helper functions;
def insert(self):
'''INSERT SQLAlchemy session'''
db.session.add(self)
db.session.commit()
def update(self):
'''UPDATE SQLAlchemy session'''
db.session.commit()
def delete(self):
'''DELETE/REMOVE SQLAlchemy session object'''
db.session.delete(self)
db.session.commit()
def format(self):
'''format and return JSON object'''
return {
'id': self.id,
'user_id': self.user_id,
'anime_id': self.anime_id,
'watched': self.watched,
}