Skip to content

Commit 558e494

Browse files
committed
fix: displaying movie card
1 parent a3390ae commit 558e494

File tree

4 files changed

+69
-122
lines changed

4 files changed

+69
-122
lines changed

Diff for: TopMovieList/app.py

+46-34
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# CONSTANTS
1010

11-
MOVIE_DB_API_KEY = "YOUR API KEY"
11+
MOVIE_DB_API_KEY = "Your Own API key"
1212
MOVIE_DB_SEARCH_URL = "https://api.themoviedb.org/3/search/movie"
1313
MOVIE_DB_INFO_URL = "https://api.themoviedb.org/3/movie"
1414
MOVIE_DB_IMAGE_URL = "https://image.tmdb.org/t/p/w500"
@@ -26,7 +26,7 @@
2626
db = SQLAlchemy(app)
2727

2828

29-
# creating table
29+
##CREATE TABLE
3030
class Movie(db.Model):
3131
id = db.Column(db.Integer, primary_key=True)
3232
title = db.Column(db.String(250), unique=True, nullable=False)
@@ -41,64 +41,76 @@ class Movie(db.Model):
4141
db.create_all()
4242

4343

44-
# edit the rating and reviews
44+
class FindMovieForm(FlaskForm):
45+
title = StringField("Movie Title", validators=[DataRequired()])
46+
submit = SubmitField("Add Movie")
47+
4548

4649
class RateMovieForm(FlaskForm):
4750
rating = StringField("Your Rating Out of 10 e.g. 7.5")
4851
review = StringField("Your Review")
4952
submit = SubmitField("Done")
5053

5154

52-
# find movies from The Movie Database
53-
class FindMovieForm(FlaskForm):
54-
title = StringField('Movie Title', validators=[DataRequired()])
55-
submit = SubmitField("Add Movie")
55+
@app.route("/")
56+
def home():
57+
all_movies = Movie.query.order_by(Movie.rating).all()
58+
for i in range(len(all_movies)):
59+
all_movies[i].ranking = len(all_movies) - i
60+
db.session.commit()
61+
return render_template("index.html", movies=all_movies)
5662

5763

58-
@app.route('/')
59-
def home():
60-
# get all the movies from DB
61-
all_movies = Movie.query.all()
62-
return render_template('index.html', movies=all_movies)
64+
@app.route("/add", methods=["GET", "POST"])
65+
def add_movie():
66+
form = FindMovieForm()
67+
if form.validate_on_submit():
68+
movie_title = form.title.data
69+
70+
response = requests.get(MOVIE_DB_SEARCH_URL, params={"api_key": MOVIE_DB_API_KEY, "query": movie_title})
71+
data = response.json()["results"]
72+
return render_template("select.html", options=data)
73+
return render_template("add.html", form=form)
74+
75+
76+
@app.route("/find")
77+
def find_movie():
78+
movie_api_id = request.args.get("id")
79+
if movie_api_id:
80+
movie_api_url = f"{MOVIE_DB_INFO_URL}/{movie_api_id}"
81+
response = requests.get(movie_api_url, params={"api_key": MOVIE_DB_API_KEY, "language": "en-US"})
82+
data = response.json()
83+
new_movie = Movie(
84+
title=data["title"],
85+
year=data["release_date"].split("-")[0],
86+
img_url=f"{MOVIE_DB_IMAGE_URL}{data['poster_path']}",
87+
description=data["overview"]
88+
)
89+
db.session.add(new_movie)
90+
db.session.commit()
91+
return redirect(url_for("rate_movie", id=new_movie.id))
6392

6493

65-
@app.route('/edit', methods=["GET", "POST"])
94+
@app.route("/edit", methods=["GET", "POST"])
6695
def rate_movie():
67-
# inherit form the class
6896
form = RateMovieForm()
69-
# get the id
70-
movie_id = request.args.get('id')
71-
# check with db
97+
movie_id = request.args.get("id")
7298
movie = Movie.query.get(movie_id)
73-
# if the id match then update
7499
if form.validate_on_submit():
75100
movie.rating = float(form.rating.data)
76101
movie.review = form.review.data
77102
db.session.commit()
78103
return redirect(url_for('home'))
79-
return render_template('edit.html', movie=movie, form=form)
104+
return render_template("edit.html", movie=movie, form=form)
80105

81106

82-
@app.route('/delete')
107+
@app.route("/delete")
83108
def delete_movie():
84109
movie_id = request.args.get("id")
85110
movie = Movie.query.get(movie_id)
86111
db.session.delete(movie)
87112
db.session.commit()
88-
return redirect(url_for('home'))
89-
90-
91-
@app.route('/add', methods=["GET", "POST"])
92-
def add_movie():
93-
form = FindMovieForm()
94-
95-
# get information from The Movie database
96-
if form.validate_on_submit():
97-
movie_title = form.title.data
98-
response = requests.get(MOVIE_DB_SEARCH_URL, params={"api_key": MOVIE_DB_API_KEY, "query": movie_title})
99-
data = response.json()["results"]
100-
return render_template("select.html", options=data)
101-
return render_template("add.html", form=form)
113+
return redirect(url_for("home"))
102114

103115

104116
if __name__ == '__main__':

Diff for: TopMovieList/movies.db

0 Bytes
Binary file not shown.

Diff for: TopMovieList/templates/index.html

+12-39
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,25 @@
1-
{# Base style #}
21
{% extends 'bootstrap/base.html' %}
32

4-
{#Custom style #}
5-
63
{% block styles %}
7-
{# inherit style from bootstrap #}
84
{{ super() }}
9-
<!-- Google Fonts -->
10-
11-
<link
12-
rel="stylesheet"
13-
href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,700"
14-
/>
15-
<link
16-
rel="stylesheet"
17-
href="https://fonts.googleapis.com/css?family=Poppins:300,400,700"
18-
/>
19-
<link
20-
rel="stylesheet"
21-
href="https://fonts.googleapis.com/css?family=Poppins:300,400,700"
22-
/>
23-
24-
<!-- Font Awesome CSS -->
25-
<link
26-
rel="stylesheet"
27-
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
28-
integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
29-
crossorigin="anonymous"
30-
/>
31-
32-
<!-- Custom CSS Styling -->
33-
<link
34-
rel="stylesheet"
35-
href="{{ url_for('static', filename='css/styles.css') }}"
36-
/>
37-
5+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,700">
6+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
7+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
8+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"
9+
integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog=="
10+
crossorigin="anonymous"/>
11+
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
3812
{% endblock %}
39-
{# page title #}
4013

41-
{% block title %} MY Top 10 Moives {% endblock %}
14+
{% block title %}My Top 10 Movies{% endblock %}
4215

4316
{% block content %}
44-
4517
<div class="container">
4618
<h1 class="heading">My Top 10 Movies</h1>
4719
<p class="description">These are my all time favourite movies.</p>
20+
<div class="container text-center add">
21+
<a href="{{ url_for('add_movie') }}" class="button">Add Movie</a>
22+
</div>
4823
{% for movie in movies %}
4924
<div class="card">
5025
<div class="front" style="background-image: url('{{ movie.img_url }}');">
@@ -68,8 +43,6 @@ <h1 class="heading">My Top 10 Movies</h1>
6843
</div>
6944
{% endfor %}
7045
</div>
71-
<div class="container text-center add">
72-
<a href="{{ url_for('add_movie') }}" class="button">Add Movie</a>
73-
</div>
46+
7447

7548
{% endblock %}

Diff for: TopMovieList/templates/select.html

+11-49
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,23 @@
11
{% extends 'bootstrap/base.html' %}
2-
{% import 'bootstrap/wtf.html' as wtf %}
2+
{% import "bootstrap/wtf.html" as wtf %}
33

44
{% block styles %}
55
{{ super() }}
6-
7-
<!-- Google Fonts -->
8-
9-
<link
10-
rel="stylesheet"
11-
href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,700"
12-
/>
13-
<link
14-
rel="stylesheet"
15-
href="https://fonts.googleapis.com/css?family=Poppins:300,400,700"
16-
/>
17-
<link
18-
rel="stylesheet"
19-
href="https://fonts.googleapis.com/css?family=Poppins:300,400,700"
20-
/>
21-
22-
<!-- Font Awesome CSS -->
23-
<link
24-
rel="stylesheet"
25-
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
26-
integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
27-
crossorigin="anonymous"
28-
/>
29-
30-
<!-- Custom CSS Styling -->
31-
<link
32-
rel="stylesheet"
33-
href="{{ url_for('static', filename='css/styles.css') }}"
34-
/>
6+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,700">
7+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
8+
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
359
{% endblock %}
3610

37-
{% block title %} Select Movies {% endblock %}
11+
{% block title %}Select Movie{% endblock %}
3812

3913
{% block content %}
4014
<div class="container">
41-
42-
<div class="content">
43-
<h1 class="heading">Select Movie</h1>
44-
45-
46-
<div class="card" style="width: 18rem;">
47-
<div class="card-header">
48-
Select A Movie
49-
</div>
50-
{% for movie in options: %}
51-
<ul class="list-group list-group-flush">
52-
<li class="list-group-item">{{ movie.title }} | ({{ movie.release_date }})</li>
53-
</ul>
54-
{% endfor %}
55-
</div>
56-
57-
58-
</div>
15+
<h1 class="heading">Select Movie</h1>
16+
{% for movie in options: %}
17+
<p>
18+
<a href="{{ url_for('find_movie', id=movie.id) }} ">{{ movie.title }} - {{ movie.release_date }}</a>
19+
</p>
20+
{% endfor %}
5921

6022
</div>
6123
{% endblock %}

0 commit comments

Comments
 (0)