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 <noreply@anthropic.com>
This commit is contained in:
Stephan Kerkman 2026-05-30 22:03:33 +02:00
parent bee9fe0cd8
commit d1e588bbc3

View file

@ -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)