From e5226e299e61d3eac4b2a2c945bd67f4ac680bc2 Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Tue, 20 May 2025 15:14:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20New=20stub=20for=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/stub.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/app/stub.py b/app/stub.py index b7bb22d..20e135e 100644 --- a/app/stub.py +++ b/app/stub.py @@ -2,14 +2,38 @@ import pymongo from datetime import datetime from config import MONGODB_URL, MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE from utils import get_password_hash +from PIL import Image +import io +import os +import uuid client = pymongo.MongoClient(MONGODB_URL, username=MONGODB_USERNAME, password=MONGODB_PASSWORD) db = client[MONGODB_DATABASE] +# Créer le dossier pour stocker les images s'il n'existe pas +IMAGES_DIR = "images" +if not os.path.exists(IMAGES_DIR): + os.makedirs(IMAGES_DIR) + +def create_test_image(color='red', size=(100, 100)): + img = Image.new('RGB', size, color=color) + img_byte_arr = io.BytesIO() + img.save(img_byte_arr, format='JPEG') + img_byte_arr.seek(0) + return img_byte_arr + +def save_image(image_data, filename): + filepath = os.path.join(IMAGES_DIR, filename) + with open(filepath, 'wb') as f: + f.write(image_data.getvalue()) + return filepath + def populate_data(): users_collection = db["users"] pins_collection = db["pins"] + images_collection = db["images"] + # Créer les utilisateurs user1_id = users_collection.insert_one({ "username": "string", "password": get_password_hash("string") @@ -20,33 +44,79 @@ def populate_data(): "password": get_password_hash("test") }).inserted_id + # Créer les images de test + image_a = create_test_image(color='red') + image_b = create_test_image(color='blue') + image_x = create_test_image(color='green') + image_y = create_test_image(color='yellow') + + # Sauvegarder les images dans le système de fichiers + image_a_path = save_image(image_a, f"{uuid.uuid4()}.jpg") + image_b_path = save_image(image_b, f"{uuid.uuid4()}.jpg") + image_x_path = save_image(image_x, f"{uuid.uuid4()}.jpg") + image_y_path = save_image(image_y, f"{uuid.uuid4()}.jpg") + + # Insérer les métadonnées des images dans la base de données + image_a_id = images_collection.insert_one({ + "path": image_a_path, + "content_type": "image/jpeg", + "exif_date": datetime.now(), + "caption": "Tour Eiffel", + "user_id": str(user1_id) + }).inserted_id + + image_b_id = images_collection.insert_one({ + "path": image_b_path, + "content_type": "image/jpeg", + "exif_date": datetime.now(), + "caption": "Mont St Michel", + "user_id": str(user1_id) + }).inserted_id + + image_x_id = images_collection.insert_one({ + "path": image_x_path, + "content_type": "image/jpeg", + "exif_date": datetime.now(), + "caption": "Eiffel Tower", + "user_id": str(user2_id) + }).inserted_id + + image_y_id = images_collection.insert_one({ + "path": image_y_path, + "content_type": "image/jpeg", + "exif_date": datetime.now(), + "caption": "Mont Saint Michel", + "user_id": str(user2_id) + }).inserted_id + + # Créer les pins avec les vraies images pins_collection.insert_many([ { - "title": f"Tour Eiffel", + "title": "Tour Eiffel", "description": "Description A", "location": [48.858296, 2.294526], - "files": ["file_a"], + "files": [str(image_a_id)], "user_id": str(user1_id) }, { - "title": f"Mont St Michel", + "title": "Mont St Michel", "description": "Description B", "location": [48.636111, -1.511389], - "files": ["file_b"], + "files": [str(image_b_id)], "user_id": str(user1_id) }, { - "title": f"Eiffel Tower", + "title": "Eiffel Tower", "description": "Description X", "location": [48.858296, 2.294526], - "files": ["file_x"], + "files": [str(image_x_id)], "user_id": str(user2_id) }, { - "title": f"Mont Saint Michel", + "title": "Mont Saint Michel", "description": "Description Y", "location": [48.636111, -1.511389], - "files": ["file_y"], + "files": [str(image_y_id)], "user_id": str(user2_id) } ])