-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
87 lines (63 loc) · 2.05 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
from datetime import datetime
from sqlalchemy import Column, Integer, DateTime, String
from db import db
# Models
''' Movie Model '''
class Movie(db.Model):
__tablename__ = "movies"
id = Column(Integer, primary_key=True, nullable=False)
title = Column(db.String, unique=True, nullable=False)
release_date = Column(DateTime, default=datetime.now(), nullable=False)
def __init__(self, title, release_date=datetime.now()):
self.title = title
self.release_date = release_date
def __repr__(self):
return f"( Movie {self.id} {self.title} {self.release_date} )"
# Insert Movie into the database
def insert(self):
db.session.add(self)
db.session.commit()
# Update Movie in the database
def update(self):
db.session.commit()
# Delete Movie from the database
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'title': self.title,
'release_date': self.release_date
}
''' Actor Model '''
class Actor(db.Model):
__tablename__ = "actors"
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(128), unique=True, nullable=False)
age = Column(Integer, nullable=False)
gender = Column(String(12), nullable=False)
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def __repr__(self):
return f"( Actor {self.id} {self.name} {self.age} {self.gender} )"
# Insert Actor into the database
def insert(self):
db.session.add(self)
db.session.commit()
# Update Actor in the database
def update(self):
db.session.commit()
# Delete Actor from the database database
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': self.gender
}