feat: save relative image path in the database when adding a game

This commit is contained in:
Manuel Weiser 2024-09-03 11:58:21 +02:00
parent 1d04d0d941
commit e28d6660ae

View File

@ -31,14 +31,15 @@ def add_game():
image_url = data['image'] image_url = data['image']
image_response = requests.get(image_url) image_response = requests.get(image_url)
if image_response.status_code == 200: if image_response.status_code == 200:
image_filename = os.path.join(app.instance_path, 'game_images', f"{data['title'].replace(' ', '_')}.jpg") image_filename = f"game_images/{data['title'].replace(' ', '_')}.jpg" # Relative path
with open(image_filename, 'wb') as f: local_image_path = os.path.join(app.instance_path, image_filename)
with open(local_image_path, 'wb') as f:
f.write(image_response.content) f.write(image_response.content)
else: else:
return jsonify({'message': 'Image could not be downloaded!'}), 400 return jsonify({'message': 'Image could not be downloaded!'}), 400
new_game = Game( 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'], title=data['title'],
date=datetime.now().strftime('%Y-%m-%d'), # Set current date date=datetime.now().strftime('%Y-%m-%d'), # Set current date
buyer=buyer_username, # Set buyer as the authenticated user's username buyer=buyer_username, # Set buyer as the authenticated user's username