From dcd60b567bab01d88d2bb44f96cd997a8bde1987 Mon Sep 17 00:00:00 2001 From: christophermullins Date: Fri, 30 May 2025 15:42:08 -0700 Subject: [PATCH] Everything works, need to make template pages for each error that redirect back to admin page. Need to fix style of pages/templates. --- app.py | 30 ++++++++++++++++-------------- templates/admin.html | 5 ++++- templates/index.html | 39 ++++++++++++++------------------------- templates/style.css | 19 +++++++++++++++++++ 4 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 templates/style.css diff --git a/app.py b/app.py index cfba79e..d04dff6 100644 --- a/app.py +++ b/app.py @@ -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: - album_id = int(album_id) + 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) diff --git a/templates/admin.html b/templates/admin.html index b261738..1d2b133 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -5,10 +5,13 @@ Admin + Music Album Catalog Admin Page + +

Albums

- +
diff --git a/templates/index.html b/templates/index.html index 2dcfb09..021dd4a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,35 +5,24 @@ Music Album Catalog - +

Albums

-
ID Title
+ + + + + {% for album in albums %} -
  • {{ album.title }} by {{ album.artist }} ({{ album.year }})
  • + + + + + {% endfor %} - +
    AlbumArtistYear
    {{ album.title }} {{ album.artist }} ({{ album.year }})
    diff --git a/templates/style.css b/templates/style.css new file mode 100644 index 0000000..2d43dca --- /dev/null +++ b/templates/style.css @@ -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;} \ No newline at end of file