-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
417 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,6 @@ | |
|
||
"home", | ||
"survey", | ||
"invites", | ||
] | ||
|
||
MIDDLEWARE = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ traitlets==5.14.3 | |
typing_extensions==4.12.2 | ||
tzdata==2024.1 | ||
wcwidth==0.2.13 | ||
strenum==0.4.15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
from django.contrib import admin | ||
from .models import Questionnaire, Question, Answer | ||
from .models import Survey, SurveyResponse | ||
|
||
admin.site.register(Questionnaire) | ||
admin.site.register(Question) | ||
admin.site.register(Answer) | ||
admin.site.register(Survey) | ||
admin.site.register(SurveyResponse) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,62 @@ | ||
from django import forms | ||
from .models import Answer, Question | ||
class AnswerForm(forms.ModelForm): | ||
class Meta: | ||
model = Answer | ||
fields = [] # no defaults needed | ||
|
||
def __init__(self, *args, **kwargs): | ||
questionnaire = kwargs.pop('questionnaire', None) # get the questionnaire passed in | ||
super().__init__(*args, **kwargs) | ||
|
||
if questionnaire: | ||
|
||
for index, question in enumerate(questionnaire.questions.all(), start=1): | ||
if question.question_type == 'boolean': | ||
self.fields[f'question_{question.id}'] = forms.ChoiceField( | ||
label=f"{index}. {question.question_text}", | ||
choices=[('agree', 'I agree'), ('disagree', 'I disagree')], | ||
widget=forms.RadioSelect, | ||
required=True | ||
) | ||
|
||
elif question.question_type == 'rating': | ||
|
||
self.fields[f'question_{question.id}'] = forms.ChoiceField( | ||
label=question.question_text, | ||
choices=[(i, str(i)) for i in range(1, 6)], # assuming a 1-5 rating | ||
required=True | ||
) | ||
|
||
|
||
else: | ||
|
||
pass | ||
|
||
# self.fields[f'question_{question.id}'] = forms.CharField( | ||
# label=question.question_text, | ||
# required=True, | ||
# widget=forms.Textarea(attrs={'rows': 4, 'style': 'width: 80%;'}), | ||
# ) | ||
from django.forms import BaseFormSet, formset_factory | ||
from strenum import StrEnum | ||
from django.core.validators import EmailValidator | ||
|
||
class InvitationForm(forms.Form): | ||
email = forms.EmailField(label='Participant Email', | ||
max_length=100, | ||
required=True, | ||
validators=[EmailValidator()]) | ||
|
||
class FormFieldType(StrEnum): | ||
CHAR = "char" | ||
TEXT = "text" | ||
RADIO = "radio" | ||
CHECKBOX = "checkbox" | ||
LIKERT = "likert" | ||
|
||
|
||
|
||
def create_field_from_config(field_config: dict): | ||
""" | ||
Convert a field configuration into the correct django field | ||
""" | ||
if field_config['type'] == FormFieldType.CHAR: | ||
field = forms.CharField(label=field_config["label"]) | ||
elif field_config['type'] == FormFieldType.TEXT: | ||
field = forms.CharField(label=field_config["label"], | ||
widget=forms.Textarea) | ||
elif field_config['type'] == FormFieldType.RADIO: | ||
field = forms.ChoiceField(label=field_config["label"], | ||
choices=field_config["options"], | ||
widget=forms.RadioSelect) | ||
elif field_config['type'] == FormFieldType.CHECKBOX: | ||
field = forms.MultipleChoiceField(label=field_config["label"], | ||
widget=forms.CheckboxSelectMultiple, | ||
choices=field_config["options"]) | ||
else: | ||
field = forms.CharField(label=field_config["label"], | ||
widget=forms.Textarea) | ||
|
||
if "required" in field_config: | ||
field.required = field_config["required"] | ||
|
||
return field | ||
|
||
|
||
|
||
def create_dynamic_formset(field_configs: list): | ||
""" | ||
Create a dynamic form set from a list of field configurations. | ||
""" | ||
class BlankDynamicForm(forms.Form): | ||
pass | ||
|
||
class BaseTestFormSet(BaseFormSet): | ||
def add_fields(self, form, index): | ||
super().add_fields(form, index) | ||
for field_config in field_configs: | ||
form.fields[field_config["name"]] = create_field_from_config(field_config) | ||
|
||
return formset_factory(BlankDynamicForm, BaseTestFormSet, min_num=1, max_num=1) |
Oops, something went wrong.