added form test question types

This commit is contained in:
Alexander Karpov 2023-02-25 11:37:21 +03:00
parent 878158aa09
commit 2d8576ff9b
10 changed files with 163 additions and 3 deletions

View File

@ -0,0 +1,26 @@
{% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block title %}Creating new Form on akarpov{% endblock %}
{% block content %}
<form class="pt-2" enctype="multipart/form-data" method="POST" id="designer-form">
{% csrf_token %}
{{ form.media }}
{% for field in form %}
{{ field|as_crispy_field }}
{% endfor %}
<div class="mt-4 flex justify-end space-x-4">
<button class="btn btn-secondary" type="submit" id="submit">
Save Changes
</button>
</div>
{% for question, question_form in questions.items %}
{{ question }}
{% for field in question_form %}
{{ field|as_crispy_field }}
{% endfor %}
{% endfor %}
</form>
{% endblock %}

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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")]

View File

@ -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()

View File

@ -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")),