Compare commits

...

1 Commits

Author SHA1 Message Date
Raphael Michel
cd4afb4867 Questions: Allow type country code as dependency 2025-11-10 12:58:23 +01:00
9 changed files with 15 additions and 3 deletions

View File

@@ -555,7 +555,8 @@ class QuestionSerializer(I18nAwareModelSerializer):
def validate_dependency_question(self, value):
if value:
if value.type not in (Question.TYPE_CHOICE, Question.TYPE_BOOLEAN, Question.TYPE_CHOICE_MULTIPLE):
if value.type not in (Question.TYPE_CHOICE, Question.TYPE_BOOLEAN, Question.TYPE_CHOICE_MULTIPLE,
Question.TYPE_COUNTRYCODE):
raise ValidationError('Question dependencies can only be set to boolean or choice questions.')
if value == self.instance:
raise ValidationError('A question cannot depend on itself.')

View File

@@ -1061,6 +1061,7 @@ class BaseQuestionsForm(forms.Form):
return (
('True' in qvals and dval)
or ('False' in qvals and not dval)
or (parentq.type == Question.TYPE_COUNTRYCODE and str(dval) in qvals)
or (isinstance(dval, QuestionOption) and dval.identifier in qvals)
or (isinstance(dval, (list, QuerySet)) and any(qval in [o.identifier for o in dval] for qval in qvals))
)

View File

@@ -1623,6 +1623,7 @@ class AbstractPosition(RoundingCorrectionMixin, models.Model):
return (
('True' in qvals and self.answ[parentid].answer == 'True')
or ('False' in qvals and self.answ[parentid].answer == 'False')
or (parentq.type == Question.TYPE_COUNTRYCODE and self.answ[parentid].answer in qvals)
or (any(qval in [o.identifier for o in self.answ[parentid].options.all()] for qval in qvals))
)

View File

@@ -155,7 +155,7 @@ class QuestionForm(I18nModelForm):
self.fields['items'].queryset = self.instance.event.items.all()
self.fields['items'].required = True
self.fields['dependency_question'].queryset = self.instance.event.questions.filter(
type__in=(Question.TYPE_BOOLEAN, Question.TYPE_CHOICE, Question.TYPE_CHOICE_MULTIPLE),
type__in=(Question.TYPE_BOOLEAN, Question.TYPE_CHOICE, Question.TYPE_CHOICE_MULTIPLE, Question.TYPE_COUNTRYCODE),
ask_during_checkin=False
)
if self.instance.pk:

View File

@@ -143,6 +143,7 @@
</div>
<div class="col-md-5">
<script type="text/plain" id="dependency_value_val">{{ form.instance.dependency_values|escapejson_dumps }}</script>
<script type="text/plain" id="countries">{{ countries|escapejson_dumps }}</script>
{% bootstrap_field form.dependency_values layout="inline" form_group_class="inner" %}
</div>
</div>

View File

@@ -87,6 +87,7 @@ from pretix.control.signals import item_forms, item_formsets
from pretix.helpers.models import modelcopy
from ...helpers.compat import CompatDeleteView
from ...helpers.countries import CachedCountries
from . import ChartContainingView, CreateView, PaginationMixin, UpdateView
@@ -657,6 +658,7 @@ class QuestionMixin:
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['formset'] = self.formset
ctx['countries'] = [{"id": k, "name": str(c)} for k, c in dict(CachedCountries().countries).items()]
return ctx

View File

@@ -1058,6 +1058,7 @@ class QuestionsStep(QuestionsViewMixin, CartMixin, TemplateFlowStep):
return (
('True' in qvals and answ[parentid].answer == 'True')
or ('False' in qvals and answ[parentid].answer == 'False')
or (parentq.type == Question.TYPE_COUNTRYCODE and answ[parentid].answer in qvals)
or (any(qval in [o.identifier for o in answ[parentid].options.all()] for qval in qvals))
)

View File

@@ -114,6 +114,7 @@ $(function () {
var $val = $("#id_dependency_values");
var $dq = $("#id_dependency_question");
var oldval = JSON.parse($("#dependency_value_val").text());
var countries = JSON.parse($("#countries").text());
function update_dependency_options() {
$val.parent().find(".loading-indicator").remove();
$("#id_dependency_values option").remove();
@@ -135,6 +136,10 @@ $(function () {
if (data.type === "B") {
$val.append($("<option>").attr("value", "True").text(gettext("Yes")));
$val.append($("<option>").attr("value", "False").text(gettext("No")));
} else if (data.type === "CC") {
for (var c of countries) {
$val.append($("<option>").attr("value", c.id).text(c.name));
}
} else {
for (var i = 0; i < data.options.length; i++) {
var opt = data.options[i];

View File

@@ -11,7 +11,7 @@ function questions_toggle_dependent(ev) {
var $dependency_el;
if ($("select[name=" + dependency_name + "]").length) {
// dependency is type C
// dependency is type C or CC
$dependency_el = $("select[name=" + dependency_name + "]");
if (!$dependency_el.closest(".form-group").hasClass("dependency-hidden")) { // do not show things that depend on hidden things
return q_should_be_shown($dependency_el) && $.inArray($dependency_el.val(), dependency_values) > -1;