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:
parent
ace83ce5b9
commit
dcd60b567b
28
app.py
28
app.py
|
|
@ -40,15 +40,14 @@ def admin_post():
|
||||||
action = request.form.get('action')
|
action = request.form.get('action')
|
||||||
album_id = request.form.get('album_id')
|
album_id = request.form.get('album_id')
|
||||||
|
|
||||||
if not action or not album_id:
|
if not action:
|
||||||
if not album_id:
|
return redirect('/admin')
|
||||||
album_id = 1
|
|
||||||
#return jsonify({'error': 'Missing required fields'}), 400
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if album_id is not None:
|
||||||
album_id = int(album_id)
|
album_id = int(album_id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify({'error': 'Invalid album ID'}), 400
|
return redirect('/admin')
|
||||||
|
|
||||||
db = next(get_db())
|
db = next(get_db())
|
||||||
|
|
||||||
|
|
@ -58,7 +57,7 @@ def admin_post():
|
||||||
year = request.form.get('year')
|
year = request.form.get('year')
|
||||||
|
|
||||||
if not title or not artist or not 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()
|
album = db.query(Album).filter(Album.id == album_id).first()
|
||||||
if album:
|
if album:
|
||||||
|
|
@ -66,18 +65,20 @@ def admin_post():
|
||||||
album.artist = artist
|
album.artist = artist
|
||||||
album.year = int(year)
|
album.year = int(year)
|
||||||
db.commit()
|
db.commit()
|
||||||
return jsonify({'message': 'Album updated successfully', 'album': album.to_dict()})
|
return redirect('/admin')
|
||||||
else:
|
else:
|
||||||
return jsonify({'error': 'Album not found'}), 404
|
return redirect('/admin')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
elif action == 'delete':
|
elif action == 'delete':
|
||||||
album = db.query(Album).filter(Album.id == album_id).first()
|
album = db.query(Album).filter(Album.id == album_id).first()
|
||||||
if album:
|
if album:
|
||||||
db.delete(album)
|
db.delete(album)
|
||||||
db.commit()
|
db.commit()
|
||||||
return jsonify({'message': 'Album deleted successfully'})
|
return redirect('/admin')
|
||||||
else:
|
else:
|
||||||
return jsonify({'error': 'Album not found'}), 404
|
return redirect('/admin')
|
||||||
|
|
||||||
elif action == 'add_album':
|
elif action == 'add_album':
|
||||||
title = request.form.get('title')
|
title = request.form.get('title')
|
||||||
|
|
@ -85,15 +86,16 @@ def admin_post():
|
||||||
year = request.form.get('year')
|
year = request.form.get('year')
|
||||||
|
|
||||||
if not title or not artist or not 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))
|
new_album = Album(title=title, artist=artist, year=int(year))
|
||||||
db.add(new_album)
|
db.add(new_album)
|
||||||
db.commit()
|
db.commit()
|
||||||
return jsonify({'message': 'Album added successfully', 'album': new_album.to_dict()})
|
return redirect('/admin')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify({'error': 'Invalid action'}), 400
|
return redirect('/admin')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,13 @@
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Admin</title>
|
<title>Admin</title>
|
||||||
|
<title>Music Album Catalog Admin Page</title>
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h1>Albums</h1>
|
<h1>Albums</h1>
|
||||||
<table border="1">
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
<th>Title</th>
|
<th>Title</th>
|
||||||
|
|
|
||||||
|
|
@ -5,35 +5,24 @@
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Music Album Catalog</title>
|
<title>Music Album Catalog</title>
|
||||||
<style>
|
<link rel="stylesheet" href="style.css">
|
||||||
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>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Albums</h1>
|
<h1>Albums</h1>
|
||||||
<ul>
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Album</th>
|
||||||
|
<th>Artist</th>
|
||||||
|
<th>Year</th>
|
||||||
|
</tr>
|
||||||
{% for album in albums %}
|
{% 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 %}
|
{% endfor %}
|
||||||
</ul>
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -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;}
|
||||||
Loading…
Reference in New Issue