Implement adding/editing hooks

This commit is contained in:
Mark Veidemanis 2022-10-15 18:45:25 +01:00
parent 7118af5d3c
commit 8369f44bd4
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 119 additions and 30 deletions

View File

@ -42,6 +42,12 @@ urlpatterns = [
path("accounts/", include("django.contrib.auth.urls")), path("accounts/", include("django.contrib.auth.urls")),
path("accounts/signup/", base.Signup.as_view(), name="signup"), path("accounts/signup/", base.Signup.as_view(), name="signup"),
path("hooks/<str:type>/", hooks.Hooks.as_view(), name="hooks"), path("hooks/<str:type>/", hooks.Hooks.as_view(), name="hooks"),
path("hooks/modal/add/", hooks.AddHook.as_view(), name="add-hook"), path("hooks/modal/add/", hooks.HookAction.as_view(), name="hook_action"),
path("hooks/modal/add/<str:name>/", hooks.AddHook.as_view(), name="add-hook"), path("hooks/modal/add/<str:name>/", hooks.HookAction.as_view(), name="hook_action"),
path(
"hooks/page/del/<str:hook_id>/", hooks.HookAction.as_view(), name="hook_action"
),
path(
"hooks/page/edit/<str:hook_id>/", hooks.HookAction.as_view(), name="hook_action"
),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -1,8 +1,8 @@
# Generated by Django 4.1.2 on 2022-10-14 23:15 # Generated by Django 4.1.2 on 2022-10-14 23:15
import django.db.models.deletion
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -5,7 +5,11 @@
{% block modal_content %} {% block modal_content %}
<form <form
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-put="{% url 'add-hook' %}" {% if hook_id is not None %}
hx-put="{% url 'hook_action' hook_id %}"
{% else %}
hx-put="{% url 'hook_action' %}"
{% endif %}
hx-target="#hooks-table" hx-target="#hooks-table"
hx-swap="outerHTML"> hx-swap="outerHTML">
{% csrf_token %} {% csrf_token %}

View File

@ -2,20 +2,25 @@
<table class="table is-fullwidth is-hoverable" id="hooks-table"> <table class="table is-fullwidth is-hoverable" id="hooks-table">
<thead> <thead>
<th>id</th>
<th>user</th> <th>user</th>
<th>name</th>
<th>hook</th> <th>hook</th>
<th>received hooks</th> <th>received hooks</th>
<th>actions</th> <th>actions</th>
</thead> </thead>
{% for item in hooks %} {% for item in hooks %}
<tr> <tr>
<td>{{ item.id }}</td>
<td>{{ item.user }}</td> <td>{{ item.user }}</td>
<td>{{ item.name }}</td>
<td>{{ item.hook }}</td> <td>{{ item.hook }}</td>
<td>{{ item.received }}</td> <td>{{ item.received }}</td>
<td> <td>
<div class="buttons">
<button <button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="#" hx-get="{% url 'hook_action' hook_id=item.id %}"
hx-trigger="click" hx-trigger="click"
hx-target="#modals-here" hx-target="#modals-here"
class="button is-info"> class="button is-info">
@ -25,6 +30,19 @@
</span> </span>
</span> </span>
</button> </button>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{% url 'hook_action' hook_id=item.id %}"
hx-trigger="click"
hx-target="#hooks-table"
class="button is-danger">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-trash"></i>
</span>
</span>
</button>
</div>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -1,7 +1,7 @@
<div class="buttons"> <div class="buttons">
<button <button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'add-hook' %}" hx-get="{% url 'hook_action' %}"
hx-trigger="click" hx-trigger="click"
hx-target="#modals-here" hx-target="#modals-here"
class="button is-info"> class="button is-info">
@ -17,4 +17,3 @@
{% include 'partials/notify.html' %} {% include 'partials/notify.html' %}
{% include 'partials/hook-list.html' %} {% include 'partials/hook-list.html' %}
</table>

View File

@ -35,27 +35,62 @@ class Hooks(LoginRequiredMixin, View):
return render(request, template_name, context) return render(request, template_name, context)
class AddHook(LoginRequiredMixin, APIView): class HookAction(LoginRequiredMixin, APIView):
template_name = "modals/add-hook.html" template_name = "modals/add-hook.html"
parser_classes = [FormParser] parser_classes = [FormParser]
def get(self, request): def get(self, request, hook_id=None):
"""
Get the form for adding or editing a hook.
:param hook_id: The id of the hook to edit. Optional.
"""
if hook_id:
try:
hook = Hook.objects.get(id=hook_id, user=request.user)
form = HookForm(instance=hook)
except Hook.DoesNotExist:
message = "Hook does not exist"
message_class = "danger"
context = {
"message": message,
"message_class": message_class,
}
return render(request, self.template_name, context)
else:
form = HookForm() form = HookForm()
context = {"form": form} context = {"form": form, "hook_id": hook_id}
return render(request, self.template_name, context) return render(request, self.template_name, context)
def put(self, request): def put(self, request, hook_id=None):
"""
Add or edit a hook.
:param hook_id: The id of the hook to edit. Optional.
"""
message = None message = None
message_class = "success" message_class = "success"
# data = request.data if hook_id:
try:
form = HookForm(request.data, instance=Hook.objects.get(id=hook_id))
except Hook.DoesNotExist:
message = "Hook does not exist"
message_class = "danger"
context = {
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
else:
form = HookForm(request.data) form = HookForm(request.data)
if form.is_valid(): if form.is_valid():
hook = form.save(commit=False) hook = form.save(commit=False)
hook.user = request.user hook.user = request.user
hook.save() hook.save()
message = "Hook added successfully" if hook_id:
message = f"Hook {hook_id} edited successfully"
else:
message = f"Hook {hook.id} added successfully"
else: else:
message = "Error adding hook" message = "Error adding hook"
message_class = "danger" message_class = "danger"
@ -70,3 +105,30 @@ class AddHook(LoginRequiredMixin, APIView):
context["class"] = message_class context["class"] = message_class
template_name = "partials/hook-list.html" template_name = "partials/hook-list.html"
return render(request, template_name, context) return render(request, template_name, context)
def delete(self, request, hook_id):
"""
Delete a hook.
:param hook_id: The id of the hook to delete.
"""
message = None
message_class = "success"
try:
hook = Hook.objects.get(id=hook_id, user=request.user)
hook.delete()
message = "Hook deleted successfully"
except Hook.DoesNotExist:
message = "Error deleting hook"
message_class = "danger"
hooks = get_hooks(request.user)
context = {
"hooks": hooks,
}
if message:
context["message"] = message
context["class"] = message_class
template_name = "partials/hook-list.html"
return render(request, template_name, context)