-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
145 lines (126 loc) · 5.21 KB
/
main.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
from flask import Flask, render_template, request, redirect
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import exc
from api_movies import Movie_api
from forms import UpdateForm, AddForm
import os
# ---------------------------------------------------------------------------------------
# ADDITIONAL DATABASE FUNCTIONS
# ---------------------------------------------------------------------------------------
def add_movie(movie) :
try :
db.session.add(movie)
return db.session.commit()
except exc.IntegrityError :
# print('Duplicate data')
db.session.rollback()
return False
def update_rank() :
count_data = Movies.query.count()
if count_data > 0 :
movie_list = Movies.query.order_by(Movies.rating).all()
for i in range(count_data) : movie_list[i].ranking = count_data - i
db.session.commit()
def first_movies(first_use = False) :
count_data = Movies.query.count()
if count_data == 0 and first_use == True :
init_movie = Movies(
id = 1,
title = "Phone Booth",
year = 2002,
description = "Publicist Stuart Shepard finds himself trapped in a phone booth, pinned down by an extortionist's sniper rifle. Unable to leave or receive outside help, Stuart's negotiation with the caller leads to a jaw-dropping climax.",
rating = 7.3,
ranking = 10,
review = "My favourite character was the caller.",
img_url = "https://image.tmdb.org/t/p/w500/tjrX2oWRCM3Tvarz38zlZM7Uc10.jpg"
)
add_movie(init_movie)
# ---------------------------------------------------------------------------------------
# FLASK APP, API, & DATABASE
# ---------------------------------------------------------------------------------------
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['form_token']
Bootstrap(app)
API = Movie_api()
app.config ['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///book-collection.db'
app.config ['SQLALCHEMY_TRACK_MODIFICATION'] = False
db = SQLAlchemy(app)
class Movies(db.Model) :
id = db.Column("id",db.Integer, primary_key = True)
title = db.Column("title",db.String(250), unique=True, nullable=False)
year = db.Column("year",db.Integer, nullable=False)
description = db.Column("description",db.String, nullable=False)
rating = db.Column("rating",db.Float, nullable=False)
ranking = db.Column("ranking",db.Integer, nullable=False)
review = db.Column("review",db.String(250), nullable=False)
img_url = db.Column("img_url",db.String, nullable=False)
def __repr__(self) :
return '<Movies {self.title}>'
with app.app_context() :
db.create_all()
first_movies(first_use = True)
# ---------------------------------------------------------------------------------------
# ROUTING FUNCTIONS
# ---------------------------------------------------------------------------------------
@app.route("/")
def home() :
update_rank()
all_movies = Movies.query.order_by(Movies.ranking).limit(10)
return render_template("index.html", movies = all_movies)
@app.route("/update", methods = ["GET", "POST"])
def update() :
movie_id = request.args.get('id')
find_movie = Movies.query.get(movie_id)
update_form = UpdateForm()
if request.method == "GET" :
update_form.rating.data = find_movie.rating
update_form.review.data = find_movie.review
elif update_form.validate_on_submit() :
find_movie.rating = update_form.rating.data
find_movie.review = update_form.review.data
db.session.commit()
return redirect("/")
movie_data = {}
movie_data["movie"] = find_movie
movie_data["form"] = update_form
return render_template("update.html", data = movie_data)
@app.route("/delete")
def delete() :
movie_id = request.args.get('id')
find_movie = Movies.query.get(movie_id)
db.session.delete(find_movie)
db.session.commit()
return redirect('/')
@app.route("/add", methods = ["GET", "POST"])
def add() :
add_form = AddForm()
if request.method == "POST" and add_form.validate_on_submit() :
movie_title = add_form.title.data
API.get_movie(movie_title)
movie_list = API.movie_list
data_send = {"path_img" : os.environ["url_img"], "movies" : movie_list}
return render_template("select.html", data = data_send)
return render_template("add.html", form = add_form)
@app.route("/select", methods = ["GET", "POST"])
def select() :
api_movie_list = API.movie_list
api_id = request.args.get('id')
find_movie = [m for m in api_movie_list if m["id"] == int(api_id)][0]
select_movie = Movies(
id = Movies.query.count() + 1,
title = find_movie["title"],
year = find_movie["release_date"][:4],
description = find_movie["overview"],
rating = find_movie["vote_average"],
ranking = 10,
review = find_movie["title"],
img_url = f'{ os.environ["url_img"] }{ find_movie["poster_path"] }'
)
add_movie(select_movie)
return redirect('/')
# ---------------------------------------------------------------------------------------
# HOST-PORT
# ---------------------------------------------------------------------------------------
if __name__ == "__main__" :
app.run(debug=True, host="0.0.0.0",port=2000)