-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
139 lines (97 loc) · 3.3 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
#----------------
# Imports
#----------------
import os
from flask_sqlalchemy import SQLAlchemy
from datetime import date
import json
from sqlalchemy import DateTime, ForeignKey,Table
from datetime import date
# Database Setup
# Use production database.
database_path = os.environ['DATABASE_URL']
# If you want run locally you can give path as follow.
#database_path = "postgres://{}:{}@{}/{}".format('postgres','1234','localhost:5432', 'casting')
db = SQLAlchemy()
def setup_db(app, database_path=database_path):
'''Binds a Flask Application and a SQLAlchemy '''
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
def db_drop_create_all():
'''drop existing database and create new fresh database'''
db.drop_all()
db.create_all()
create_records()
# Movie table
class Movie(db.Model):
__tablename__ = 'movies'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50), nullable=False)
release_date = db.Column(db.DateTime(), nullable=False)
def __repr__(self):
return f"<Movie id and title is {self.id} {self.title}>"
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 attributes(self):
return {
'id': self.id,
'title': self.title,
'release_date': self.release_date,
}
# Actors Model
class Actor(db.Model):
__tablename__ = 'actors'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
age = db.Column(db.Integer, nullable=False)
gender = db.Column(db.String, nullable=False)
def __repr__(self):
return f"<Actor id and name is {self.id} {self.name}>"
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = 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 attributes(self):
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': self.gender,
}
def create_records():
'''initialize own records'''
# Create record for new movie
new_movie_1 = Movie(title='Jannat',release_date=date.today())
new_movie_2 = Movie(title='Partner',release_date=date.today())
new_movie_3 = Movie(title='Super 30',release_date=date.today())
new_movie_4 = Movie(title='Avengers-End game',release_date=date.today())
new_movie_1.insert()
new_movie_2.insert()
new_movie_3.insert()
new_movie_4.insert()
new_actor_1 = Actor(name='Emraan Hashmi',gender='Male',age=39)
new_actor_2 = Actor(name='salman khan',gender='Male',age=50)
new_actor_3 = Actor(name='Hrithik Roshan',gender='Male',age=40)
new_actor_4 = Actor(name='Tony Starc',gender='Male',age=45)
new_actor_1.insert()
new_actor_2.insert()
new_actor_3.insert()
new_actor_4.insert()
db.session.commit()