-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#9] Replace model form fields with regular form fields
- Loading branch information
Showing
2 changed files
with
100 additions
and
111 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import logging | ||
|
||
from django import forms | ||
from django.core.cache import cache | ||
from django.db.models.fields import BLANK_CHOICE_DASH | ||
from django.forms.fields import TypedChoiceField | ||
from django.forms.widgets import Select | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from .utils import get_form_choices | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class OpenFormsBaseField(TypedChoiceField): | ||
""" | ||
Basic field for use in Django forms to render a Select widget filled with | ||
the available forms (uuid, name) or (slug, name) in Open Forms. | ||
This form records the form's UUID or slug, depending on what concrete model | ||
class is used. | ||
""" | ||
|
||
description = _("Open Forms form") | ||
use_uuids = None | ||
widget = Select | ||
include_blank = True | ||
|
||
def get_choices( | ||
self, | ||
blank_choice=BLANK_CHOICE_DASH, | ||
limit_choices_to=None, | ||
ordering=(), | ||
): | ||
cache_key = f"openformsclient.models.OpenFormsFieldMixin.get_choices__use_uuids_{self.use_uuids}" | ||
|
||
choices = cache.get(cache_key) | ||
if choices is None: | ||
try: | ||
choices = get_form_choices(use_uuids=self.use_uuids) | ||
except Exception as e: | ||
logger.exception(e) | ||
choices = [] | ||
else: | ||
cache.set(cache_key, choices, timeout=60) | ||
|
||
if choices: | ||
if self.include_blank: | ||
blank_defined = any(choice in ("", None) for choice, _ in choices) | ||
if not blank_defined: | ||
choices = blank_choice + choices | ||
|
||
return choices | ||
|
||
|
||
class OpenFormsUUIDField(OpenFormsBaseField, forms.UUIDField): | ||
""" | ||
Basic field for use in Django forms to render a Select widget filled with | ||
the available forms (uuid, name) in Open Forms. | ||
This field records the form's UUID. This makes the choice really specific. | ||
Note that to allow empty records, you will need to set ``null=True`` and | ||
``blank=True``. | ||
""" | ||
|
||
use_uuids = True | ||
|
||
def get_db_prep_value(self, value, connection, prepared=False): | ||
# A Select widget always returns a string. If an empty string is | ||
# returned, we need to force it to be None since an empty string is not | ||
# valid UUID nor is it empty. | ||
if not value: | ||
return None | ||
return super().get_db_prep_value(value, connection, prepared) | ||
|
||
|
||
class OpenFormsSlugField(OpenFormsBaseField, forms.SlugField): | ||
""" | ||
Basic field for use in Django forms to render a Select widget filled with | ||
the available forms (slug, name) in Open Forms. | ||
This field records the form's slug. This allows an Open Forms user to | ||
gracefully change the form without the need to change the reference | ||
everywhere. | ||
""" | ||
|
||
use_uuids = False | ||
|
||
def __init__( | ||
self, *args, max_length=100, required=False, allow_unicode=False, include_blank=True, **kwargs | ||
): | ||
super().__init__( | ||
*args, | ||
max_length=max_length, | ||
required=False, | ||
allow_unicode=allow_unicode, | ||
**kwargs, | ||
) | ||
self.include_blank = include_blank | ||
self.coerce = self.to_python |
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