Make hooks/callbacks inherit type

This commit is contained in:
Mark Veidemanis 2022-10-16 14:14:53 +01:00
parent 3af63c5ef6
commit 97d12b0fab
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
14 changed files with 102 additions and 64 deletions

View File

@ -47,13 +47,19 @@ 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.HookAction.as_view(), name="hook_action"),
path("hooks/modal/add/<str:name>/", hooks.HookAction.as_view(), name="hook_action"),
path("hooks/<str:type>/add/", hooks.HookAction.as_view(), name="hook_action"),
path(
"hooks/page/del/<str:hook_id>/", hooks.HookAction.as_view(), name="hook_action"
"hooks/<str:type>/add/<str:name>/",
hooks.HookAction.as_view(),
name="hook_action",
),
path(
"hooks/modal/edit/<str:hook_id>/",
"hooks/<str:type>/del/<str:hook_id>/",
hooks.HookAction.as_view(),
name="hook_action",
),
path(
"hooks/<str:type>/edit/<str:hook_id>/",
hooks.HookAction.as_view(),
name="hook_action",
),

View File

@ -297,7 +297,7 @@
{% endblock %}
<div id="modals-here">
</div>
<div id="items-here">
<div id="windows-here">
</div>
<div id="widgets-here" style="display: none;">
</div>

View File

@ -1,32 +0,0 @@
{% extends 'wm/modal.html' %}
{% load crispy_forms_tags %}
{% load crispy_forms_bulma_field %}
{% block modal_content %}
<form
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
{% 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 %}
{{ form|crispy }}
<button
type="button"
class="button is-light modal-close-button">
Cancel
</button>
<button type="submit" class="button is-info modal-close-button">Submit</button>
</form>
{% endblock %}

View File

@ -0,0 +1 @@
<button class="modal-close is-large" aria-label="close"></button>

View File

@ -0,0 +1,3 @@
<i
class="fa-solid fa-xmark has-text-grey-light float-right"
onclick='grid.removeWidget("widget-{{ unique }}");'></i>

View File

@ -0,0 +1,3 @@
<i
class="fa-solid fa-xmark has-text-grey-light float-right"
data-script="on click remove the closest <nav/>"></i>

View File

@ -20,9 +20,9 @@
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'hook_action' hook_id=item.id %}"
hx-get="{% url 'hook_action' type=type hook_id=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-target="#{{ type }}s-here"
class="button is-info">
<span class="icon-text">
<span class="icon">
@ -32,7 +32,7 @@
</button>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{% url 'hook_action' hook_id=item.id %}"
hx-delete="{% url 'hook_action' type=type hook_id=item.id %}"
hx-trigger="click"
hx-target="#hooks-table"
class="button is-danger">
@ -42,7 +42,8 @@
</span>
</span>
</button>
<a href="{% url 'callbacks' type='{{ type }}' hook_id=item.id %}"><button
{% if type == 'page' %}
<a href="{% url 'callbacks' type=type hook_id=item.id %}"><button
class="button is-success">
<span class="icon-text">
<span class="icon">
@ -51,6 +52,20 @@
</span>
</button>
</a>
{% else %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'callbacks' type=type hook_id=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
class="button is-success">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-eye"></i>
</span>
</span>
</button>
{% endif %}
</div>
</td>
</tr>

View File

@ -0,0 +1,31 @@
{% include 'partials/notify.html' %}
{% load crispy_forms_tags %}
{% load crispy_forms_bulma_field %}
<form
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
{% 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 %}
{{ form|crispy }}
<button
type="button"
class="button is-light modal-close-button">
Cancel
</button>
<button type="submit" class="button is-info modal-close-button">Submit</button>
</form>

View File

@ -1,7 +1,7 @@
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'hook_action' %}"
hx-get="{% url 'hook_action' type=type %}"
hx-trigger="click"
hx-target="#modals-here"
class="button is-info">

View File

@ -37,7 +37,7 @@
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'hooks' type='window' %}"
hx-trigger="click"
hx-target="#items-here"
hx-target="#windows-here"
hx-swap="afterend"
class="button is-info">
<span class="icon-text">

View File

@ -14,7 +14,7 @@
{% block modal_content %}
{% include window_content %}
{% endblock %}
<button class="modal-close is-large" aria-label="close"></button>
{% include 'partials/close-modal.html' %}
</div>
</div>
</div>

View File

@ -3,9 +3,7 @@
<p class="panel-heading" style="padding: .2em; line-height: .5em;">
<i class="fa-solid fa-arrows-up-down-left-right has-text-grey-light"></i>
{% block close_button %}
<i
class="fa-solid fa-xmark has-text-grey-light float-right"
data-script="on click remove the closest <nav/>"></i>
{% include 'partials/close-window.html' %}
{% endblock %}
{% block heading %}
{% endblock %}

View File

@ -6,9 +6,7 @@
<p class="panel-heading" style="padding: .2em; line-height: .5em;">
<i class="fa-solid fa-arrows-up-down-left-right has-text-grey-light"></i>
{% block close_button %}
<i
class="fa-solid fa-xmark has-text-grey-light float-right"
onclick='grid.removeWidget("widget-{{ unique }}");'></i>
{% include 'partials/close-widget.html' %}
{% endblock %}
<i
class="fa-solid fa-arrows-minimize has-text-grey-light float-right"

View File

@ -92,19 +92,25 @@ class Hooks(LoginRequiredMixin, View):
"unique": unique,
"window_content": self.window_content,
"items": hooks,
"type": type,
}
return render(request, template_name, context)
class HookAction(LoginRequiredMixin, APIView):
template_name = "modals/add-hook.html"
allowed_types = ["modal", "widget", "window", "page"]
window_content = "window-content/add-hook.html"
parser_classes = [FormParser]
def get(self, request, hook_id=None):
def get(self, request, type, hook_id=None):
"""
Get the form for adding or editing a hook.
:param hook_id: The id of the hook to edit. Optional.
"""
if type not in self.allowed_types:
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
if hook_id:
try:
hook = Hook.objects.get(id=hook_id, user=request.user)
@ -115,15 +121,22 @@ class HookAction(LoginRequiredMixin, APIView):
context = {
"message": message,
"message_class": message_class,
"window_content": self.window_content,
}
return render(request, self.template_name, context)
return render(request, template_name, context)
else:
form = HookForm()
context = {"form": form, "hook_id": hook_id}
context = {
"form": form,
"hook_id": hook_id,
"type": type,
"unique": unique,
"window_content": self.window_content,
}
return render(request, self.template_name, context)
return render(request, template_name, context)
def put(self, request, hook_id=None):
def put(self, request, type, hook_id=None):
"""
Add or edit a hook.
:param hook_id: The id of the hook to edit. Optional.
@ -160,6 +173,7 @@ class HookAction(LoginRequiredMixin, APIView):
context = {
"items": hooks,
"type": type,
}
if message:
context["message"] = message
@ -167,7 +181,7 @@ class HookAction(LoginRequiredMixin, APIView):
template_name = "partials/hook-list.html"
return render(request, template_name, context)
def delete(self, request, hook_id):
def delete(self, request, type, hook_id):
"""
Delete a hook.
:param hook_id: The id of the hook to delete.
@ -186,6 +200,7 @@ class HookAction(LoginRequiredMixin, APIView):
context = {
"items": hooks,
"type": type,
}
if message:
context["message"] = message