Skip to content

Commit 4ef4639

Browse files
committed
feat: add edit movie rating and reviews
1 parent 13cce54 commit 4ef4639

File tree

4 files changed

+111
-80
lines changed

4 files changed

+111
-80
lines changed

TopMovieList/app.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Flask, render_template, request, url_for, redirect
2+
from flask_bootstrap import Bootstrap
23
from flask_sqlalchemy import SQLAlchemy
34
from flask_wtf import FlaskForm
45
from wtforms import StringField, SubmitField
@@ -10,6 +11,8 @@
1011
# secret key
1112
app.secret_key = b'\xb9\xd6\xc7\xc4\xb5\xf0\xc4y\xc6\xb0\r\xc3`!\xca~'
1213

14+
Bootstrap(app)
15+
1316
# creating db
1417
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///movies.db'
1518
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
@@ -31,21 +34,12 @@ class Movie(db.Model):
3134
db.create_all()
3235

3336

34-
# Add movie data manually for checking
35-
#
36-
# new_movie = Movie(
37-
# title="Phone Booth",
38-
# year=2002,
39-
# description="Publicist Stuart Shepard finds himself trapped in a phone booth, pinned down by an extortionist's "
40-
# "sniper rifle. Unable to leave or receive outside help, Stuart's negotiation with the caller leads to a jaw-dropping climax.",
41-
# rating=7.3,
42-
# ranking=10,
43-
# review="My favourite character was the caller.",
44-
# img_url="https://image.tmdb.org/t/p/w500/tjrX2oWRCM3Tvarz38zlZM7Uc10.jpg"
45-
# )
46-
#
47-
# db.session.add(new_movie)
48-
# db.session.commit()
37+
# edit the rating and reviews
38+
39+
class RateMovieForm(FlaskForm):
40+
rating = StringField("Your Rating Out of 10 e.g. 7.5")
41+
review = StringField("Your Review")
42+
submit = SubmitField("Done")
4943

5044

5145
@app.route('/')
@@ -55,5 +49,22 @@ def home():
5549
return render_template('index.html', movies=all_movies)
5650

5751

52+
@app.route('/edit', methods=["GET", "POST"])
53+
def rate_movie():
54+
# inherit form the class
55+
form = RateMovieForm()
56+
# get the id
57+
movie_id = request.args.get('id')
58+
# check with db
59+
movie = Movie.query.get(movie_id)
60+
# if the id match then update
61+
if form.validate_on_submit():
62+
movie.rating = float(form.rating.data)
63+
movie.review = form.review.data
64+
db.session.commit()
65+
return redirect(url_for('home'))
66+
return render_template('edit.html', movie=movie, form=form)
67+
68+
5869
if __name__ == '__main__':
5970
app.run()

TopMovieList/templates/base.html

Lines changed: 0 additions & 63 deletions
This file was deleted.

TopMovieList/templates/edit.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{% extends 'bootstrap/base.html' %}
2+
{% import 'bootstrap/wtf.html' as wtf %}
3+
4+
{% block styles %}
5+
{{ 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+
/>
35+
{% endblock %}
36+
37+
{% block title %} Edit Rating & Reviews {% endblock %}
38+
39+
{% block content %}
40+
41+
<div class="content">
42+
<h1 class="heading">{{ movie.title }}</h1>
43+
<p class="description">Edit Movie Rating & Reviews</p>
44+
45+
{{ wtf.quick_form(form, novalidate=True) }}
46+
</div>
47+
48+
{% endblock %}

TopMovieList/templates/index.html

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
{# Base style #}
2-
{% extends 'base.html' %}
2+
{% extends 'bootstrap/base.html' %}
33

4+
{#Custom style #}
5+
6+
{% block styles %}
7+
{# inherit style from bootstrap #}
8+
{{ 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+
38+
{% endblock %}
439
{# page title #}
540

641
{% block title %} MY Top 10 Moives {% endblock %}
@@ -25,7 +60,7 @@ <h1 class="heading">My Top 10 Movies</h1>
2560
<p class="review">"{{ movie.review }}"</p>
2661
<p class="overview">{{ movie.description }}</p>
2762

28-
<a href="#" class="button">Update</a>
63+
<a href="{{ url_for('rate_movie', id=movie.id) }}" class="button">Update</a>
2964
<a href="#" class="button delete-button">Delete</a>
3065

3166
</div>

0 commit comments

Comments
 (0)