Skip to content

Commit

Permalink
Create, edit and delete item by form
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Dec 30, 2018
1 parent cfc08fa commit f7f7355
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
49 changes: 47 additions & 2 deletions app.py
Expand Up @@ -3,7 +3,7 @@
import sys

import click
from flask import Flask, render_template
from flask import Flask, render_template, request, url_for, redirect, flash
from flask_sqlalchemy import SQLAlchemy

# SQLite URI compatible
Expand All @@ -15,6 +15,7 @@


app = Flask(__name__)
app.config['SECRET_KEY'] = 'dev'
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + os.path.join(app.root_path, 'data.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

Expand Down Expand Up @@ -82,7 +83,51 @@ def page_not_found(e):
return render_template('404.html'), 404


@app.route('/')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
title = request.form['title']
year = request.form['year']

if not title or not year or len(year) > 4:
flash('Invalid input.')
return redirect(url_for('index'))

movie = Movie(title=title, year=year)
db.session.add(movie)
db.session.commit()
flash('Item Created!')
return redirect(url_for('index'))

movies = Movie.query.all()
return render_template('index.html', movies=movies)


@app.route('/movie/edit/<int:movie_id>', methods=['GET', 'POST'])
def edit(movie_id):
movie = Movie.query.get_or_404(movie_id)

if request.method == 'POST':
title = request.form['title']
year = request.form['year']

if not title or not year or len(year) > 4:
flash('Invalid input.')
return redirect(url_for('edit', movie_id=movie_id))

movie.title = title
movie.year = year
db.session.commit()
flash('Item Updated.')
return redirect(url_for('index'))

return render_template('edit.html', movie=movie)


@app.route('/movie/delete/<int:movie_id>', methods=['POST'])
def delete(movie_id):
movie = Movie.query.get_or_404(movie_id)
db.session.delete(movie)
db.session.commit()
flash('Item Deleted.')
return redirect(url_for('index'))
41 changes: 41 additions & 0 deletions static/style.css
Expand Up @@ -33,6 +33,43 @@ nav li a:hover {
background-color: #111;
}

.alert {
position: relative;
padding: 7px;
margin: 7px 0;
border: 1px solid transparent;
color: #004085;
background-color: #cce5ff;
border-color: #b8daff;
border-radius: 5px;
}

input[type=text] {
border: 1px solid #ddd;
}

input[name=year] {
width: 50px;
}

.btn {
font-size: 12px;
padding: 3px 5px;
text-decoration: none;
cursor: pointer;
background-color: white;
color: black;
border: 1px solid #555555;
border-radius: 5px;
}

.btn:hover {
text-decoration: none;
background-color: black;
color: white;
border: 1px solid black;
}

.movie-list {
list-style-type: none;
padding: 0;
Expand All @@ -57,6 +94,10 @@ nav li a:hover {
float: right;
}

.inline-form {
display: inline;
}

.imdb {
font-size: 12px;
font-weight: bold;
Expand Down
4 changes: 3 additions & 1 deletion templates/base.html
Expand Up @@ -10,7 +10,9 @@
{% endblock %}
</head>
<body>

{% for message in get_flashed_messages() %}
<div class="alert">{{ message }}</div>
{% endfor %}
<h2>
<img alt="Avatar" class="avatar" src="{{ url_for('static', filename='images/avatar.png') }}">
{{ user.name }}'s Watchlist
Expand Down
10 changes: 10 additions & 0 deletions templates/edit.html
@@ -0,0 +1,10 @@
{% extends 'base.html' %}

{% block content %}
<h3>Edit item</h3>
<form method="post">
Name <input type="text" name="title" autocomplete="off" required value="{{ movie.title }}">
Year <input type="text" name="year" autocomplete="off" required value="{{ movie.year }}">
<input class="btn" type="submit" name="submit" value="Update">
</form>
{% endblock %}
9 changes: 9 additions & 0 deletions templates/index.html
Expand Up @@ -2,10 +2,19 @@

{% block content %}
<p>{{ movies|length }} Titles</p>
<form method="post">
Name <input type="text" name="title" autocomplete="off" required>
Year <input type="text" name="year" autocomplete="off" required>
<input class="btn" type="submit" name="submit" value="Add">
</form>
<ul class="movie-list">
{% for movie in movies %}
<li>{{ movie.title }} - {{ movie.year }}
<span class="float-right">
<a class="btn" href="{{ url_for('edit', movie_id=movie.id) }}">Edit</a>
<form class="inline-form" method="post" action="{{ url_for('.delete', movie_id=movie.id) }}">
<input class="btn" type="submit" name="delete" value="Delete" onclick="return confirm('Are you sure?')">
</form>
<a class="imdb" href="https://www.imdb.com/find?q={{ movie.title }}" target="_blank" title="Find this movie on IMDb">IMDb</a>
</span>
</li>
Expand Down

0 comments on commit f7f7355

Please sign in to comment.