Skip to content

Commit

Permalink
participation questionnaire
Browse files Browse the repository at this point in the history
  • Loading branch information
thomersch committed Feb 15, 2020
1 parent 81737c2 commit 612c401
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
17 changes: 17 additions & 0 deletions osmcal/forms.py
Expand Up @@ -20,6 +20,23 @@ class Meta:
fields = ('question_text', 'answer_type', 'mandatory')


class QuestionnaireForm(forms.Form):
def __init__(self, questions, **kwargs):
self.fields = {}
super().__init__(**kwargs)
for question in questions:
if question.answer_type == 'TEXT':
f = forms.CharField(label=question.question_text)
elif question.answer_type == 'BOOL':
f = forms.BooleanField(label=question.question_text)
elif question.answer_type == 'CHOI':
f = forms.ChoiceField(label=question.question_text, choices=[(x['text'], x['text']) for x in question.choices])
else:
raise ValueError("invalid answer_type: %s" % (question.answer_type))

self.fields[str(question.id)] = f


class EventForm(forms.ModelForm):
class Meta:
model = models.Event
Expand Down
23 changes: 23 additions & 0 deletions osmcal/migrations/0015_auto_20200215_1726.py
@@ -0,0 +1,23 @@
# Generated by Django 3.0.2 on 2020-02-15 17:26

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('osmcal', '0014_auto_20200201_1440'),
]

operations = [
migrations.RemoveField(
model_name='participationquestion',
name='quota',
),
migrations.AddField(
model_name='eventparticipation',
name='answers',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions osmcal/models.py
Expand Up @@ -79,6 +79,8 @@ class EventParticipation(models.Model):
event = models.ForeignKey('Event', null=True, on_delete=models.SET_NULL, related_name='participation')
user = models.ForeignKey('User', null=True, on_delete=models.SET_NULL)

answers = JSONField(blank=True, null=True)


class EventLog(models.Model):
event = models.ForeignKey('Event', related_name='log', on_delete=models.CASCADE)
Expand Down
18 changes: 18 additions & 0 deletions osmcal/templates/osmcal/event_questionnaire.html
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block title %}
{{ event.name }} Questionnaire
{% endblock %}

{% block content %}
<h1>Attend ”{{ event.name }}”</h1>
<div class="text">The event organiser wants you to answer you some questions for sign-up. Answers are publicly visible.</div>

<form method="POST">
<input hidden name="signup-answers" value="1">
{{ form }}
{% csrf_token %}

<button>Sign up</button>
</form>
{% endblock %}
23 changes: 20 additions & 3 deletions osmcal/views.py
Expand Up @@ -124,7 +124,26 @@ def get(self, request, event_id):
@method_decorator(login_required)
def post(self, request, event_id):
evt = Event.objects.get(id=event_id)
EventParticipation.objects.create(event=evt, user=request.user)
questions = evt.questions.all()
answers = None

if questions:
question_form = forms.QuestionnaireForm

if request.POST.get('signup-answers'):
form = question_form(questions, data=request.POST)
form.is_valid()
answers = form.cleaned_data
else:
return render(request, 'osmcal/event_questionnaire.html', context={
'event': evt,
'form': question_form(questions)
})

ep = EventParticipation.objects.create(event=evt, user=request.user)
if answers:
ep.answers = answers
ep.save()
return redirect(reverse('event', kwargs={'event_id': event_id}))


Expand Down Expand Up @@ -171,8 +190,6 @@ def post(self, request, event_id=None):
for qf in question_formset:
questions_data.append(qf.cleaned_data)

print(questions_data)

if event_id is None:
evt = Event.objects.create(**form.cleaned_data)
evt.created_by = request.user
Expand Down

0 comments on commit 612c401

Please sign in to comment.