From d1e588bbc3eda2ec3781ce955752eb45ba4fb2b5 Mon Sep 17 00:00:00 2001 From: Stephan Kerkman Date: Sat, 30 May 2026 22:03:33 +0200 Subject: [PATCH] feat: save identify photos as plant thumbnails after wizard completes Temp images uploaded during Pl@ntNet identification are now copied to PlantPhoto records when the plant is created in step 3. First image is auto-set as thumbnail. Temp files still deleted after transfer. Co-Authored-By: Claude Sonnet 4.6 --- plants/views/identify.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/plants/views/identify.py b/plants/views/identify.py index 1403eec..ddf0c50 100644 --- a/plants/views/identify.py +++ b/plants/views/identify.py @@ -1,4 +1,5 @@ import logging +import os import uuid from django.shortcuts import render, redirect from django.core.files.base import ContentFile @@ -6,7 +7,7 @@ from django.core.files.storage import default_storage from plants.services import plantnet from plants.services.plantnet import PlantNetError from plants.services.vpc import search_species as vpc_search -from plants.models import Plant, Species +from plants.models import Plant, PlantPhoto, Species logger = logging.getLogger(__name__) @@ -171,9 +172,23 @@ def identify_fields(request): plant = Plant.objects.create(name=name, species=species) try: - for p in request.session.get('identify_image_paths', []): - if default_storage.exists(p['path']): - default_storage.delete(p['path']) + image_paths = request.session.get('identify_image_paths', []) + for i, p in enumerate(image_paths): + if not default_storage.exists(p['path']): + continue + try: + with default_storage.open(p['path'], 'rb') as fh: + data = fh.read() + filename = os.path.basename(p['path']) + photo = PlantPhoto(plant=plant, is_thumbnail=(i == 0)) + photo.image.save(filename, ContentFile(data), save=True) + except Exception: + logger.exception('identify_fields: failed to save photo %s', p['path']) + finally: + try: + default_storage.delete(p['path']) + except Exception: + pass finally: _clear_session(request)