diff --git a/cards/templates/cards/_background_title_form.html b/cards/templates/cards/_background_title_form.html new file mode 100644 index 0000000..caddc76 --- /dev/null +++ b/cards/templates/cards/_background_title_form.html @@ -0,0 +1,20 @@ +
+
+
+ {% csrf_token %} +
+
+
+
+ {{ form.title }} +
+
+
Uploaded {{ bg.uploaded_at }}
+
+ + +
+
+
+
+
diff --git a/cards/templates/cards/_backgrounds_list.html b/cards/templates/cards/_backgrounds_list.html index d4690b1..6dec239 100644 --- a/cards/templates/cards/_backgrounds_list.html +++ b/cards/templates/cards/_backgrounds_list.html @@ -8,6 +8,7 @@ {{ bg.title }}
Uploaded {{ bg.uploaded_at }}
+ {% if not bg.is_active %} {% else %} diff --git a/cards/urls.py b/cards/urls.py index 75c397e..4c20092 100644 --- a/cards/urls.py +++ b/cards/urls.py @@ -11,6 +11,7 @@ urlpatterns = [ path('preview//', views.preview_card, name='preview'), path('backgrounds/', views.background_list, name='backgrounds'), path('backgrounds/create/', views.background_create, name='background_create'), + path('backgrounds//edit/', views.background_edit, name='background_edit'), path('backgrounds/download-all/', views.backgrounds_download_all, name='backgrounds_download_all'), path('backgrounds//activate/', views.background_activate, name='background_activate'), path('backgrounds//delete/', views.background_delete, name='background_delete'), diff --git a/cards/views.py b/cards/views.py index 8449c96..36bcf9b 100644 --- a/cards/views.py +++ b/cards/views.py @@ -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) diff --git a/db.sqlite3 b/db.sqlite3 index 2e40566..2d79712 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