Implement adding/editing hooks
This commit is contained in:
parent
7118af5d3c
commit
8369f44bd4
10
app/urls.py
10
app/urls.py
|
@ -42,6 +42,12 @@ urlpatterns = [
|
|||
path("accounts/", include("django.contrib.auth.urls")),
|
||||
path("accounts/signup/", base.Signup.as_view(), name="signup"),
|
||||
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/<str:name>/", 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.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)
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# Generated by Django 4.1.2 on 2022-10-14 23:15
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
|
|
@ -5,7 +5,11 @@
|
|||
{% block modal_content %}
|
||||
<form
|
||||
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-swap="outerHTML">
|
||||
{% csrf_token %}
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
{% include 'partials/notify.html' %}
|
||||
|
||||
<table class="table is-fullwidth is-hoverable" id="hooks-table">
|
||||
<thead>
|
||||
<th>user</th>
|
||||
<th>hook</th>
|
||||
<th>received hooks</th>
|
||||
<th>actions</th>
|
||||
</thead>
|
||||
{% for item in hooks %}
|
||||
<tr>
|
||||
<td>{{ item.user }}</td>
|
||||
<td>{{ item.hook }}</td>
|
||||
<td>{{ item.received }}</td>
|
||||
<td>
|
||||
<thead>
|
||||
<th>id</th>
|
||||
<th>user</th>
|
||||
<th>name</th>
|
||||
<th>hook</th>
|
||||
<th>received hooks</th>
|
||||
<th>actions</th>
|
||||
</thead>
|
||||
{% for item in hooks %}
|
||||
<tr>
|
||||
<td>{{ item.id }}</td>
|
||||
<td>{{ item.user }}</td>
|
||||
<td>{{ item.name }}</td>
|
||||
<td>{{ item.hook }}</td>
|
||||
<td>{{ item.received }}</td>
|
||||
<td>
|
||||
<div class="buttons">
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-get="#"
|
||||
hx-get="{% url 'hook_action' hook_id=item.id %}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
class="button is-info">
|
||||
|
@ -25,8 +30,21 @@
|
|||
</span>
|
||||
</span>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
<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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
</table>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<div class="buttons">
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-get="{% url 'add-hook' %}"
|
||||
hx-get="{% url 'hook_action' %}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
class="button is-info">
|
||||
|
@ -17,4 +17,3 @@
|
|||
{% include 'partials/notify.html' %}
|
||||
{% include 'partials/hook-list.html' %}
|
||||
|
||||
</table>
|
||||
|
|
|
@ -35,27 +35,62 @@ class Hooks(LoginRequiredMixin, View):
|
|||
return render(request, template_name, context)
|
||||
|
||||
|
||||
class AddHook(LoginRequiredMixin, APIView):
|
||||
class HookAction(LoginRequiredMixin, APIView):
|
||||
template_name = "modals/add-hook.html"
|
||||
parser_classes = [FormParser]
|
||||
|
||||
def get(self, request):
|
||||
form = HookForm()
|
||||
context = {"form": form}
|
||||
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()
|
||||
context = {"form": form, "hook_id": hook_id}
|
||||
|
||||
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_class = "success"
|
||||
|
||||
# data = request.data
|
||||
form = HookForm(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)
|
||||
if form.is_valid():
|
||||
hook = form.save(commit=False)
|
||||
hook.user = request.user
|
||||
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:
|
||||
message = "Error adding hook"
|
||||
message_class = "danger"
|
||||
|
@ -70,3 +105,30 @@ class AddHook(LoginRequiredMixin, APIView):
|
|||
context["class"] = message_class
|
||||
template_name = "partials/hook-list.html"
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue