Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add django-dynamic-form to the project #293

Closed
wants to merge 9 commits into from
3 changes: 3 additions & 0 deletions example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dynamic_forms.apps.DynamicFormsConfig',
'wkhtmltopdf',
'konfera',
'payments',
'questionnaires',
]

MIDDLEWARE = [
Expand All @@ -52,6 +54,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'dynamic_forms.middlewares.FormModelMiddleware',
]

ROOT_URLCONF = 'example.urls'
Expand Down
1 change: 1 addition & 0 deletions example/example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
url(r'^admin/', admin.site.urls),
url(r'', include('konfera.urls')),
url(r'', include('payments.urls', namespace='konfera_payments')),
url(r'^forms/', include('questionnaires.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
4 changes: 2 additions & 2 deletions konfera/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ class TicketAdmin(admin.ModelAdmin):
list_filter = ('status', 'type__event',)
ordering = ('order__purchase_date', 'email')
search_fields = ('=last_name', '=first_name', '=email',) # case insensitive searching
readonly_fields = ('link_to_order', 'date_created', 'date_modified')
readonly_fields = ('link_to_order', 'date_created', 'date_modified', 'uuid')
fieldsets = (
(_('Personal details'), {
'fields': ('title', 'first_name', 'last_name', 'email', 'phone')
'fields': ('title', 'first_name', 'last_name', 'email', 'phone', 'uuid')
}),
(_('Ticket info'), {
'fields': ('link_to_order', 'type', 'discount_code', 'status', 'description')
Expand Down
Empty file added questionnaires/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions questionnaires/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.contrib import admin
from django.utils.translation import ugettext as _

from dynamic_forms.admin import FormModelAdmin, FormModelDataAdmin
from dynamic_forms.models import FormModel, FormModelData

from .models import FormForTicket, QuestionnaireForEvent


@admin.register(FormForTicket)
class FormForTicketAdmin(admin.ModelAdmin):
readonly_fields = ['form_data', 'ticket', 'date_created']
list_display = ['ticket', 'form_data', 'date_created']
search_fields = ['ticket__first_name', 'ticket__last_name', 'form_data__value']
list_filter = ['ticket__type__event']


@admin.register(QuestionnaireForEvent)
class QuestionnaireForEventAdmin(admin.ModelAdmin):
list_display = ['slug', 'questionnaire', 'event', 'deadline']
search_fields = ['slug', 'questionnaire__name']
list_filter = ['event']


admin.site.unregister(FormModel)
admin.site.unregister(FormModelData)


class ProxyFormModel(FormModel):
class Meta:
proxy = True
verbose_name = _('Questionnaire')
verbose_name_plural = _('Questionnaires')


class ProxyFormModelDate(FormModelData):
class Meta:
proxy = True
verbose_name = _('Answer')
verbose_name_plural = _('Answers')


admin.site.register(ProxyFormModel, FormModelAdmin)
admin.site.register(ProxyFormModelDate, FormModelDataAdmin)
5 changes: 5 additions & 0 deletions questionnaires/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class QuestionnairesConfig(AppConfig):
name = 'questionnaires'
34 changes: 34 additions & 0 deletions questionnaires/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-01-23 15:50
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
('dynamic_forms', '0006_auto_20170123_1550'),
('konfera', '0029_auto_20170111_2105'),
]

operations = [
migrations.CreateModel(
name='FormForTicket',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False)),
('date_created', models.DateTimeField(auto_now_add=True, verbose_name='Created')),
('date_modified', models.DateTimeField(auto_now=True, verbose_name='Last modified')),
('form_data', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='dynamic_forms.FormModelData')),
('ticket', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='konfera.Ticket')),
],
options={
'abstract': False,
},
),
]
64 changes: 64 additions & 0 deletions questionnaires/migrations/0002_auto_20170124_1131.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-01-24 11:31
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('dynamic_forms', '0006_auto_20170123_1550'),
('konfera', '0029_auto_20170111_2105'),
('questionnaires', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='QuestionnaireForEvent',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False)),
('date_created', models.DateTimeField(auto_now_add=True, verbose_name='Created')),
('date_modified', models.DateTimeField(auto_now=True, verbose_name='Last modified')),
('deadline', models.DateTimeField()),
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='konfera.Event')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='ProxyFormModel',
fields=[
],
options={
'verbose_name': 'Questionnaire',
'proxy': True,
'verbose_name_plural': 'Questionnaires',
},
bases=('dynamic_forms.formmodel',),
),
migrations.CreateModel(
name='ProxyFormModelDate',
fields=[
],
options={
'verbose_name': 'Answer',
'proxy': True,
'verbose_name_plural': 'Answers',
},
bases=('dynamic_forms.formmodeldata',),
),
migrations.AlterModelOptions(
name='formforticket',
options={'verbose_name': 'Answer for a ticket', 'verbose_name_plural': 'Answers for tickets'},
),
migrations.AddField(
model_name='questionnaireforevent',
name='questionnaire',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='dynamic_forms.FormModel'),
),
]
26 changes: 26 additions & 0 deletions questionnaires/migrations/0003_auto_20170124_1417.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-01-24 14:17
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('konfera', '0029_auto_20170111_2105'),
('questionnaires', '0002_auto_20170124_1131'),
]

