Coverage for apis_core/generic/forms/fields.py: 41%
34 statements
« prev ^ index » next coverage.py v7.5.3, created at 2025-10-10 13:36 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2025-10-10 13:36 +0000
1from django.core.exceptions import ValidationError
2from django.forms import ModelChoiceField, MultiValueField, MultiWidget
3from django.utils.translation import gettext as _
6class ModelImportChoiceField(ModelChoiceField):
7 def to_python(self, value):
8 result = None
9 if value.startswith(("http://", "https://")):
10 try:
11 result = self.queryset.model.import_from(value)
12 except Exception as e:
13 raise ValidationError(
14 _("Could not import %(value)s: %(exception)s"),
15 params={"value": value, "exception": e},
16 )
17 return result or super().to_python(value)
20class RowColumnMultiWidget(MultiWidget):
21 """
22 A custom MultiWidget that is meant to be used with the
23 RowColumnMultiValueField. The widget takes a list of widgets
24 as a parameter and displays those widgets in columns in one row.
25 The `labels` parameter is used to add a separate label to the
26 individual widgets.
27 """
29 template_name = "widgets/row_column_multiwidget.html"
30 use_fieldset = False
32 def __init__(self, widgets, labels=[], attrs=None):
33 self.labels = labels
34 super().__init__(widgets, attrs)
36 def get_context(self, name, value, attrs):
37 ctx = super().get_context(name, value, attrs)
38 for widget in ctx["widget"]["subwidgets"]:
39 if self.labels:
40 widget["label"] = self.labels.pop(0)
41 return ctx
43 def decompress(self, value):
44 if value:
45 return value
46 return []
49class RowColumnMultiValueField(MultiValueField):
50 """
51 This is a custom MultiValueField that simply shows multiple form
52 fields in a row. The form fields are passed to the constructor and
53 the corresponding RowColumnMultiWidget simply iterates through all
54 the fields and shows them in rows.
55 Additionaly it is possible to pass a list of `labels` that are then
56 also passed on to the widget, which uses those to add a separate
57 label to the individual widgets.
58 """
60 def __init__(self, fields, labels=[], *args, **kwargs):
61 kwargs["widget"] = RowColumnMultiWidget(
62 widgets=[f.widget for f in fields], labels=labels
63 )
64 super().__init__(fields, *args, **kwargs)
66 def compress(self, data_list):
67 return data_list