Implement managing hooks

This commit is contained in:
Mark Veidemanis 2022-10-12 07:22:22 +01:00
parent 83808dfa52
commit 7118af5d3c
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
10 changed files with 157 additions and 39 deletions

View File

@ -44,7 +44,10 @@ INSTALLED_APPS = [
# "django_tables2_bulma_template",
]
CRISPY_TEMPLATE_PACK = "bulma"
CRISPY_ALLOWED_TEMPLATE_PACKS = ("bulma",)
CRISPY_ALLOWED_TEMPLATE_PACKS = (
"uni_form",
"bulma",
)
DJANGO_TABLES2_TEMPLATE = "django-tables2/bulma.html"
MIDDLEWARE = [

View File

@ -41,8 +41,7 @@ urlpatterns = [
path("admin/", admin.site.urls),
path("accounts/", include("django.contrib.auth.urls")),
path("accounts/signup/", base.Signup.as_view(), name="signup"),
# path("demo/modal/", demo.DemoModal.as_view(), name="modal"),
# path("demo/widget/", demo.DemoWidget.as_view(), name="widget"),
# path("demo/window/", demo.DemoWindow.as_view(), name="window"),
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"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -1,7 +1,8 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.forms import ModelForm
from .models import User
from .models import Hook, User
# Create your forms here.
@ -32,3 +33,12 @@ class CustomUserCreationForm(UserCreationForm):
class Meta:
model = User
fields = "__all__"
class HookForm(ModelForm):
class Meta:
model = Hook
fields = (
"name",
"hook",
)

View File

@ -0,0 +1,25 @@
# Generated by Django 4.1.2 on 2022-10-14 23:15
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('core', '0002_session_session'),
]
operations = [
migrations.CreateModel(
name='Hook',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=1024, null=True)),
('hook', models.CharField(max_length=255)),
('received', models.IntegerField(default=0)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -75,8 +75,9 @@ class Session(models.Model):
class Hook(models.Model):
user = user = models.ForeignKey(User, on_delete=models.CASCADE)
hook = models.CharField(max_length=255, null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=1024, null=True, blank=True)
hook = models.CharField(max_length=255)
received = models.IntegerField(default=0)

View File

@ -0,0 +1,28 @@
{% extends 'wm/modal.html' %}
{% load crispy_forms_tags %}
{% load crispy_forms_bulma_field %}
{% block modal_content %}
<form
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-put="{% url 'add-hook' %}"
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,32 @@
{% 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>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="#"
hx-trigger="click"
hx-target="#modals-here"
class="button is-info">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-pencil"></i>
</span>
</span>
</button>
</td>
</tr>
{% endfor %}
</table>

View File

@ -1,7 +1,7 @@
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="#"
hx-get="{% url 'add-hook' %}"
hx-trigger="click"
hx-target="#modals-here"
class="button is-info">
@ -15,33 +15,6 @@
</div>
{% include 'partials/notify.html' %}
<table class="table is-fullwidth is-hoverable">
<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>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="#"
hx-trigger="click"
hx-target="#modals-here"
class="button is-info">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-pencil"></i>
</span>
<span>Hook</span>
</span>
</button>
</td>
</tr>
{% endfor %}
{% include 'partials/hook-list.html' %}
</table>

View File

@ -4,6 +4,16 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseBadRequest
from django.shortcuts import render
from django.views import View
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
from core.forms import HookForm
from core.models import Hook
def get_hooks(user):
hooks = Hook.objects.filter(user=user)
return hooks
class Hooks(LoginRequiredMixin, View):
@ -15,7 +25,7 @@ class Hooks(LoginRequiredMixin, View):
return HttpResponseBadRequest
template_name = f"wm/{type}.html"
unique = str(uuid.uuid4())[:8]
hooks = [{"user": "test", "hook": "/help22", "received": "40"}]
hooks = get_hooks(request.user)
context = {
"title": f"{type} Demo",
"unique": unique,
@ -23,3 +33,40 @@ class Hooks(LoginRequiredMixin, View):
"hooks": hooks,
}
return render(request, template_name, context)
class AddHook(LoginRequiredMixin, APIView):
template_name = "modals/add-hook.html"
parser_classes = [FormParser]
def get(self, request):
form = HookForm()
context = {"form": form}
return render(request, self.template_name, context)
def put(self, request):
message = None
message_class = "success"
# data = request.data
form = HookForm(request.data)
if form.is_valid():
hook = form.save(commit=False)
hook.user = request.user
hook.save()
message = "Hook added successfully"
else:
message = "Error adding 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)

View File

@ -1,7 +1,7 @@
wheel
django
pre-commit
django-crispy-forms
django-crispy-forms==1.14.0
crispy-bulma
stripe
django-rest-framework