operations = [
migrations.AddField(
model_name='questionnaireforevent',
name='slug',
field=models.SlugField(default='slug'),
preserve_default=False,
),
migrations.AlterUniqueTogether(
name='questionnaireforevent',
unique_together=set([('event', 'slug')]),
),
]
Empty file.
47 changes: 47 additions & 0 deletions questionnaires/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.db import models
from django.core.validators import ValidationError
from django.utils import timezone
from django.utils.translation import ugettext as _

from konfera.models.abstract import KonferaModel


class FormForTicket(KonferaModel, models.Model):
form_data = models.ForeignKey('dynamic_forms.FormModelData', on_delete=models.CASCADE)
ticket = models.ForeignKey('konfera.Ticket', on_delete=models.CASCADE)

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)

def clean(self):
if FormForTicket.objects.filter(form_data__form=self.form_data.form, ticket=self.ticket).exists():
raise ValidationError(_('Form already submitted.'))
super().clean()

def __str__(self):
return '{} - {}'.format(self.form_data.form.name, self.ticket)

class Meta:
verbose_name = _('Answer for a ticket')
verbose_name_plural = _('Answers for tickets')


class QuestionnaireManager(models.Manager):
def active(self, event):
return self.get_queryset().filter(event=event, deadline__gte=timezone.now())


class QuestionnaireForEvent(KonferaModel, models.Model):
questionnaire = models.OneToOneField('dynamic_forms.FormModel', on_delete=models.CASCADE)
event = models.ForeignKey('konfera.Event', on_delete=models.CASCADE)
deadline = models.DateTimeField()
slug = models.SlugField()

def __str__(self):
return _('Questionnaire {name} for {event}').format(name=self.questionnaire.name, event=self.event.title)

class Meta:
unique_together = [('event', 'slug')]

objects = QuestionnaireManager()
1 change: 1 addition & 0 deletions questionnaires/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
django-dynamic-forms-0.5.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the typo

11 changes: 11 additions & 0 deletions questionnaires/templates/questionnaires/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'konfera/base.html' %}
{% load i18n %}

{% block content %}
<h2>{{ name }}</h2>
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">{% trans "Submit" %}</button>
</form>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'konfera/base.html' %}


{% block content %}
<h1>You have already submitted the form</h1>
{% endblock %}
3 changes: 3 additions & 0 deletions questionnaires/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions questionnaires/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url, include

from . import views

urlpatterns = [
url(r'^(?P<slug>\w+)/ticket/(?P<ticket_uuid>[\w, -]+)/$', views.ShowFormForTicket.as_view()),
url(r'^df/', include('dynamic_forms.urls', namespace='dynamic_forms')),
]
41 changes: 41 additions & 0 deletions questionnaires/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.shortcuts import get_object_or_404, render

from dynamic_forms.actions import dynamic_form_store_database
from dynamic_forms.views import DynamicFormView

from konfera.models import Ticket

from .models import FormForTicket, QuestionnaireForEvent


class ShowFormForTicket(DynamicFormView):

def get_template_names(self):
return ['questionnaires/form.html']

def get_success_url(self):
return self.request.path

def get_data(self):
if 'ticket' not in self.kwargs:
self.kwargs['ticket'] = get_object_or_404(Ticket, uuid=self.kwargs['ticket_uuid'])
if 'model' not in self.kwargs:
questionnaire = get_object_or_404(
QuestionnaireForEvent.objects.active(self.kwargs['ticket'].type.event),
slug=self.kwargs['slug']
)
self.kwargs['model'] = questionnaire.questionnaire

def dispatch(self, request, *args, **kwargs):
self.get_data()

if FormForTicket.objects.filter(form_data__form=self.kwargs['model'], ticket=self.kwargs['ticket']).exists():
return render(request, 'questionnaires/form_already_submited.html')

return super().dispatch(request, *args, **kwargs)

def form_valid(self, form):
self.get_data()
data = dynamic_form_store_database(self.kwargs['model'], form, self.request)
FormForTicket.objects.create(form_data=data, ticket=self.kwargs['ticket'])
return super().form_valid(form)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ django-sitetree==1.6.0
mock==2.0.0
pytz==2016.7
Pillow==3.4.2
django-dynamic-forms-0.5.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the typo