feat: plant edit and delete with confirmation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Stephan Kerkman 2026-05-28 10:06:03 +02:00
parent 3bc27a345f
commit eb4dd82b88
3 changed files with 68 additions and 2 deletions

View file

@ -0,0 +1,17 @@
{% extends "plants/base.html" %}
{% block title %}Delete {{ plant.name }} — PlantDB{% endblock %}
{% block content %}
<div class="card mt-3">
<div class="card-body">
<h5 class="card-title">Delete "{{ plant.name }}"?</h5>
<p class="card-text text-muted">This will also delete all pruning history for this plant. This cannot be undone.</p>
<form method="POST">
{% csrf_token %}
<div class="d-flex gap-2">
<button type="submit" class="btn btn-danger">Yes, delete</button>
<a href="{% url 'plant_detail' plant.pk %}" class="btn btn-outline-secondary">Cancel</a>
</div>
</form>
</div>
</div>
{% endblock %}

View file

@ -182,3 +182,36 @@ class TestPlantAdd:
)
plant = Plant.objects.get(name='Garden Rose')
assert plant.species == s
@pytest.mark.django_db
class TestPlantEdit:
def test_edit_form_shows_current_values(self, client):
plant = Plant.objects.create(name='Fern', location='Office', pruning_months=[3])
resp = client.get(reverse('plant_edit', args=[plant.pk]))
assert resp.status_code == 200
assert resp.context['plant'] == plant
def test_post_updates_plant(self, client):
plant = Plant.objects.create(name='Fern', location='Office')
client.post(reverse('plant_edit', args=[plant.pk]), {
'name': 'Updated Fern',
'location': 'Bedroom',
'notes': '',
'pruning_months': [],
})
plant.refresh_from_db()
assert plant.name == 'Updated Fern'
@pytest.mark.django_db
class TestPlantDelete:
def test_delete_confirm_page(self, client):
plant = Plant.objects.create(name='Fern', location='Office')
resp = client.get(reverse('plant_delete', args=[plant.pk]))
assert resp.status_code == 200
def test_post_deletes_plant(self, client):
plant = Plant.objects.create(name='Fern', location='Office')
client.post(reverse('plant_delete', args=[plant.pk]))
assert not Plant.objects.filter(pk=plant.pk).exists()

View file

@ -88,8 +88,24 @@ def plant_add(request):
def plant_edit(request, pk):
pass
plant = get_object_or_404(Plant, pk=pk)
if request.method == 'POST':
form = PlantForm(request.POST, request.FILES, instance=plant)
if form.is_valid():
form.save()
return redirect('plant_detail', pk=plant.pk)
else:
form = PlantForm(instance=plant)
return render(request, 'plants/plant_form.html', {
'form': form,
'plant': plant,
'title': 'Edit Plant',
})
def plant_delete(request, pk):
pass
plant = get_object_or_404(Plant, pk=pk)
if request.method == 'POST':
plant.delete()
return redirect('plant_list')
return render(request, 'plants/plant_confirm_delete.html', {'plant': plant})