-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete_row.py
49 lines (43 loc) · 1.69 KB
/
delete_row.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.http import HttpResponse
from django_components import component
from app.models import Contact
@component.register("delete_row")
class DeleteRowComponent(component.Component):
template = """
<table class="table">
<thead class="thead">
<tr>
<th class="td">Name</th>
<th class="td">Email</th>
<th class="td">Status</th>
<th class="td"></th>
</tr>
</thead>
<tbody id="tbody" hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML">
{% for contact in contacts %}
<tr class="tr {% if contact.id in ids %} {{ update }} {% endif %}">
<td class="td">{{ contact.first_name }} {{ contact.last_name }}</td>
<td class="td">{{ contact.email }}</td>
<td class="td">{{ contact.status }}</td>
<td class="td-tight">
<button class="btn-red-small" hx-delete="{% url 'contact_delete_row' id=contact.id %}">
Delete
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
"""
css = """
tr.htmx-swapping td {
opacity: 0;
transition: opacity 1s ease-out;
}
"""
def delete(self, request, id, *args, **kwargs):
delete_id = int(id)
Contact.objects.filter(id=delete_id).delete()
return HttpResponse(status=200)
def get_context_data(self, **kwargs):
return {"contacts": Contact.objects.all().order_by("id")[:5]} # remove limit