-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
103 lines (85 loc) · 3.3 KB
/
app.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
import os
import re
from flask import Flask, render_template, request, redirect, flash, url_for
from flask_sqlalchemy import SQLAlchemy
# Creating a Flask application object
# Setting up the URI of the database to be used
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(
os.path.join(project_dir, "moviesdatabase.db"))
app = Flask(__name__)
app.secret_key = 'random string'
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Creating an object of SQLAlchemy class with
# Flask application object as the parameter
db = SQLAlchemy(app)
class Movies(db.Model):
# Movies class inherited from db model
id = db.Column('id', db.Integer, primary_key=True)
title = db.Column(db.Text)
story = db.Column(db.Text)
poster_url = db.Column(db.Text)
youtube_url = db.Column(db.Text)
youtube_url_id = db.Column(db.Text)
def __init__(self, title, story, poster_url, youtube_url, youtube_url_id):
self.title = title
self.story = story
self.poster_url = poster_url
self.youtube_url = youtube_url
self.youtube_url_id = youtube_url_id
@app.route('/')
def movies():
'''
This method is used to retrieve movies
from movies database and render html
'''
return render_template('movies.html', movies=Movies.query.all())
@app.route('/trailer/<youtubeid>')
def trailer(youtubeid):
'''
This method is used to retrieve single movie
from movies database based on youtube trailer id
and render the html
'''
movie = None
movie = Movies.query.filter_by(youtube_url_id=youtubeid).first()
return render_template('single.html', movie=movie)
@app.route('/new', methods=['GET', 'POST'])
def addmovie():
'''
This method checks the http request, if the request method is post,
it validated the entered data
and saves the new movie to database and redirects to the movies page,
else if the request is get then it renders the newmovie html
'''
if request.method == 'POST':
if (not request.form['title'] or not request.form['story'] or not
request.form['poster_url'] or not
request.form['youtube_url']):
flash('Please enter all the fields', 'error')
else:
# Extract the youtube ID from the url
youtube_id_match = re.search(
r'(?<=v=)[^&#]+', request.form['youtube_url'])
youtube_id_match = youtube_id_match or re.search(
r'(?<=be/)[^&#]+', request.form['youtube_url'])
trailer_youtube_id = youtube_id_match.group(
0) if youtube_id_match else None
movie = Movies(request.form['title'], request.form['story'],
request.form['poster_url'],
request.form['youtube_url'],
trailer_youtube_id)
db.session.add(movie)
db.session.commit()
flash('Record was successfully added')
return redirect(url_for('movies'))
return render_template('newmovie.html')
if __name__ == "__main__":
'''
When the application runs db.create_all will check
for the database and creates
if no database is found
'''
db.create_all()
app.run(debug=True)