From 2d8576ff9b8f234b4772f8c0d32a34c3226ad0bc Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sat, 25 Feb 2023 11:37:21 +0300 Subject: [PATCH] added form test question types --- akarpov/templates/test_platform/create.html | 26 ++++++++++ akarpov/templates/test_platform/edit.html | 10 ++++ akarpov/templates/test_platform/view.html | 10 ++++ akarpov/test_platform/forms.py | 7 +-- akarpov/test_platform/services/__init__.py | 0 akarpov/test_platform/services/forms.py | 28 +++++++++++ akarpov/test_platform/signals.py | 53 +++++++++++++++++++++ akarpov/test_platform/urls.py | 6 +++ akarpov/test_platform/views.py | 25 ++++++++++ config/urls.py | 1 + 10 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 akarpov/templates/test_platform/create.html create mode 100644 akarpov/templates/test_platform/edit.html create mode 100644 akarpov/templates/test_platform/view.html create mode 100644 akarpov/test_platform/services/__init__.py create mode 100644 akarpov/test_platform/services/forms.py create mode 100644 akarpov/test_platform/urls.py diff --git a/akarpov/templates/test_platform/create.html b/akarpov/templates/test_platform/create.html new file mode 100644 index 0000000..8fb047d --- /dev/null +++ b/akarpov/templates/test_platform/create.html @@ -0,0 +1,26 @@ +{% extends "base.html" %} +{% load static %} +{% load crispy_forms_tags %} + +{% block title %}Creating new Form on akarpov{% endblock %} + +{% block content %} +
+ {% csrf_token %} + {{ form.media }} + {% for field in form %} + {{ field|as_crispy_field }} + {% endfor %} +
+ +
+{% for question, question_form in questions.items %} +{{ question }} +{% for field in question_form %} + {{ field|as_crispy_field }} +{% endfor %} +{% endfor %} +
+{% endblock %} diff --git a/akarpov/templates/test_platform/edit.html b/akarpov/templates/test_platform/edit.html new file mode 100644 index 0000000..04e75bc --- /dev/null +++ b/akarpov/templates/test_platform/edit.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + diff --git a/akarpov/templates/test_platform/view.html b/akarpov/templates/test_platform/view.html new file mode 100644 index 0000000..04e75bc --- /dev/null +++ b/akarpov/templates/test_platform/view.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + diff --git a/akarpov/test_platform/forms.py b/akarpov/test_platform/forms.py index 9c67ad1..efb0f26 100644 --- a/akarpov/test_platform/forms.py +++ b/akarpov/test_platform/forms.py @@ -1,5 +1,3 @@ -from abc import ABC - from django import forms from akarpov.test_platform.models import ( @@ -14,12 +12,15 @@ class FormFormClass(forms.ModelForm): + time_since = forms.DateField(widget=forms.TextInput(attrs={"type": "date"})) + time_till = forms.DateField(widget=forms.TextInput(attrs={"type": "date"})) + class Meta: model = Form fields = ["name", "description", "public", "image", "time_since", "time_till"] -class BaseQuestionForm(forms.ModelForm, ABC): +class BaseQuestionForm(forms.ModelForm): class Meta: model = BaseQuestion fields = ["question", "help", "required"] diff --git a/akarpov/test_platform/services/__init__.py b/akarpov/test_platform/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/akarpov/test_platform/services/forms.py b/akarpov/test_platform/services/forms.py new file mode 100644 index 0000000..c28e7fc --- /dev/null +++ b/akarpov/test_platform/services/forms.py @@ -0,0 +1,28 @@ +from akarpov.test_platform.forms import ( + NumberQuestionForm, + NumberRangeQuestionForm, + SelectQuestionForm, + TextQuestionForm, +) +from akarpov.test_platform.models import ( + BaseQuestion, + NumberQuestion, + NumberRangeQuestion, + SelectQuestion, + TextQuestion, +) + +question_forms = { + TextQuestion: TextQuestionForm, + NumberQuestion: NumberQuestionForm, + NumberRangeQuestion: NumberRangeQuestionForm, + SelectQuestion: SelectQuestionForm, +} + + +def get_question_types(): + res = {} + questions = BaseQuestion.get_subclasses() + for question in questions: + res[question.type_plural] = question_forms[question] + return res diff --git a/akarpov/test_platform/signals.py b/akarpov/test_platform/signals.py index e69de29..f680eac 100644 --- a/akarpov/test_platform/signals.py +++ b/akarpov/test_platform/signals.py @@ -0,0 +1,53 @@ +from django.db.models.signals import post_save, pre_save +from django.dispatch import receiver + +from akarpov.common.tasks import crop_model_image +from akarpov.test_platform.models import Form +from akarpov.utils.generators import generate_charset + + +@receiver(post_save, sender=Form) +def form_on_create(sender, instance: Form, created, **kwargs): + if created: + if instance.image: + crop_model_image.apply_async( + kwargs={ + "pk": instance.pk, + "app_label": "test_platform", + "model_name": "Form", + }, + countdown=2, + ) + + +@receiver(pre_save, sender=Form) +def form_on_save(sender, instance: Form, **kwargs): + if instance.id is None: + if instance.public: + slug = generate_charset(5) + while Form.objects.filter(slug=slug).exists(): + slug = generate_charset(5) + instance.slug = slug + else: + slug = generate_charset(20) + while Form.objects.filter(slug=slug).exists(): + slug = generate_charset(20) + instance.slug = slug + else: + previous = Form.objects.get(id=instance.id) + if ( + previous.image != instance.image + and kwargs["update_fields"] != frozenset({"image_cropped"}) + and instance + ): + if instance.image: + crop_model_image.apply_async( + kwargs={ + "pk": instance.pk, + "app_label": "test_platform", + "model_name": "Form", + }, + countdown=2, + ) + else: + instance.image_cropped = None diff --git a/akarpov/test_platform/urls.py b/akarpov/test_platform/urls.py new file mode 100644 index 0000000..59bba23 --- /dev/null +++ b/akarpov/test_platform/urls.py @@ -0,0 +1,6 @@ +from django.urls import path + +from akarpov.test_platform.views import from_create_view + +app_name = "test_platform" +urlpatterns = [path("create", from_create_view, name="create")] diff --git a/akarpov/test_platform/views.py b/akarpov/test_platform/views.py index e69de29..ee819fc 100644 --- a/akarpov/test_platform/views.py +++ b/akarpov/test_platform/views.py @@ -0,0 +1,25 @@ +from django.contrib.auth.mixins import LoginRequiredMixin +from django.views.generic import CreateView + +from akarpov.test_platform.forms import FormFormClass +from akarpov.test_platform.models import Form +from akarpov.test_platform.services.forms import get_question_types + + +class FromCreateView(LoginRequiredMixin, CreateView): + model = Form + form_class = FormFormClass + + template_name = "test_platform/create.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["questions"] = get_question_types() + return context + + def form_valid(self, form): + form.instance.creator = self.request.user + return super().form_valid(form) + + +from_create_view = FromCreateView.as_view() diff --git a/config/urls.py b/config/urls.py index cd100a8..5e29010 100644 --- a/config/urls.py +++ b/config/urls.py @@ -23,6 +23,7 @@ path(settings.ADMIN_URL, admin.site.urls), # User management path("users/", include("akarpov.users.urls", namespace="users")), + path("forms/", include("akarpov.test_platform.urls", namespace="forms")), path("tools/", include("akarpov.tools.urls", namespace="tools")), path("ckeditor/", include("ckeditor_uploader.urls")), path("accounts/", include("allauth.urls")),