Pierre-Sassoulas / django-survey

A django survey app that can export results as CSV or PDF using your native language.
GNU Affero General Public License v3.0
217 stars 145 forks source link

maximum possible #125

Closed gjelsas closed 2 years ago

gjelsas commented 3 years ago

First implementation for a field maximum possible answers.

I would like to create a useful notice to the user, so he/she knows why his/her selection was not accepted. At the moment the survey just doesn't get saved. #124

Pierre-Sassoulas commented 3 years ago

Thank you for this. I think another approach would be to create a new form class called SelectMultipleLimited or something like this. See code here for SelectMultiple here : https://github.com/django/django/blob/master/django/forms/widgets.py#L744 You can probably inherit from this class and add a different clean function and a clean translated error message there.

gjelsas commented 3 years ago

Thank you for this. I think another approach would be to create a new form class called SelectMultipleLimited or something like this. See code here for SelectMultiple here : https://github.com/django/django/blob/master/django/forms/widgets.py#L744 You can probably inherit from this class and add a different clean function and a clean translated error message there.

You mean, not to have it just as a function, but creating an all new class? Where would that class be? That would mean to have another kind of Question.type to select from? For writing that, I would need more understanding of the code from my part... Some more hints could maybe help me getting there! Thanks!

Pierre-Sassoulas commented 3 years ago

Yes exactly :) Well we could add the class in https://github.com/Pierre-Sassoulas/django-survey/blob/master/survey/forms.py, then create a Question.SELECT_MULTIPLE_LIMITED value in the Question model, then we need to add this value in all the relevant list like here. The change would be a new class based on SelectMultiple and 3/4 lines of changes for handling a new question type.

gjelsas commented 3 years ago

I'm trying to inherit that new class from django.forms.widgets.CheckboxSelectMultiple but I do always hit a TypeError: __init__() got an unexpected keyword argument 'label' Error in django-survey/survey/forms.py, line 237, get_question_field return self.FIELDS[question.type](**kwargs) and I don't know how to avoid that. So if there is a hint or a direction to point me at, it would be greatly appreciated...

Pierre-Sassoulas commented 3 years ago

Apparently CheckboxSelectMultiple do not have a label argument in its constructor, you can either remove the label argument from the kwargs (del kwargs["label"]) or make a spacial case for CheckboxSelectMultiple where label could have another name.

Doc for CheckboxSelectMultiple so I don't search for it next time :D https://docs.djangoproject.com/en/3.1/ref/forms/widgets/#checkboxselectmultiple

Pierre-Sassoulas commented 2 years ago

Closing in favor of #166 following a rebase.