From e28d6660ae358ff983148a6c8f486a179e838cb5 Mon Sep 17 00:00:00 2001 From: "Manuel Weiser (aider)" Date: Tue, 3 Sep 2024 11:58:21 +0200 Subject: [PATCH] feat: save relative image path in the database when adding a game --- game_collection/app.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/game_collection/app.py b/game_collection/app.py index 0627a8c..e321a9d 100644 --- a/game_collection/app.py +++ b/game_collection/app.py @@ -31,14 +31,15 @@ def add_game(): image_url = data['image'] image_response = requests.get(image_url) if image_response.status_code == 200: - image_filename = os.path.join(app.instance_path, 'game_images', f"{data['title'].replace(' ', '_')}.jpg") - with open(image_filename, 'wb') as f: + image_filename = f"game_images/{data['title'].replace(' ', '_')}.jpg" # Relative path + local_image_path = os.path.join(app.instance_path, image_filename) + with open(local_image_path, 'wb') as f: f.write(image_response.content) else: return jsonify({'message': 'Image could not be downloaded!'}), 400 new_game = Game( - image=image_filename, # Save the local path to the image + image=image_filename, # Save the relative path to the image title=data['title'], date=datetime.now().strftime('%Y-%m-%d'), # Set current date buyer=buyer_username, # Set buyer as the authenticated user's username