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
|
||||
|
||||
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.orm import sessionmaker
|
||||
|
||||
from config.config import config
|
||||
from models.album import Base, Album
|
||||
|
||||
app = Flask(__name__)
|
||||
|
|
@ -26,37 +28,72 @@ def index():
|
|||
return render_template('index.html', albums=albums)
|
||||
|
||||
|
||||
@app.route('/admin', methods=['GET', 'POST'])
|
||||
def admin():
|
||||
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'))
|
||||
|
||||
@app.route('/admin', methods=['GET'])
|
||||
def admin_get():
|
||||
db = next(get_db())
|
||||
albums = db.query(Album).all()
|
||||
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__':
|
||||
app.run(debug=True)
|
||||
|
|
|
|||
|
|
@ -1,77 +1,60 @@
|
|||
<!-- templates/admin.html -->
|
||||
<!-- admin.html -->
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Admin Panel</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>
|
||||
<title>Admin</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Add Album</h1>
|
||||
<form method="POST">
|
||||
<label for="title">Title:</label>
|
||||
<input type="text" id="title" name="title" required><br><br>
|
||||
|
||||
<label for="artist">Artist:</label>
|
||||
<input type="text" id="artist" name="artist" required><br><br>
|
||||
|
||||
<label for="year">Year:</label>
|
||||
<input type="number" id="year" name="year" required><br><br>
|
||||
|
||||
<button type="submit">Add Album</button>
|
||||
</form>
|
||||
|
||||
<h1>Manage Albums</h1>
|
||||
<ul>
|
||||
<h1>Albums</h1>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Title</th>
|
||||
<th>Artist</th>
|
||||
<th>Year</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
{% for album in albums %}
|
||||
<li>
|
||||
{{ album.title }} by {{ album.artist }} ({{ album.year }})
|
||||
<form method="POST" style="display: inline;">
|
||||
<tr>
|
||||
<td>{{ album.id }}</td>
|
||||
<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="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>
|
||||
</form>
|
||||
<form method="POST" style="display: inline;">
|
||||
<form action="/admin" method="post">
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<input type="hidden" name="album_id" value="{{ album.id }}">
|
||||
<button type="submit">Delete</button>
|
||||
</form>
|
||||
</li>
|
||||
</td>
|
||||
</tr>
|
||||
{% 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>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Reference in New Issue