From 7118af5d3ce9e38e99f16906694b4579b5fa8ae0 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 12 Oct 2022 07:22:22 +0100 Subject: [PATCH] Implement managing hooks --- app/settings.py | 5 ++- app/urls.py | 5 +-- core/forms.py | 12 +++++- core/migrations/0003_hook.py | 25 ++++++++++++ core/models.py | 5 ++- core/templates/modals/add-hook.html | 28 ++++++++++++++ core/templates/partials/hook-list.html | 32 ++++++++++++++++ core/templates/window-content/hooks.html | 33 ++-------------- core/views/hooks.py | 49 +++++++++++++++++++++++- docker/prod/requirements.prod.txt | 2 +- 10 files changed, 157 insertions(+), 39 deletions(-) create mode 100644 core/migrations/0003_hook.py create mode 100644 core/templates/modals/add-hook.html create mode 100644 core/templates/partials/hook-list.html diff --git a/app/settings.py b/app/settings.py index dda8222..a6aac00 100644 --- a/app/settings.py +++ b/app/settings.py @@ -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 = [ diff --git a/app/urls.py b/app/urls.py index a5808f5..c8a92ac 100644 --- a/app/urls.py +++ b/app/urls.py @@ -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//", hooks.Hooks.as_view(), name="hooks"), + path("hooks/modal/add/", hooks.AddHook.as_view(), name="add-hook"), + path("hooks/modal/add//", hooks.AddHook.as_view(), name="add-hook"), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/forms.py b/core/forms.py index 5392800..f6fde84 100644 --- a/core/forms.py +++ b/core/forms.py @@ -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", + ) diff --git a/core/migrations/0003_hook.py b/core/migrations/0003_hook.py new file mode 100644 index 0000000..7ae5ae3 --- /dev/null +++ b/core/migrations/0003_hook.py @@ -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)), + ], + ), + ] diff --git a/core/models.py b/core/models.py index 0330953..f8dba39 100644 --- a/core/models.py +++ b/core/models.py @@ -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) diff --git a/core/templates/modals/add-hook.html b/core/templates/modals/add-hook.html new file mode 100644 index 0000000..bf1902d --- /dev/null +++ b/core/templates/modals/add-hook.html @@ -0,0 +1,28 @@ +{% extends 'wm/modal.html' %} +{% load crispy_forms_tags %} + +{% load crispy_forms_bulma_field %} +{% block modal_content %} +
+ {% csrf_token %} + {{ form|crispy }} + + +
+{% endblock %} + + + + + + + + diff --git a/core/templates/partials/hook-list.html b/core/templates/partials/hook-list.html new file mode 100644 index 0000000..f389743 --- /dev/null +++ b/core/templates/partials/hook-list.html @@ -0,0 +1,32 @@ +{% include 'partials/notify.html' %} + + + + + + + + + {% for item in hooks %} + + + + + + + {% endfor %} + +
userhookreceived hooksactions
{{ item.user }}{{ item.hook }}{{ item.received }} + +
diff --git a/core/templates/window-content/hooks.html b/core/templates/window-content/hooks.html index 02f4299..5135775 100644 --- a/core/templates/window-content/hooks.html +++ b/core/templates/window-content/hooks.html @@ -1,7 +1,7 @@
{% include 'partials/notify.html' %} - - - - - - - - {% for item in hooks %} - - - - - - - {% endfor %} +{% include 'partials/hook-list.html' %} +
userhookreceived hooksactions
{{ item.user }}{{ item.hook }}{{ item.received }} - -
diff --git a/core/views/hooks.py b/core/views/hooks.py index 61df3db..7fa5263 100644 --- a/core/views/hooks.py +++ b/core/views/hooks.py @@ -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) diff --git a/docker/prod/requirements.prod.txt b/docker/prod/requirements.prod.txt index 7768346..3a72239 100644 --- a/docker/prod/requirements.prod.txt +++ b/docker/prod/requirements.prod.txt @@ -1,7 +1,7 @@ wheel django pre-commit -django-crispy-forms +django-crispy-forms==1.14.0 crispy-bulma stripe django-rest-framework