This commit is contained in:
Ross
2025-12-06 18:36:14 +00:00
parent 0e81fe2235
commit f33a7705d1
5 changed files with 53 additions and 0 deletions
@@ -0,0 +1,20 @@
<div class="column is-3-desktop is-4-tablet is-6-mobile" id="background-{{ bg.pk }}">
<article class="box">
<form method="post" action="{% url 'cards:background_edit' bg.pk %}" enctype="multipart/form-data" class="background-title-form" hx-post="{% url 'cards:background_edit' bg.pk %}" hx-target="#backgrounds-list" hx-swap="innerHTML">
{% csrf_token %}
<div style="height:140px; background-image:url('{{ bg.image.url }}'); background-size:cover; background-position:center; border-radius:4px;"></div>
<div style="margin-top:0.5rem">
<div class="field">
<div class="control">
{{ form.title }}
</div>
</div>
<div class="is-size-7">Uploaded {{ bg.uploaded_at }}</div>
<div class="buttons" style="margin-top:0.5rem">
<button class="button is-small is-primary" type="submit">Save</button>
<button type="button" class="button is-small" hx-get="{% url 'cards:backgrounds' %}" hx-target="#backgrounds-list" hx-swap="innerHTML">Cancel</button>
</div>
</div>
</form>
</article>
</div>
@@ -8,6 +8,7 @@
<strong>{{ bg.title }}</strong> <strong>{{ bg.title }}</strong>
<div class="is-size-7">Uploaded {{ bg.uploaded_at }}</div> <div class="is-size-7">Uploaded {{ bg.uploaded_at }}</div>
<div class="buttons" style="margin-top:0.5rem"> <div class="buttons" style="margin-top:0.5rem">
<button class="button is-small" hx-get="{% url 'cards:background_edit' bg.pk %}" hx-target="#background-{{ bg.pk }}" hx-swap="outerHTML">Edit</button>
{% if not bg.is_active %} {% if not bg.is_active %}
<button class="button is-small" hx-post="{% url 'cards:background_activate' bg.pk %}" hx-swap="outerHTML" hx-target="#backgrounds-list">Set active</button> <button class="button is-small" hx-post="{% url 'cards:background_activate' bg.pk %}" hx-swap="outerHTML" hx-target="#backgrounds-list">Set active</button>
{% else %} {% else %}
+1
View File
@@ -11,6 +11,7 @@ urlpatterns = [
path('preview/<int:pk>/', views.preview_card, name='preview'), path('preview/<int:pk>/', views.preview_card, name='preview'),
path('backgrounds/', views.background_list, name='backgrounds'), path('backgrounds/', views.background_list, name='backgrounds'),
path('backgrounds/create/', views.background_create, name='background_create'), path('backgrounds/create/', views.background_create, name='background_create'),
path('backgrounds/<int:pk>/edit/', views.background_edit, name='background_edit'),
path('backgrounds/download-all/', views.backgrounds_download_all, name='backgrounds_download_all'), path('backgrounds/download-all/', views.backgrounds_download_all, name='backgrounds_download_all'),
path('backgrounds/<int:pk>/activate/', views.background_activate, name='background_activate'), path('backgrounds/<int:pk>/activate/', views.background_activate, name='background_activate'),
path('backgrounds/<int:pk>/delete/', views.background_delete, name='background_delete'), path('backgrounds/<int:pk>/delete/', views.background_delete, name='background_delete'),
+31
View File
@@ -241,6 +241,37 @@ def background_delete(request, pk):
return redirect('cards:backgrounds') 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): def preview_card(request, pk):
"""Render a full preview of a card. HTMX loads the modal fragment into #modal-root.""" """Render a full preview of a card. HTMX loads the modal fragment into #modal-root."""
card = get_object_or_404(Dinosaur, pk=pk) card = get_object_or_404(Dinosaur, pk=pk)
BIN
View File
Binary file not shown.