Second iteration. Adding albums works now, but throws an error.
This commit is contained in:
parent
6352aff937
commit
ace83ce5b9
93
app.py
93
app.py
|
|
@ -1,8 +1,10 @@
|
||||||
# app.py
|
# app.py
|
||||||
|
|
||||||
from flask import Flask, request, render_template, redirect, url_for
|
from flask import Flask, request, render_template, jsonify, redirect, url_for
|
||||||
from sqlalchemy import create_engine, func
|
from sqlalchemy import create_engine, func
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from config.config import config
|
||||||
from models.album import Base, Album
|
from models.album import Base, Album
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
@ -26,37 +28,72 @@ def index():
|
||||||
return render_template('index.html', albums=albums)
|
return render_template('index.html', albums=albums)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/admin', methods=['GET', 'POST'])
|
@app.route('/admin', methods=['GET'])
|
||||||
def admin():
|
def admin_get():
|
||||||
if request.method == 'POST':
|
|
||||||
action = request.form['action']
|
|
||||||
album_id = int(request.form['album_id'])
|
|
||||||
|
|
||||||
db = next(get_db())
|
|
||||||
|
|
||||||
if action == 'edit':
|
|
||||||
title = request.form['title']
|
|
||||||
artist = request.form['artist']
|
|
||||||
year = int(request.form['year'])
|
|
||||||
|
|
||||||
album = db.query(Album).filter(Album.id == album_id).first()
|
|
||||||
if album:
|
|
||||||
album.title = title
|
|
||||||
album.artist = artist
|
|
||||||
album.year = year
|
|
||||||
db.commit()
|
|
||||||
elif action == 'delete':
|
|
||||||
album = db.query(Album).filter(Album.id == album_id).first()
|
|
||||||
if album:
|
|
||||||
db.delete(album)
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
return redirect(url_for('admin'))
|
|
||||||
|
|
||||||
db = next(get_db())
|
db = next(get_db())
|
||||||
albums = db.query(Album).all()
|
albums = db.query(Album).all()
|
||||||
return render_template('admin.html', albums=albums)
|
return render_template('admin.html', albums=albums)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/admin', methods=['POST'])
|
||||||
|
def admin_post():
|
||||||
|
action = request.form.get('action')
|
||||||
|
album_id = request.form.get('album_id')
|
||||||
|
|
||||||
|
if not action or not album_id:
|
||||||
|
if not album_id:
|
||||||
|
album_id = 1
|
||||||
|
#return jsonify({'error': 'Missing required fields'}), 400
|
||||||
|
|
||||||
|
try:
|
||||||
|
album_id = int(album_id)
|
||||||
|
except ValueError:
|
||||||
|
return jsonify({'error': 'Invalid album ID'}), 400
|
||||||
|
|
||||||
|
db = next(get_db())
|
||||||
|
|
||||||
|
if action == 'edit':
|
||||||
|
title = request.form.get('title')
|
||||||
|
artist = request.form.get('artist')
|
||||||
|
year = request.form.get('year')
|
||||||
|
|
||||||
|
if not title or not artist or not year:
|
||||||
|
return jsonify({'error': 'All fields are required'}), 400
|
||||||
|
|
||||||
|
album = db.query(Album).filter(Album.id == album_id).first()
|
||||||
|
if album:
|
||||||
|
album.title = title
|
||||||
|
album.artist = artist
|
||||||
|
album.year = int(year)
|
||||||
|
db.commit()
|
||||||
|
return jsonify({'message': 'Album updated successfully', 'album': album.to_dict()})
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Album not found'}), 404
|
||||||
|
|
||||||
|
elif action == 'delete':
|
||||||
|
album = db.query(Album).filter(Album.id == album_id).first()
|
||||||
|
if album:
|
||||||
|
db.delete(album)
|
||||||
|
db.commit()
|
||||||
|
return jsonify({'message': 'Album deleted successfully'})
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Album not found'}), 404
|
||||||
|
|
||||||
|
elif action == 'add_album':
|
||||||
|
title = request.form.get('title')
|
||||||
|
artist = request.form.get('artist')
|
||||||
|
year = request.form.get('year')
|
||||||
|
|
||||||
|
if not title or not artist or not year:
|
||||||
|
return jsonify({'error': 'All fields are required'}), 400
|
||||||
|
|
||||||
|
new_album = Album(title=title, artist=artist, year=int(year))
|
||||||
|
db.add(new_album)
|
||||||
|
db.commit()
|
||||||
|
return jsonify({'message': 'Album added successfully', 'album': new_album.to_dict()})
|
||||||
|
|
||||||
|
else:
|
||||||
|
return jsonify({'error': 'Invalid action'}), 400
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
|
||||||
|
|
@ -1,77 +1,60 @@
|
||||||
<!-- templates/admin.html -->
|
<!-- admin.html -->
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Admin Panel</title>
|
<title>Admin</title>
|
||||||
<style>
|
|
||||||
body {
|
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
margin: 20px;
|
|
||||||
}
|
|
||||||
h1 {
|
|
||||||
color: #333;
|
|
||||||
}
|
|
||||||
form {
|
|
||||||
max-width: 400px;
|
|
||||||
margin: 0 auto;
|
|
||||||
}
|
|
||||||
label {
|
|
||||||
display: block;
|
|
||||||
margin-bottom: 5px;
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
input[type="text"], input[type="number"] {
|
|
||||||
width: 100%;
|
|
||||||
padding: 8px;
|
|
||||||
box-sizing: border-box;
|
|
||||||
margin-bottom: 15px;
|
|
||||||
}
|
|
||||||
button {
|
|
||||||
background-color: #4CAF50;
|
|
||||||
color: white;
|
|
||||||
padding: 10px 20px;
|
|
||||||
border: none;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
background-color: #45a049;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Add Album</h1>
|
<h1>Albums</h1>
|
||||||
<form method="POST">
|
<table border="1">
|
||||||
<label for="title">Title:</label>
|
<tr>
|
||||||
<input type="text" id="title" name="title" required><br><br>
|
<th>ID</th>
|
||||||
|
<th>Title</th>
|
||||||
<label for="artist">Artist:</label>
|
<th>Artist</th>
|
||||||
<input type="text" id="artist" name="artist" required><br><br>
|
<th>Year</th>
|
||||||
|
<th>Actions</th>
|
||||||
<label for="year">Year:</label>
|
</tr>
|
||||||
<input type="number" id="year" name="year" required><br><br>
|
|
||||||
|
|
||||||
<button type="submit">Add Album</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h1>Manage Albums</h1>
|
|
||||||
<ul>
|
|
||||||
{% for album in albums %}
|
{% for album in albums %}
|
||||||
<li>
|
<tr>
|
||||||
{{ album.title }} by {{ album.artist }} ({{ album.year }})
|
<td>{{ album.id }}</td>
|
||||||
<form method="POST" style="display: inline;">
|
<td>{{ album.title }}</td>
|
||||||
|
<td>{{ album.artist }}</td>
|
||||||
|
<td>{{ album.year }}</td>
|
||||||
|
<td>
|
||||||
|
<form action="/admin" method="post">
|
||||||
<input type="hidden" name="action" value="edit">
|
<input type="hidden" name="action" value="edit">
|
||||||
<input type="hidden" name="album_id" value="{{ album.id }}">
|
<input type="hidden" name="album_id" value="{{ album.id }}">
|
||||||
|
<input type="text" name="title" placeholder="New Title">
|
||||||
|
<input type="text" name="artist" placeholder="New Artist">
|
||||||
|
<input type="number" name="year" placeholder="New Year">
|
||||||
<button type="submit">Edit</button>
|
<button type="submit">Edit</button>
|
||||||
</form>
|
</form>
|
||||||
<form method="POST" style="display: inline;">
|
<form action="/admin" method="post">
|
||||||
<input type="hidden" name="action" value="delete">
|
<input type="hidden" name="action" value="delete">
|
||||||
<input type="hidden" name="album_id" value="{{ album.id }}">
|
<input type="hidden" name="album_id" value="{{ album.id }}">
|
||||||
<button type="submit">Delete</button>
|
<button type="submit">Delete</button>
|
||||||
</form>
|
</form>
|
||||||
</li>
|
</td>
|
||||||
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</table>
|
||||||
|
|
||||||
|
<!-- Form for adding a new album -->
|
||||||
|
<h2>Add New Album</h2>
|
||||||
|
<form action="/admin" method="post">
|
||||||
|
<input type="hidden" name="action" value="add_album">
|
||||||
|
<label for="title">Title:</label>
|
||||||
|
<input type="text" id="title" name="title" required>
|
||||||
|
<br>
|
||||||
|
<label for="artist">Artist:</label>
|
||||||
|
<input type="text" id="artist" name="artist" required>
|
||||||
|
<br>
|
||||||
|
<label for="year">Year:</label>
|
||||||
|
<input type="number" id="year" name="year" required>
|
||||||
|
<br>
|
||||||
|
<button type="submit">Add Album</button>
|
||||||
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue