| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from django import forms |
| 12 |
from django.conf import settings |
| 13 |
from django.core.validators import ( |
| 14 |
MaxLengthValidator, |
| 15 |
MaxValueValidator, |
| 16 |
MinLengthValidator, |
| 17 |
MinValueValidator, |
| 18 |
) |
| 19 |
from django.forms import widgets |
| 20 |
|
| 21 |
from tests.django.toystore.models import ( |
| 22 |
Company, |
| 23 |
CouldBeCharming, |
| 24 |
Customer, |
| 25 |
FileFields, |
| 26 |
ManyNumerics, |
| 27 |
ManyTimes, |
| 28 |
OddFields, |
| 29 |
Store, |
| 30 |
) |
| 31 |
|
| 32 |
if "django.contrib.auth" in settings.INSTALLED_APPS: |
| 33 |
from django.contrib.auth.forms import ReadOnlyPasswordHashField, UsernameField |
| 34 |
|
| 35 |
|
| 36 |
class ReprModelForm(forms.ModelForm): |
| 37 |
def __repr__(self): |
| 38 |
"""I recommend putting this in your form to show the failed cases.""" |
| 39 |
return f"{self.data!r}\n{self.errors!r}" |
| 40 |
|
| 41 |
|
| 42 |
class ReprForm(forms.Form): |
| 43 |
def __repr__(self): |
| 44 |
return f"{self.data!r}\n{self.errors!r}" |
| 45 |
|
| 46 |
|
| 47 |
class CouldBeCharmingForm(ReprModelForm): |
| 48 |
class Meta: |
| 49 |
model = CouldBeCharming |
| 50 |
fields = "__all__" |
| 51 |
|
| 52 |
|
| 53 |
class FileFieldsForm(ReprModelForm): |
| 54 |
class Meta: |
| 55 |
model = FileFields |
| 56 |
fields = "__all__" |
| 57 |
|
| 58 |
|
| 59 |
class CustomerForm(ReprModelForm): |
| 60 |
class Meta: |
| 61 |
model = Customer |
| 62 |
fields = "__all__" |
| 63 |
|
| 64 |
|
| 65 |
class ManyNumericsForm(ReprModelForm): |
| 66 |
class Meta: |
| 67 |
model = ManyNumerics |
| 68 |
fields = "__all__" |
| 69 |
|
| 70 |
|
| 71 |
class ManyTimesForm(ReprModelForm): |
| 72 |
class Meta: |
| 73 |
model = ManyTimes |
| 74 |
fields = "__all__" |
| 75 |
|
| 76 |
|
| 77 |
class OddFieldsForm(ReprModelForm): |
| 78 |
class Meta: |
| 79 |
model = OddFields |
| 80 |
fields = "__all__" |
| 81 |
|
| 82 |
|
| 83 |
class DynamicForm(ReprForm): |
| 84 |
def __init__(self, field_count=5, **kwargs): |
| 85 |
super().__init__(**kwargs) |
| 86 |
for i in range(field_count): |
| 87 |
field_name = f"field-{i}" |
| 88 |
self.fields[field_name] = forms.CharField(required=False) |
| 89 |
|
| 90 |
|
| 91 |
class BasicFieldForm(ReprForm): |
| 92 |
_boolean_required = forms.BooleanField() |
| 93 |
_boolean = forms.BooleanField(required=False) |
| 94 |
# This took me too long to figure out... The BooleanField will actually |
| 95 |
# raise a ValidationError when it receives a value of False. Why they |
| 96 |
# didn't call it a TrueOnlyField escapes me, but *if* you actually want |
| 97 |
# to accept both True and False in your BooleanField, make sure you set |
| 98 |
# `required=False`. This behavior has been hotly contested in the bug |
| 99 |
# tracker (e.g. https://code.djangoproject.com/ticket/23547), but it |
| 100 |
# seems that since the tests and documentation are already written |
| 101 |
# this behavior is Truth. |
| 102 |
# see the note in the documentation |
| 103 |
# https://docs.djangoproject.com/en/dev/ref/forms/fields/#booleanfield |
| 104 |
|
| 105 |
_char_required = forms.CharField(required=True) |
| 106 |
_char = forms.CharField(required=False) |
| 107 |
_decimal = forms.DecimalField(max_digits=8, decimal_places=3) |
| 108 |
_float = forms.FloatField() |
| 109 |
_integer = forms.IntegerField() |
| 110 |
_null_boolean = forms.NullBooleanField() |
| 111 |
|
| 112 |
|
| 113 |
class TemporalFieldForm(ReprForm): |
| 114 |
_date = forms.DateField() |
| 115 |
_date_time = forms.DateTimeField() |
| 116 |
_duration = forms.DurationField() |
| 117 |
_time = forms.TimeField() |
| 118 |
_split_date_time = forms.SplitDateTimeField() |
| 119 |
|
| 120 |
|
| 121 |
class WithValidatorsForm(ReprForm): |
| 122 |
num_validators = (MinValueValidator(1), MaxValueValidator(5)) |
| 123 |
_int_one_to_five = forms.IntegerField(validators=num_validators) |
| 124 |
_decimal_one_to_five = forms.FloatField(validators=num_validators) |
| 125 |
_float_one_to_five = forms.FloatField(validators=num_validators) |
| 126 |
len_validators = (MinLengthValidator(5), MaxLengthValidator(10)) |
| 127 |
_string_five_to_ten = forms.CharField(validators=len_validators) |
| 128 |
|
| 129 |
|
| 130 |
class EmailFieldForm(ReprForm): |
| 131 |
_email = forms.EmailField() |
| 132 |
|
| 133 |
|
| 134 |
class SlugFieldForm(ReprForm): |
| 135 |
_slug = forms.SlugField() |
| 136 |
|
| 137 |
|
| 138 |
class URLFieldForm(ReprForm): |
| 139 |
_url = forms.URLField() |
| 140 |
|
| 141 |
|
| 142 |
class RegexFieldForm(ReprForm): |
| 143 |
_regex = forms.RegexField(regex="[A-Z]{3}\\.[a-z]{4}") |
| 144 |
|
| 145 |
|
| 146 |
class UUIDFieldForm(ReprForm): |
| 147 |
_uuid = forms.UUIDField() |
| 148 |
|
| 149 |
|
| 150 |
class ChoiceFieldForm(ReprForm): |
| 151 |
_choice = forms.ChoiceField( |
| 152 |
choices=(("cola", "Cola"), ("tea", "Tea"), ("water", "Water")) |
| 153 |
) |
| 154 |
_multiple = forms.MultipleChoiceField( |
| 155 |
choices=(("cola", "Cola"), ("tea", "Tea"), ("water", "Water")) |
| 156 |
) |
| 157 |
_typed = forms.TypedChoiceField( |
| 158 |
choices=(("1", "one"), ("2", "two"), ("3", "three"), ("4", "four")), |
| 159 |
coerce=int, |
| 160 |
empty_value=0, |
| 161 |
) |
| 162 |
_typed_multiple = forms.TypedMultipleChoiceField( |
| 163 |
choices=(("1", "one"), ("2", "two"), ("3", "three"), ("4", "four")), |
| 164 |
coerce=int, |
| 165 |
empty_value=0, |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
class InternetProtocolForm(ReprForm): |
| 170 |
_ip_both = forms.GenericIPAddressField() |
| 171 |
_ip_v4 = forms.GenericIPAddressField(protocol="IPv4") |
| 172 |
_ip_v6 = forms.GenericIPAddressField(protocol="IPv6") |
| 173 |
|
| 174 |
|
| 175 |
class BroadBooleanInput(widgets.CheckboxInput): |
| 176 |
"""Basically pulled directly from the Django CheckboxInput. I added |
| 177 |
some stuff to ``values`` |
| 178 |
""" |
| 179 |
|
| 180 |
def value_from_datadict(self, data, files, name): |
| 181 |
if name not in data: |
| 182 |
return False |
| 183 |
value = data.get(name) |
| 184 |
# Translate true and false strings to boolean values. |
| 185 |
values = {"true": True, "false": False, "0": False, "1": True} |
| 186 |
if isinstance(value, str): |
| 187 |
value = values.get(value.lower(), value) |
| 188 |
return bool(value) |
| 189 |
|
| 190 |
|
| 191 |
class MultiCheckboxWidget(widgets.MultiWidget): |
| 192 |
def __init__(self, subfield_count=12, **kwargs): |
| 193 |
_widgets = [BroadBooleanInput()] * subfield_count |
| 194 |
super().__init__(_widgets, **kwargs) |
| 195 |
|
| 196 |
def decompress(self, value): |
| 197 |
values = [] |
| 198 |
for _value in value.split("::"): |
| 199 |
if _value in ("0", "", "False", 0, None, False): |
| 200 |
values.append(False) |
| 201 |
else: |
| 202 |
values.append(True) |
| 203 |
return values |
| 204 |
|
| 205 |
|
| 206 |
class BroadBooleanField(forms.BooleanField): |
| 207 |
pass |
| 208 |
|
| 209 |
|
| 210 |
class MultiBooleanField(forms.MultiValueField): |
| 211 |
def __init__(self, subfield_count=12, **kwargs): |
| 212 |
subfields = [BroadBooleanField()] * subfield_count |
| 213 |
widget = MultiCheckboxWidget(subfield_count=subfield_count) |
| 214 |
super().__init__(fields=subfields, widget=widget) |
| 215 |
|
| 216 |
def compress(self, values): |
| 217 |
return "::".join(str(x) for x in values) |
| 218 |
|
| 219 |
|
| 220 |
class ManyMultiValueForm(ReprForm): |
| 221 |
def __init__(self, subfield_count=12, **kwargs): |
| 222 |
super().__init__(**kwargs) |
| 223 |
self.fields["mv_field"] = MultiBooleanField(subfield_count=subfield_count) |
| 224 |
|
| 225 |
|
| 226 |
class ShortStringForm(ReprForm): |
| 227 |
_not_too_long = forms.CharField(max_length=20, required=False) |
| 228 |
|
| 229 |
|
| 230 |
if "django.contrib.auth" in settings.INSTALLED_APPS: |
| 231 |
|
| 232 |
class UsernameForm(ReprForm): |
| 233 |
username = UsernameField() |
| 234 |
|
| 235 |
class ReadOnlyPasswordHashFieldForm(ReprForm): |
| 236 |
password = ReadOnlyPasswordHashField() |
| 237 |
|
| 238 |
else: |
| 239 |
UsernameForm = None |
| 240 |
ReadOnlyPasswordHashFieldForm = None |
| 241 |
|
| 242 |
|
| 243 |
class StoreForm(ReprModelForm): |
| 244 |
company = forms.ModelChoiceField(queryset=Company.objects.order_by("name")) |
| 245 |
|
| 246 |
class Meta: |
| 247 |
model = Store |
| 248 |
fields = "__all__" |
| 249 |
|
| 250 |
|
| 251 |
class MultipleCompaniesForm(ReprForm): |
| 252 |
companies = forms.ModelMultipleChoiceField( |
| 253 |
queryset=Company.objects.order_by("name") |
| 254 |
) |