-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (52 loc) · 1.73 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
import os
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(
os.path.join(project_dir, "moviedatabase.db"))
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
db = SQLAlchemy(app)
class Movie(db.Model):
title = db.Column(db.String(80), unique=True,
nullable=False, primary_key=True)
def __repr__(self):
return "<Title: {}>".format(self.title)
@app.route("/", methods=["GET", "POST"])
def home():
if request.form:
try:
movie = Movie(title=request.form.get("title"))
db.session.add(movie)
db.session.commit()
except Exception as e:
print("Failed to add movie")
print(e)
movies = Movie.query.all()
return render_template("home.html", movies=movies)
@app.route("/update", methods=["POST"])
def update():
try:
newtitle = request.form.get("newtitle")
oldtitle = request.form.get("oldtitle")
movie = Movie.query.filter_by(title=oldtitle).first()
movie.title = newtitle
db.session.commit()
except Exception as e:
print("Couldn't update movie title")
print(e)
return redirect("/")
@app.route("/delete", methods=["POST"])
def delete():
try:
title = request.form.get("title")
movie = Movie.query.filter_by(title=title).first()
db.session.delete(movie)
db.session.commit()
except Exception as e:
print("Couldn't delete movie title")
print(e)
return redirect("/")
if __name__ == "__main__":
app.run(debug=True)