Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django import forms
4from django.utils.translation import gettext_lazy as _
5from superdjango.contrib.i18n.languages import COMMON_LANGUAGE_CHOICES, MAX_CHOICE_LENGTH as LANGUAGE_MAX_CHOICE_LENGTH
6from superdjango.contrib.i18n.timezones import COMMON_TIMEZONE_CHOICES, MAX_CHOICE_LENGTH as TIMEZONE_MAX_CHOICE_LENGTH
8# Exports
10__all__ = (
11 "FullNameChoiceField",
12 "LanguageChoiceField",
13 "TimeZoneChoiceField",
14)
16# Fields
19class FullNameChoiceField(forms.ModelChoiceField):
20 """If available, uses the full name of the user for the dropdown instead of the user name.
22 **Usage**
24 In your form:
26 .. code-block:: python
28 from django.contrib.auth import get_user_model
30 user_model = get_user_model()
32 class MyModelForm(ModelForm):
33 requested_by = FullNameChoiceField(
34 queryset=user_model.objects.filter(is_staff=True),
35 label=_("requested by")
36 )
38 """
40 def label_from_instance(self, obj):
41 return obj.get_full_name() or obj.username
44class LanguageChoiceField(forms.ChoiceField):
45 """A field that presents language choices.
47 By default, all ``COMMON_LANGUAGE_CHOICES`` are used.
49 """
50 # TODO: Determine whether LanguageChoiceField should be homed in forms.fields or in i18n.
51 description = _("A field of language choices.")
53 def __init__(self, *args, **kwargs):
54 kwargs.setdefault("choices", COMMON_LANGUAGE_CHOICES)
55 kwargs['max_length'] = LANGUAGE_MAX_CHOICE_LENGTH
57 super().__init__(*args, **kwargs)
60# class SlugFromField(forms.SlugField):
61# """A slug field that provides automated slugify."""
62# description = _("A slug field that provides automated slugify.")
63#
64# def media
66class TimeZoneChoiceField(forms.ChoiceField):
67 """A field that presents time zone choices.
69 By default, all ``COMMON_TIMEZONE_CHOICES`` are used.
71 """
72 # TODO: Determine whether TimeZoneChoiceField should be homed in forms.fields or in i18n.
73 description = _("A field of time zone choices.")
75 def __init__(self, *args, **kwargs):
76 kwargs.setdefault("choices", COMMON_TIMEZONE_CHOICES)
77 kwargs['max_length'] = TIMEZONE_MAX_CHOICE_LENGTH
79 super().__init__(*args, **kwargs)