diff --git a/plants/migrations/0007_plantcardphoto.py b/plants/migrations/0007_plantcardphoto.py new file mode 100644 index 0000000..d3008f7 --- /dev/null +++ b/plants/migrations/0007_plantcardphoto.py @@ -0,0 +1,26 @@ +# Generated by Django 5.2.1 on 2026-05-28 19:37 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('plants', '0006_species_vpc_raw'), + ] + + operations = [ + migrations.CreateModel( + name='PlantCardPhoto', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('image', models.ImageField(upload_to='plants/cards/')), + ('uploaded_at', models.DateTimeField(auto_now_add=True)), + ('plant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='card_photos', to='plants.plant')), + ], + options={ + 'ordering': ['uploaded_at'], + }, + ), + ] diff --git a/plants/models.py b/plants/models.py index 27feab4..234c3ec 100644 --- a/plants/models.py +++ b/plants/models.py @@ -82,6 +82,15 @@ class PlantPhoto(models.Model): ordering = ['uploaded_at'] +class PlantCardPhoto(models.Model): + plant = models.ForeignKey(Plant, on_delete=models.CASCADE, related_name='card_photos') + image = models.ImageField(upload_to='plants/cards/') + uploaded_at = models.DateTimeField(auto_now_add=True) + + class Meta: + ordering = ['uploaded_at'] + + class PruningLog(models.Model): plant = models.ForeignKey( Plant, on_delete=models.CASCADE, related_name='pruning_logs', diff --git a/plants/tests/test_models.py b/plants/tests/test_models.py index c58e5e5..60e262a 100644 --- a/plants/tests/test_models.py +++ b/plants/tests/test_models.py @@ -53,3 +53,31 @@ def test_delete_species_nulls_plant_species(): s.delete() p.refresh_from_db() assert p.species is None + + +@pytest.mark.django_db +def test_card_photo_belongs_to_plant(): + from plants.models import PlantCardPhoto + plant = Plant.objects.create(name='Test') + photo = PlantCardPhoto.objects.create(plant=plant, image='plants/cards/test.jpg') + assert photo.plant == plant + + +@pytest.mark.django_db +def test_card_photos_ordered_by_uploaded_at(): + from plants.models import PlantCardPhoto + plant = Plant.objects.create(name='Test') + p1 = PlantCardPhoto.objects.create(plant=plant, image='plants/cards/a.jpg') + p2 = PlantCardPhoto.objects.create(plant=plant, image='plants/cards/b.jpg') + qs = list(PlantCardPhoto.objects.filter(plant=plant)) + assert qs[0] == p1 + assert qs[1] == p2 + + +@pytest.mark.django_db +def test_card_photos_deleted_with_plant(): + from plants.models import PlantCardPhoto + plant = Plant.objects.create(name='Test') + PlantCardPhoto.objects.create(plant=plant, image='plants/cards/test.jpg') + plant.delete() + assert PlantCardPhoto.objects.count() == 0