.
This commit is contained in:
@@ -3,6 +3,7 @@ from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from django.template.loader import render_to_string
|
||||
from .models import Dinosaur, BackgroundImage
|
||||
from .forms import DinosaurForm
|
||||
from .forms import BackgroundImageForm
|
||||
|
||||
|
||||
def is_htmx(request):
|
||||
@@ -75,3 +76,59 @@ def card_delete(request, pk):
|
||||
return HttpResponse(status=204)
|
||||
return redirect('cards:list')
|
||||
return HttpResponseBadRequest('Only POST allowed')
|
||||
|
||||
|
||||
def background_list(request):
|
||||
"""Show existing backgrounds and provide a form to upload a new one."""
|
||||
backgrounds = BackgroundImage.objects.all()
|
||||
form = BackgroundImageForm()
|
||||
if is_htmx(request):
|
||||
return render(request, 'cards/_backgrounds_list.html', {'backgrounds': backgrounds, 'form': form})
|
||||
return render(request, 'cards/backgrounds_list.html', {'backgrounds': backgrounds, 'form': form})
|
||||
|
||||
|
||||
def background_create(request):
|
||||
if request.method != 'POST':
|
||||
return HttpResponseBadRequest('Only POST allowed')
|
||||
|
||||
form = BackgroundImageForm(request.POST, request.FILES)
|
||||
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):
|
||||
# return the updated list fragment and clear the form OOB
|
||||
list_html = render_to_string('cards/_backgrounds_list.html', {'backgrounds': BackgroundImage.objects.all(), 'form': BackgroundImageForm()}, request=request)
|
||||
oob = f'<div id="htmx-background-form" hx-swap-oob="true">{render_to_string("cards/_background_form.html", {"form": BackgroundImageForm()}, request=request)}</div>'
|
||||
return HttpResponse(list_html + oob)
|
||||
return redirect('cards:backgrounds')
|
||||
else:
|
||||
if is_htmx(request):
|
||||
form_html = render_to_string('cards/_background_form.html', {'form': form}, request=request)
|
||||
oob = f'<div id="htmx-background-form" hx-swap-oob="true">{form_html}</div>'
|
||||
return HttpResponse(oob)
|
||||
return render(request, 'cards/backgrounds_list.html', {'backgrounds': BackgroundImage.objects.all(), 'form': form})
|
||||
|
||||
|
||||
def background_activate(request, pk):
|
||||
if request.method != 'POST':
|
||||
return HttpResponseBadRequest('Only POST allowed')
|
||||
bg = get_object_or_404(BackgroundImage, pk=pk)
|
||||
BackgroundImage.objects.update(is_active=False)
|
||||
bg.is_active = True
|
||||
bg.save()
|
||||
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')
|
||||
|
||||
|
||||
def background_delete(request, pk):
|
||||
if request.method != 'POST':
|
||||
return HttpResponseBadRequest('Only POST allowed')
|
||||
bg = get_object_or_404(BackgroundImage, pk=pk)
|
||||
bg.delete()
|
||||
if is_htmx(request):
|
||||
return HttpResponse(status=204)
|
||||
return redirect('cards:backgrounds')
|
||||
|
||||
Reference in New Issue
Block a user