-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
99 lines (76 loc) · 2.45 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
import os
from sqlalchemy import Column, String, Integer
from flask_sqlalchemy import SQLAlchemy
database_path = os.environ['DATABASE_URL']
db = SQLAlchemy()
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
def db_drop_and_create_all():
db.drop_all()
db.create_all()
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
# db_drop_and_create_all()
'''
drops the database tables and starts fresh
can be used to initialize a clean database
'''
# Actor by movie
actor_movie = db.Table('actor_movie',
db.Column('Actor', db.Integer, db.ForeignKey(
'Actor.id'), primary_key=True),
db.Column('Movie', db.Integer, db.ForeignKey(
'Movie.id'), primary_key=True),
)
class Movie(db.Model):
__tablename__ = 'Movie'
id = Column(Integer().with_variant(Integer, "sqlite"), primary_key=True)
title = Column(String(100), nullable=False)
release_date = db.Column(db.Date, nullable=False)
actor_movie = db.relationship(
'Actor', secondary=actor_movie, backref=db.backref(
'actor_movie'), lazy='dynamic')
def insert(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def update(self):
db.session.commit()
def format(self):
return {
'id': self.id,
'title': self.title,
'release_date': self.release_date.strftime('%Y-%m-%d')
}
def __repr__(self):
return self.title
class Actor(db.Model):
__tablename__ = 'Actor'
id = Column(Integer().with_variant(Integer, "sqlite"), primary_key=True)
name = Column(String(120), nullable=False)
age = Column(db.Integer, nullable=False)
gender = db.Column(db.String(120), nullable=False)
def insert(self):
db.session.add(self)
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def update(self):
db.session.commit()
def format(self):
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': self.gender
}
def __repr__(self):
return self.name