.
This commit is contained in:
@@ -241,6 +241,37 @@ def background_delete(request, pk):
|
||||
return redirect('cards:backgrounds')
|
||||
|
||||
|
||||
def background_edit(request, pk):
|
||||
"""Edit an existing BackgroundImage's metadata (title, image, is_active).
|
||||
|
||||
Designed to work with HTMX: GET returns a small edit form fragment which
|
||||
replaces the background card. POST saves and returns the updated
|
||||
backgrounds list fragment so the UI stays in sync.
|
||||
"""
|
||||
bg = get_object_or_404(BackgroundImage, pk=pk)
|
||||
if request.method == 'POST':
|
||||
form = BackgroundImageForm(request.POST, request.FILES, instance=bg)
|
||||
if form.is_valid():
|
||||
bg = form.save()
|
||||
# If marked active, unset others
|
||||
if bg.is_active:
|
||||
BackgroundImage.objects.exclude(pk=bg.pk).update(is_active=False)
|
||||
if is_htmx(request):
|
||||
list_html = render_to_string('cards/_backgrounds_list.html', {'backgrounds': BackgroundImage.objects.all(), 'form': BackgroundImageForm()}, request=request)
|
||||
return HttpResponse(list_html)
|
||||
return redirect('cards:backgrounds')
|
||||
else:
|
||||
if is_htmx(request):
|
||||
form_html = render_to_string('cards/_background_title_form.html', {'bg': bg, 'form': form}, request=request)
|
||||
return HttpResponse(form_html)
|
||||
return render(request, 'cards/backgrounds_list.html', {'backgrounds': BackgroundImage.objects.all(), 'form': form})
|
||||
else:
|
||||
form = BackgroundImageForm(instance=bg)
|
||||
if is_htmx(request):
|
||||
return render(request, 'cards/_background_title_form.html', {'bg': bg, 'form': form})
|
||||
return render(request, 'cards/backgrounds_list.html', {'backgrounds': BackgroundImage.objects.all(), 'form': form})
|
||||
|
||||
|
||||
def preview_card(request, pk):
|
||||
"""Render a full preview of a card. HTMX loads the modal fragment into #modal-root."""
|
||||
card = get_object_or_404(Dinosaur, pk=pk)
|
||||
|
||||
Reference in New Issue
Block a user