feat: add upload_card_photo view and URL

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Stephan Kerkman 2026-05-28 21:52:06 +02:00
parent a97bb77f32
commit 5e91983783
4 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1 @@
<div class="mb-3"></div>

View file

@ -241,6 +241,33 @@ class TestPlantCard:
assert 'plants/plant_card.html' in [t.name for t in resp.templates]
@pytest.mark.django_db
class TestUploadCardPhoto:
def test_upload_creates_card_photo(self, client):
from django.core.files.uploadedfile import SimpleUploadedFile
from plants.models import PlantCardPhoto
plant = Plant.objects.create(name='Fern')
image = SimpleUploadedFile('card.jpg', b'\xff\xd8\xff\xe0' + b'\x00' * 100, content_type='image/jpeg')
resp = client.post(reverse('upload_card_photo', args=[plant.pk]), {'image': image})
assert resp.status_code == 200
assert PlantCardPhoto.objects.filter(plant=plant).count() == 1
def test_upload_blocked_at_max_3(self, client):
from django.core.files.uploadedfile import SimpleUploadedFile
from plants.models import PlantCardPhoto
plant = Plant.objects.create(name='Fern')
for i in range(3):
PlantCardPhoto.objects.create(plant=plant, image=f'plants/cards/card{i}.jpg')
image = SimpleUploadedFile('card_extra.jpg', b'\xff\xd8\xff\xe0' + b'\x00' * 100, content_type='image/jpeg')
client.post(reverse('upload_card_photo', args=[plant.pk]), {'image': image})
assert PlantCardPhoto.objects.filter(plant=plant).count() == 3
def test_returns_card_gallery_partial(self, client):
plant = Plant.objects.create(name='Fern')
resp = client.post(reverse('upload_card_photo', args=[plant.pk]), {})
assert resp.templates[0].name == 'plants/partials/card_gallery.html'
@pytest.mark.django_db
class TestPruningCalendar:
def test_returns_200(self, client):

View file

@ -11,6 +11,7 @@ urlpatterns = [
path('plants/<int:pk>/card/', plants.plant_card, name='plant_card'),
path('plants/<int:pk>/log-pruning/', plants.log_pruning, name='log_pruning'),
path('plants/<int:pk>/upload-photo/', plants.upload_photo, name='upload_photo'),
path('plants/<int:pk>/card-photos/upload/', plants.upload_card_photo, name='upload_card_photo'),
path('photos/<int:photo_pk>/set-thumbnail/', plants.set_thumbnail, name='set_thumbnail'),
path('photos/<int:photo_pk>/delete/', plants.delete_photo, name='delete_photo'),
path('pruning/', pruning.pruning_calendar, name='pruning_calendar'),

View file

@ -122,6 +122,15 @@ def plant_card(request, pk):
return render(request, 'plants/plant_card.html', {'plant': plant})
@require_POST
def upload_card_photo(request, pk):
plant = get_object_or_404(Plant, pk=pk)
if request.FILES.get('image') and plant.card_photos.count() < 3:
PlantCardPhoto.objects.create(plant=plant, image=request.FILES['image'])
plant = Plant.objects.prefetch_related('card_photos').get(pk=pk)
return render(request, 'plants/partials/card_gallery.html', {'plant': plant})
@require_POST
def upload_photo(request, pk):
plant = get_object_or_404(Plant.objects.prefetch_related('photos'), pk=pk)