2022-11-02 12:27:53 -04:00
|
|
|
from django import forms
|
2023-04-18 15:18:19 -04:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2022-11-03 11:58:26 -07:00
|
|
|
from django.utils.translation import gettext as _
|
2021-09-28 10:44:53 -04:00
|
|
|
|
2022-05-24 10:12:32 +02:00
|
|
|
from extras.choices import CustomFieldVisibilityChoices
|
2023-04-18 15:18:19 -04:00
|
|
|
from extras.models import *
|
|
|
|
from utilities.forms.fields import DynamicModelMultipleChoiceField
|
2021-09-28 10:44:53 -04:00
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
'CustomFieldsMixin',
|
2022-11-02 12:27:53 -04:00
|
|
|
'SavedFiltersMixin',
|
2021-09-28 10:44:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomFieldsMixin:
|
|
|
|
"""
|
|
|
|
Extend a Form to include custom field support.
|
2022-02-01 11:00:18 -05:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
model: The model class
|
2021-09-28 10:44:53 -04:00
|
|
|
"""
|
2022-02-01 11:00:18 -05:00
|
|
|
model = None
|
|
|
|
|
2021-09-28 10:44:53 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
2021-12-30 17:03:41 -05:00
|
|
|
self.custom_fields = {}
|
2022-07-26 12:41:51 -04:00
|
|
|
self.custom_field_groups = {}
|
2021-09-28 10:44:53 -04:00
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self._append_customfield_fields()
|
|
|
|
|
|
|
|
def _get_content_type(self):
|
|
|
|
"""
|
|
|
|
Return the ContentType of the form's model.
|
|
|
|
"""
|
2022-02-01 11:00:18 -05:00
|
|
|
if not getattr(self, 'model', None):
|
2021-09-28 10:44:53 -04:00
|
|
|
raise NotImplementedError(f"{self.__class__.__name__} must specify a model class.")
|
|
|
|
return ContentType.objects.get_for_model(self.model)
|
|
|
|
|
2022-01-17 11:12:54 -05:00
|
|
|
def _get_custom_fields(self, content_type):
|
2022-09-26 16:42:11 -04:00
|
|
|
return CustomField.objects.filter(content_types=content_type).exclude(
|
|
|
|
ui_visibility=CustomFieldVisibilityChoices.VISIBILITY_HIDDEN
|
|
|
|
)
|
2022-01-17 11:12:54 -05:00
|
|
|
|
2021-09-28 10:44:53 -04:00
|
|
|
def _get_form_field(self, customfield):
|
|
|
|
return customfield.to_form_field()
|
|
|
|
|
|
|
|
def _append_customfield_fields(self):
|
|
|
|
"""
|
|
|
|
Append form fields for all CustomFields assigned to this object type.
|
|
|
|
"""
|
2022-01-17 11:12:54 -05:00
|
|
|
for customfield in self._get_custom_fields(self._get_content_type()):
|
2022-05-24 10:12:32 +02:00
|
|
|
if customfield.ui_visibility == CustomFieldVisibilityChoices.VISIBILITY_HIDDEN:
|
|
|
|
continue
|
|
|
|
|
2021-09-28 10:44:53 -04:00
|
|
|
field_name = f'cf_{customfield.name}'
|
|
|
|
self.fields[field_name] = self._get_form_field(customfield)
|
|
|
|
|
|
|
|
# Annotate the field in the list of CustomField form fields
|
2021-12-30 17:03:41 -05:00
|
|
|
self.custom_fields[field_name] = customfield
|
2022-07-26 12:41:51 -04:00
|
|
|
if customfield.group_name not in self.custom_field_groups:
|
|
|
|
self.custom_field_groups[customfield.group_name] = []
|
|
|
|
self.custom_field_groups[customfield.group_name].append(field_name)
|
2022-11-02 12:27:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SavedFiltersMixin(forms.Form):
|
2022-11-15 10:44:12 -05:00
|
|
|
filter_id = DynamicModelMultipleChoiceField(
|
2022-11-02 12:27:53 -04:00
|
|
|
queryset=SavedFilter.objects.all(),
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
label=_('Saved Filter'),
|
2022-11-02 12:27:53 -04:00
|
|
|
query_params={
|
|
|
|
'usable': True,
|
|
|
|
}
|
|
|
|
)
|