bloombase/plants/views/locations.py
Stephan Kerkman c1d0f0a346 feat: add area management — list, add, edit, delete locations
Adds /locations/ page with full CRUD for plant areas. Edit redirects
to dedicated form; delete unlinks plants and shows confirm dialog if
area has plants. Gear icon in plant list filter bar links to management.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 15:17:36 +02:00

34 lines
1.1 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST
from django.db.models import Count
from plants.models import Location, Plant
def location_list(request):
if request.method == 'POST':
name = request.POST.get('name', '').strip()
if name:
Location.objects.get_or_create(name=name)
return redirect('location_list')
locations = Location.objects.annotate(plant_count=Count('plants'))
return render(request, 'plants/locations.html', {'locations': locations})
def location_edit(request, pk):
location = get_object_or_404(Location, pk=pk)
if request.method == 'POST':
name = request.POST.get('name', '').strip()
if name:
location.name = name
location.save()
return redirect('location_list')
return render(request, 'plants/location_edit.html', {'location': location})
@require_POST
def location_delete(request, pk):
location = get_object_or_404(Location, pk=pk)
Plant.objects.filter(location=location).update(location=None)
location.delete()
return redirect('location_list')