mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 07:26:33 +03:00
added form test question types
This commit is contained in:
parent
878158aa09
commit
2d8576ff9b
26
akarpov/templates/test_platform/create.html
Normal file
26
akarpov/templates/test_platform/create.html
Normal 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 %}
|
10
akarpov/templates/test_platform/edit.html
Normal file
10
akarpov/templates/test_platform/edit.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
10
akarpov/templates/test_platform/view.html
Normal file
10
akarpov/templates/test_platform/view.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -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"]
|
||||
|
|
0
akarpov/test_platform/services/__init__.py
Normal file
0
akarpov/test_platform/services/__init__.py
Normal file
28
akarpov/test_platform/services/forms.py
Normal file
28
akarpov/test_platform/services/forms.py
Normal 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
|
|
@ -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
|
6
akarpov/test_platform/urls.py
Normal file
6
akarpov/test_platform/urls.py
Normal 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")]
|
|
@ -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()
|
|
@ -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")),
|
||||
|
|
Loading…
Reference in New Issue
Block a user