Everything works, need to make template pages for each error that redirect back to admin page.

Need to fix style of pages/templates.
This commit is contained in:
christophermullins 2025-05-30 15:42:08 -07:00
parent ace83ce5b9
commit dcd60b567b
4 changed files with 53 additions and 40 deletions

28
app.py
View File

@ -40,15 +40,14 @@ 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
if not action:
return redirect('/admin')
try:
if album_id is not None:
album_id = int(album_id)
except ValueError:
return jsonify({'error': 'Invalid album ID'}), 400
return redirect('/admin')
db = next(get_db())
@ -58,7 +57,7 @@ def admin_post():
year = request.form.get('year')
if not title or not artist or not year:
return jsonify({'error': 'All fields are required'}), 400
return redirect('/admin')
album = db.query(Album).filter(Album.id == album_id).first()
if album:
@ -66,18 +65,20 @@ def admin_post():
album.artist = artist
album.year = int(year)
db.commit()
return jsonify({'message': 'Album updated successfully', 'album': album.to_dict()})
return redirect('/admin')
else:
return jsonify({'error': 'Album not found'}), 404
return redirect('/admin')
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'})
return redirect('/admin')
else:
return jsonify({'error': 'Album not found'}), 404
return redirect('/admin')
elif action == 'add_album':
title = request.form.get('title')
@ -85,15 +86,16 @@ def admin_post():
year = request.form.get('year')
if not title or not artist or not year:
return jsonify({'error': 'All fields are required'}), 400
return redirect('/admin')
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()})
return redirect('/admin')
else:
return jsonify({'error': 'Invalid action'}), 400
return redirect('/admin')
if __name__ == '__main__':
app.run(debug=True)

View File

@ -5,10 +5,13 @@
<head>
<meta charset="UTF-8">
<title>Admin</title>
<title>Music Album Catalog Admin Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Albums</h1>
<table border="1">
<table>
<tr>
<th>ID</th>
<th>Title</th>

View File

@ -5,35 +5,24 @@
<head>
<meta charset="UTF-8">
<title>Music Album Catalog</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #f9f9f9;
border: 1px solid #ddd;
margin-bottom: 10px;
padding: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Albums</h1>
<ul>
<table>
<tr>
<th>Album</th>
<th>Artist</th>
<th>Year</th>
</tr>
{% for album in albums %}
<li>{{ album.title }} by {{ album.artist }} ({{ album.year }})</li>
<tr>
<td>{{ album.title }} </td>
<td> {{ album.artist }} </td>
<td> ({{ album.year }}) </td>
</tr>
{% endfor %}
</ul>
</table>
</body>
</html>

19
templates/style.css Normal file
View File

@ -0,0 +1,19 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
tr {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
}
tr:first-child tr {
background-color: #D6EEEE;
}
tr:hover {background-color: #D6EEEE;}