-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
159 lines (128 loc) · 3.67 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
import enum
from datetime import datetime
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import (
Column,
String,
Integer,
Enum,
DateTime,
ForeignKey
)
db = SQLAlchemy()
def setup_db(app):
"""
setup_db(app)
binds a flask application and a SQLAlchemy service
"""
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
Migrate(app, db)
class GenderEnum(enum.Enum):
"""
constraint gender to be one of:
male
female
to avoid strings other than those in the database
"""
male = 1
female = 2
@staticmethod
def transform(gender: str):
gender = gender.lower()
if gender == 'male':
return GenderEnum.male
elif gender == 'female':
return GenderEnum.female
raise Exception('Gender should be "male" or "female"')
@staticmethod
def reverse_transform(gender):
if gender == GenderEnum.male:
return 'male'
return 'female'
class Actor(db.Model):
"""
Database model for an actor
"""
__tablename__ = 'actor'
id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)
gender = Column(Enum(GenderEnum))
movies = db.relationship('Job', backref='movie', lazy=True)
def __init__(self, name: str, age: int, gender: str):
self.name = name
self.age = age
self.gender = GenderEnum.transform(gender)
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return f'Actor: \n{vars(self)}'
@property
def serialize(self):
"""Return object data in easily serializable format"""
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': GenderEnum.reverse_transform(self.gender)
}
class Movie(db.Model):
"""
Database model for a movie
"""
__tablename__ = 'movie'
id = Column(Integer, primary_key=True)
title = Column(String)
release_date = Column(DateTime, default=datetime.utcnow)
actors = db.relationship('Job', backref='actor', lazy=True)
def __init__(self, title: str, release_date: datetime = datetime.utcnow()):
self.title = title
self.release_date = release_date
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return f'Movie: \n{vars(self)}'
@property
def serialize(self):
return {
'id': self.id,
'title': self.title,
'release_date': self.release_date,
}
class Job(db.Model):
"""
Many-to-many relationship between actors and movies
"""
__tablename__ = 'job'
id = Column(Integer, primary_key=True)
movie_id = Column(Integer, ForeignKey('movie.id', ondelete='CASCADE'), nullable=False)
actor_id = Column(Integer, ForeignKey('actor.id', ondelete='CASCADE'), nullable=False)
def __init__(self, movie_id: int, actor_id: int):
self.movie_id = movie_id
self.actor_id = actor_id
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return f'ActingJob: \n{vars(self)}'