forms.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. import csv
  2. import json
  3. import re
  4. from io import StringIO
  5. import django_filters
  6. import yaml
  7. from django import forms
  8. from django.conf import settings
  9. from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
  10. from django.db.models import Count
  11. from django.forms import BoundField
  12. from django.forms.models import fields_for_model
  13. from django.urls import reverse
  14. from .choices import unpack_grouped_choices
  15. from .constants import *
  16. from .validators import EnhancedURLValidator
  17. NUMERIC_EXPANSION_PATTERN = r'\[((?:\d+[?:,-])+\d+)\]'
  18. ALPHANUMERIC_EXPANSION_PATTERN = r'\[((?:[a-zA-Z0-9]+[?:,-])+[a-zA-Z0-9]+)\]'
  19. IP4_EXPANSION_PATTERN = r'\[((?:[0-9]{1,3}[?:,-])+[0-9]{1,3})\]'
  20. IP6_EXPANSION_PATTERN = r'\[((?:[0-9a-f]{1,4}[?:,-])+[0-9a-f]{1,4})\]'
  21. BOOLEAN_WITH_BLANK_CHOICES = (
  22. ('', '---------'),
  23. ('True', 'Yes'),
  24. ('False', 'No'),
  25. )
  26. def parse_numeric_range(string, base=10):
  27. """
  28. Expand a numeric range (continuous or not) into a decimal or
  29. hexadecimal list, as specified by the base parameter
  30. '0-3,5' => [0, 1, 2, 3, 5]
  31. '2,8-b,d,f' => [2, 8, 9, a, b, d, f]
  32. """
  33. values = list()
  34. for dash_range in string.split(','):
  35. try:
  36. begin, end = dash_range.split('-')
  37. except ValueError:
  38. begin, end = dash_range, dash_range
  39. begin, end = int(begin.strip(), base=base), int(end.strip(), base=base) + 1
  40. values.extend(range(begin, end))
  41. return list(set(values))
  42. def parse_alphanumeric_range(string):
  43. """
  44. Expand an alphanumeric range (continuous or not) into a list.
  45. 'a-d,f' => [a, b, c, d, f]
  46. '0-3,a-d' => [0, 1, 2, 3, a, b, c, d]
  47. """
  48. values = []
  49. for dash_range in string.split(','):
  50. try:
  51. begin, end = dash_range.split('-')
  52. vals = begin + end
  53. # Break out of loop if there's an invalid pattern to return an error
  54. if (not (vals.isdigit() or vals.isalpha())) or (vals.isalpha() and not (vals.isupper() or vals.islower())):
  55. return []
  56. except ValueError:
  57. begin, end = dash_range, dash_range
  58. if begin.isdigit() and end.isdigit():
  59. for n in list(range(int(begin), int(end) + 1)):
  60. values.append(n)
  61. else:
  62. # Value-based
  63. if begin == end:
  64. values.append(begin)
  65. # Range-based
  66. else:
  67. # Not a valid range (more than a single character)
  68. if not len(begin) == len(end) == 1:
  69. raise forms.ValidationError('Range "{}" is invalid.'.format(dash_range))
  70. for n in list(range(ord(begin), ord(end) + 1)):
  71. values.append(chr(n))
  72. return values
  73. def expand_alphanumeric_pattern(string):
  74. """
  75. Expand an alphabetic pattern into a list of strings.
  76. """
  77. lead, pattern, remnant = re.split(ALPHANUMERIC_EXPANSION_PATTERN, string, maxsplit=1)
  78. parsed_range = parse_alphanumeric_range(pattern)
  79. for i in parsed_range:
  80. if re.search(ALPHANUMERIC_EXPANSION_PATTERN, remnant):
  81. for string in expand_alphanumeric_pattern(remnant):
  82. yield "{}{}{}".format(lead, i, string)
  83. else:
  84. yield "{}{}{}".format(lead, i, remnant)
  85. def expand_ipaddress_pattern(string, family):
  86. """
  87. Expand an IP address pattern into a list of strings. Examples:
  88. '192.0.2.[1,2,100-250]/24' => ['192.0.2.1/24', '192.0.2.2/24', '192.0.2.100/24' ... '192.0.2.250/24']
  89. '2001:db8:0:[0,fd-ff]::/64' => ['2001:db8:0:0::/64', '2001:db8:0:fd::/64', ... '2001:db8:0:ff::/64']
  90. """
  91. if family not in [4, 6]:
  92. raise Exception("Invalid IP address family: {}".format(family))
  93. if family == 4:
  94. regex = IP4_EXPANSION_PATTERN
  95. base = 10
  96. else:
  97. regex = IP6_EXPANSION_PATTERN
  98. base = 16
  99. lead, pattern, remnant = re.split(regex, string, maxsplit=1)
  100. parsed_range = parse_numeric_range(pattern, base)
  101. for i in parsed_range:
  102. if re.search(regex, remnant):
  103. for string in expand_ipaddress_pattern(remnant, family):
  104. yield ''.join([lead, format(i, 'x' if family == 6 else 'd'), string])
  105. else:
  106. yield ''.join([lead, format(i, 'x' if family == 6 else 'd'), remnant])
  107. def add_blank_choice(choices):
  108. """
  109. Add a blank choice to the beginning of a choices list.
  110. """
  111. return ((None, '---------'),) + tuple(choices)
  112. def form_from_model(model, fields):
  113. """
  114. Return a Form class with the specified fields derived from a model. This is useful when we need a form to be used
  115. for creating objects, but want to avoid the model's validation (e.g. for bulk create/edit functions). All fields
  116. are marked as not required.
  117. """
  118. form_fields = fields_for_model(model, fields=fields)
  119. for field in form_fields.values():
  120. field.required = False
  121. return type('FormFromModel', (forms.Form,), form_fields)
  122. #
  123. # Widgets
  124. #
  125. class SmallTextarea(forms.Textarea):
  126. """
  127. Subclass used for rendering a smaller textarea element.
  128. """
  129. pass
  130. class SlugWidget(forms.TextInput):
  131. """
  132. Subclass TextInput and add a slug regeneration button next to the form field.
  133. """
  134. template_name = 'widgets/sluginput.html'
  135. class ColorSelect(forms.Select):
  136. """
  137. Extends the built-in Select widget to colorize each <option>.
  138. """
  139. option_template_name = 'widgets/colorselect_option.html'
  140. def __init__(self, *args, **kwargs):
  141. kwargs['choices'] = add_blank_choice(COLOR_CHOICES)
  142. super().__init__(*args, **kwargs)
  143. self.attrs['class'] = 'netbox-select2-color-picker'
  144. class BulkEditNullBooleanSelect(forms.NullBooleanSelect):
  145. """
  146. A Select widget for NullBooleanFields
  147. """
  148. def __init__(self, *args, **kwargs):
  149. super().__init__(*args, **kwargs)
  150. # Override the built-in choice labels
  151. self.choices = (
  152. ('1', '---------'),
  153. ('2', 'Yes'),
  154. ('3', 'No'),
  155. )
  156. self.attrs['class'] = 'netbox-select2-static'
  157. class SelectWithDisabled(forms.Select):
  158. """
  159. Modified the stock Select widget to accept choices using a dict() for a label. The dict for each option must include
  160. 'label' (string) and 'disabled' (boolean).
  161. """
  162. option_template_name = 'widgets/selectwithdisabled_option.html'
  163. class StaticSelect2(SelectWithDisabled):
  164. """
  165. A static content using the Select2 widget
  166. :param filter_for: (Optional) A dict of chained form fields for which this field is a filter. The key is the
  167. name of the filter-for field (child field) and the value is the name of the query param filter.
  168. """
  169. def __init__(self, filter_for=None, *args, **kwargs):
  170. super().__init__(*args, **kwargs)
  171. self.attrs['class'] = 'netbox-select2-static'
  172. if filter_for:
  173. for key, value in filter_for.items():
  174. self.add_filter_for(key, value)
  175. def add_filter_for(self, name, value):
  176. """
  177. Add details for an additional query param in the form of a data-filter-for-* attribute.
  178. :param name: The name of the query param
  179. :param value: The value of the query param
  180. """
  181. self.attrs['data-filter-for-{}'.format(name)] = value
  182. class StaticSelect2Multiple(StaticSelect2, forms.SelectMultiple):
  183. def __init__(self, *args, **kwargs):
  184. super().__init__(*args, **kwargs)
  185. self.attrs['data-multiple'] = 1
  186. class SelectWithPK(StaticSelect2):
  187. """
  188. Include the primary key of each option in the option label (e.g. "Router7 (4721)").
  189. """
  190. option_template_name = 'widgets/select_option_with_pk.html'
  191. class ContentTypeSelect(StaticSelect2):
  192. """
  193. Appends an `api-value` attribute equal to the slugified model name for each ContentType. For example:
  194. <option value="37" api-value="console-server-port">console server port</option>
  195. This attribute can be used to reference the relevant API endpoint for a particular ContentType.
  196. """
  197. option_template_name = 'widgets/select_contenttype.html'
  198. class ArrayFieldSelectMultiple(SelectWithDisabled, forms.SelectMultiple):
  199. """
  200. MultiSelect widget for a SimpleArrayField. Choices must be populated on the widget.
  201. """
  202. def __init__(self, *args, **kwargs):
  203. self.delimiter = kwargs.pop('delimiter', ',')
  204. super().__init__(*args, **kwargs)
  205. def optgroups(self, name, value, attrs=None):
  206. # Split the delimited string of values into a list
  207. if value:
  208. value = value[0].split(self.delimiter)
  209. return super().optgroups(name, value, attrs)
  210. def value_from_datadict(self, data, files, name):
  211. # Condense the list of selected choices into a delimited string
  212. data = super().value_from_datadict(data, files, name)
  213. return self.delimiter.join(data)
  214. class APISelect(SelectWithDisabled):
  215. """
  216. A select widget populated via an API call
  217. :param api_url: API endpoint URL. Required if not set automatically by the parent field.
  218. :param display_field: (Optional) Field to display for child in selection list. Defaults to `name`.
  219. :param value_field: (Optional) Field to use for the option value in selection list. Defaults to `id`.
  220. :param disabled_indicator: (Optional) Mark option as disabled if this field equates true.
  221. :param filter_for: (Optional) A dict of chained form fields for which this field is a filter. The key is the
  222. name of the filter-for field (child field) and the value is the name of the query param filter.
  223. :param conditional_query_params: (Optional) A dict of URL query params to append to the URL if the
  224. condition is met. The condition is the dict key and is specified in the form `<field_name>__<field_value>`.
  225. If the provided field value is selected for the given field, the URL query param will be appended to
  226. the rendered URL. The value is the in the from `<param_name>=<param_value>`. This is useful in cases where
  227. a particular field value dictates an additional API filter.
  228. :param additional_query_params: Optional) A dict of query params to append to the API request. The key is the
  229. name of the query param and the value if the query param's value.
  230. :param null_option: If true, include the static null option in the selection list.
  231. """
  232. def __init__(
  233. self,
  234. api_url=None,
  235. display_field=None,
  236. value_field=None,
  237. disabled_indicator=None,
  238. filter_for=None,
  239. conditional_query_params=None,
  240. additional_query_params=None,
  241. null_option=False,
  242. full=False,
  243. *args,
  244. **kwargs
  245. ):
  246. super().__init__(*args, **kwargs)
  247. self.attrs['class'] = 'netbox-select2-api'
  248. if api_url:
  249. self.attrs['data-url'] = '/{}{}'.format(settings.BASE_PATH, api_url.lstrip('/')) # Inject BASE_PATH
  250. if full:
  251. self.attrs['data-full'] = full
  252. if display_field:
  253. self.attrs['display-field'] = display_field
  254. if value_field:
  255. self.attrs['value-field'] = value_field
  256. if disabled_indicator:
  257. self.attrs['disabled-indicator'] = disabled_indicator
  258. if filter_for:
  259. for key, value in filter_for.items():
  260. self.add_filter_for(key, value)
  261. if conditional_query_params:
  262. for key, value in conditional_query_params.items():
  263. self.add_conditional_query_param(key, value)
  264. if additional_query_params:
  265. for key, value in additional_query_params.items():
  266. self.add_additional_query_param(key, value)
  267. if null_option:
  268. self.attrs['data-null-option'] = 1
  269. def add_filter_for(self, name, value):
  270. """
  271. Add details for an additional query param in the form of a data-filter-for-* attribute.
  272. :param name: The name of the query param
  273. :param value: The value of the query param
  274. """
  275. self.attrs['data-filter-for-{}'.format(name)] = value
  276. def add_additional_query_param(self, name, value):
  277. """
  278. Add details for an additional query param in the form of a data-* JSON-encoded list attribute.
  279. :param name: The name of the query param
  280. :param value: The value of the query param
  281. """
  282. key = 'data-additional-query-param-{}'.format(name)
  283. values = json.loads(self.attrs.get(key, '[]'))
  284. values.append(value)
  285. self.attrs[key] = json.dumps(values)
  286. def add_conditional_query_param(self, condition, value):
  287. """
  288. Add details for a URL query strings to append to the URL if the condition is met.
  289. The condition is specified in the form `<field_name>__<field_value>`.
  290. :param condition: The condition for the query param
  291. :param value: The value of the query param
  292. """
  293. self.attrs['data-conditional-query-param-{}'.format(condition)] = value
  294. class APISelectMultiple(APISelect, forms.SelectMultiple):
  295. def __init__(self, *args, **kwargs):
  296. super().__init__(*args, **kwargs)
  297. self.attrs['data-multiple'] = 1
  298. class DatePicker(forms.TextInput):
  299. """
  300. Date picker using Flatpickr.
  301. """
  302. def __init__(self, *args, **kwargs):
  303. super().__init__(*args, **kwargs)
  304. self.attrs['class'] = 'date-picker'
  305. self.attrs['placeholder'] = 'YYYY-MM-DD'
  306. class DateTimePicker(forms.TextInput):
  307. """
  308. DateTime picker using Flatpickr.
  309. """
  310. def __init__(self, *args, **kwargs):
  311. super().__init__(*args, **kwargs)
  312. self.attrs['class'] = 'datetime-picker'
  313. self.attrs['placeholder'] = 'YYYY-MM-DD hh:mm:ss'
  314. class TimePicker(forms.TextInput):
  315. """
  316. Time picker using Flatpickr.
  317. """
  318. def __init__(self, *args, **kwargs):
  319. super().__init__(*args, **kwargs)
  320. self.attrs['class'] = 'time-picker'
  321. self.attrs['placeholder'] = 'hh:mm:ss'
  322. #
  323. # Form fields
  324. #
  325. class CSVDataField(forms.CharField):
  326. """
  327. A CharField (rendered as a Textarea) which accepts CSV-formatted data. It returns a list of dictionaries mapping
  328. column headers to values. Each dictionary represents an individual record.
  329. """
  330. widget = forms.Textarea
  331. def __init__(self, from_form, *args, **kwargs):
  332. form = from_form()
  333. self.model = form.Meta.model
  334. self.fields = form.fields
  335. self.required_fields = [
  336. name for name, field in form.fields.items() if field.required
  337. ]
  338. super().__init__(*args, **kwargs)
  339. self.strip = False
  340. if not self.label:
  341. self.label = ''
  342. if not self.initial:
  343. self.initial = ','.join(self.required_fields) + '\n'
  344. if not self.help_text:
  345. self.help_text = 'Enter the list of column headers followed by one line per record to be imported, using ' \
  346. 'commas to separate values. Multi-line data and values containing commas may be wrapped ' \
  347. 'in double quotes.'
  348. def to_python(self, value):
  349. records = []
  350. reader = csv.reader(StringIO(value.strip()))
  351. # Consume the first line of CSV data as column headers. Create a dictionary mapping each header to an optional
  352. # "to" field specifying how the related object is being referenced. For example, importing a Device might use a
  353. # `site.slug` header, to indicate the related site is being referenced by its slug.
  354. headers = {}
  355. for header in next(reader):
  356. if '.' in header:
  357. field, to_field = header.split('.', 1)
  358. headers[field] = to_field
  359. else:
  360. headers[header] = None
  361. # Parse CSV data
  362. for i, row in enumerate(reader, start=1):
  363. if len(row) != len(headers):
  364. raise forms.ValidationError(f"Row {i}: Expected {len(headers)} columns but found {len(row)}")
  365. row = [col.strip() for col in row]
  366. record = dict(zip(headers.keys(), row))
  367. records.append(record)
  368. return headers, records
  369. def validate(self, value):
  370. headers, records = value
  371. # Validate provided column headers
  372. for field, to_field in headers.items():
  373. if field not in self.fields:
  374. raise forms.ValidationError(f'Unexpected column header "{field}" found.')
  375. if to_field and not hasattr(self.fields[field], 'to_field_name'):
  376. raise forms.ValidationError(f'Column "{field}" is not a related object; cannot use dots')
  377. if to_field and not hasattr(self.fields[field].queryset.model, to_field):
  378. raise forms.ValidationError(f'Invalid related object attribute for column "{field}": {to_field}')
  379. # Validate required fields
  380. for f in self.required_fields:
  381. if f not in headers:
  382. raise forms.ValidationError(f'Required column header "{f}" not found.')
  383. return value
  384. class CSVChoiceField(forms.ChoiceField):
  385. """
  386. Invert the provided set of choices to take the human-friendly label as input, and return the database value.
  387. """
  388. def __init__(self, choices, *args, **kwargs):
  389. super().__init__(choices=choices, *args, **kwargs)
  390. self.choices = [(label, label) for value, label in unpack_grouped_choices(choices)]
  391. self.choice_values = {label: value for value, label in unpack_grouped_choices(choices)}
  392. def clean(self, value):
  393. value = super().clean(value)
  394. if not value:
  395. return ''
  396. if value not in self.choice_values:
  397. raise forms.ValidationError("Invalid choice: {}".format(value))
  398. return self.choice_values[value]
  399. class ExpandableNameField(forms.CharField):
  400. """
  401. A field which allows for numeric range expansion
  402. Example: 'Gi0/[1-3]' => ['Gi0/1', 'Gi0/2', 'Gi0/3']
  403. """
  404. def __init__(self, *args, **kwargs):
  405. super().__init__(*args, **kwargs)
  406. if not self.help_text:
  407. self.help_text = """
  408. Alphanumeric ranges are supported for bulk creation. Mixed cases and types within a single range
  409. are not supported. Examples:
  410. <ul>
  411. <li><code>[ge,xe]-0/0/[0-9]</code></li>
  412. <li><code>e[0-3][a-d,f]</code></li>
  413. </ul>
  414. """
  415. def to_python(self, value):
  416. if re.search(ALPHANUMERIC_EXPANSION_PATTERN, value):
  417. return list(expand_alphanumeric_pattern(value))
  418. return [value]
  419. class ExpandableIPAddressField(forms.CharField):
  420. """
  421. A field which allows for expansion of IP address ranges
  422. Example: '192.0.2.[1-254]/24' => ['192.0.2.1/24', '192.0.2.2/24', '192.0.2.3/24' ... '192.0.2.254/24']
  423. """
  424. def __init__(self, *args, **kwargs):
  425. super().__init__(*args, **kwargs)
  426. if not self.help_text:
  427. self.help_text = 'Specify a numeric range to create multiple IPs.<br />'\
  428. 'Example: <code>192.0.2.[1,5,100-254]/24</code>'
  429. def to_python(self, value):
  430. # Hackish address family detection but it's all we have to work with
  431. if '.' in value and re.search(IP4_EXPANSION_PATTERN, value):
  432. return list(expand_ipaddress_pattern(value, 4))
  433. elif ':' in value and re.search(IP6_EXPANSION_PATTERN, value):
  434. return list(expand_ipaddress_pattern(value, 6))
  435. return [value]
  436. class CommentField(forms.CharField):
  437. """
  438. A textarea with support for Markdown rendering. Exists mostly just to add a standard help_text.
  439. """
  440. widget = forms.Textarea
  441. default_label = ''
  442. # TODO: Port Markdown cheat sheet to internal documentation
  443. default_helptext = '<i class="fa fa-info-circle"></i> '\
  444. '<a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" target="_blank">'\
  445. 'Markdown</a> syntax is supported'
  446. def __init__(self, *args, **kwargs):
  447. required = kwargs.pop('required', False)
  448. label = kwargs.pop('label', self.default_label)
  449. help_text = kwargs.pop('help_text', self.default_helptext)
  450. super().__init__(required=required, label=label, help_text=help_text, *args, **kwargs)
  451. class FlexibleModelChoiceField(forms.ModelChoiceField):
  452. """
  453. Allow a model to be reference by either '{ID}' or the field specified by `to_field_name`.
  454. """
  455. def to_python(self, value):
  456. if value in self.empty_values:
  457. return None
  458. try:
  459. if not self.to_field_name:
  460. key = 'pk'
  461. elif re.match(r'^\{\d+\}$', value):
  462. key = 'pk'
  463. value = value.strip('{}')
  464. else:
  465. key = self.to_field_name
  466. value = self.queryset.get(**{key: value})
  467. except (ValueError, TypeError, self.queryset.model.DoesNotExist):
  468. raise forms.ValidationError(self.error_messages['invalid_choice'], code='invalid_choice')
  469. return value
  470. class SlugField(forms.SlugField):
  471. """
  472. Extend the built-in SlugField to automatically populate from a field called `name` unless otherwise specified.
  473. """
  474. def __init__(self, slug_source='name', *args, **kwargs):
  475. label = kwargs.pop('label', "Slug")
  476. help_text = kwargs.pop('help_text', "URL-friendly unique shorthand")
  477. widget = kwargs.pop('widget', SlugWidget)
  478. super().__init__(label=label, help_text=help_text, widget=widget, *args, **kwargs)
  479. self.widget.attrs['slug-source'] = slug_source
  480. class TagFilterField(forms.MultipleChoiceField):
  481. """
  482. A filter field for the tags of a model. Only the tags used by a model are displayed.
  483. :param model: The model of the filter
  484. """
  485. widget = StaticSelect2Multiple
  486. def __init__(self, model, *args, **kwargs):
  487. def get_choices():
  488. tags = model.tags.annotate(count=Count('extras_taggeditem_items')).order_by('name')
  489. return [(str(tag.slug), '{} ({})'.format(tag.name, tag.count)) for tag in tags]
  490. # Choices are fetched each time the form is initialized
  491. super().__init__(label='Tags', choices=get_choices, required=False, *args, **kwargs)
  492. class DynamicModelChoiceMixin:
  493. filter = django_filters.ModelChoiceFilter
  494. widget = APISelect
  495. def __init__(self, *args, **kwargs):
  496. super().__init__(*args, **kwargs)
  497. def get_bound_field(self, form, field_name):
  498. bound_field = BoundField(form, self, field_name)
  499. # Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options
  500. # will be populated on-demand via the APISelect widget.
  501. data = self.prepare_value(bound_field.data or bound_field.initial)
  502. if data:
  503. filter = self.filter(field_name=self.to_field_name or 'pk', queryset=self.queryset)
  504. self.queryset = filter.filter(self.queryset, data)
  505. else:
  506. self.queryset = self.queryset.none()
  507. # Set the data URL on the APISelect widget (if not already set)
  508. widget = bound_field.field.widget
  509. if not widget.attrs.get('data-url'):
  510. app_label = self.queryset.model._meta.app_label
  511. model_name = self.queryset.model._meta.model_name
  512. data_url = reverse('{}-api:{}-list'.format(app_label, model_name))
  513. widget.attrs['data-url'] = data_url
  514. return bound_field
  515. class DynamicModelChoiceField(DynamicModelChoiceMixin, forms.ModelChoiceField):
  516. """
  517. Override get_bound_field() to avoid pre-populating field choices with a SQL query. The field will be
  518. rendered only with choices set via bound data. Choices are populated on-demand via the APISelect widget.
  519. """
  520. pass
  521. class DynamicModelMultipleChoiceField(DynamicModelChoiceMixin, forms.ModelMultipleChoiceField):
  522. """
  523. A multiple-choice version of DynamicModelChoiceField.
  524. """
  525. filter = django_filters.ModelMultipleChoiceFilter
  526. widget = APISelectMultiple
  527. class LaxURLField(forms.URLField):
  528. """
  529. Modifies Django's built-in URLField in two ways:
  530. 1) Allow any valid scheme per RFC 3986 section 3.1
  531. 2) Remove the requirement for fully-qualified domain names (e.g. http://myserver/ is valid)
  532. """
  533. default_validators = [EnhancedURLValidator()]
  534. class JSONField(_JSONField):
  535. """
  536. Custom wrapper around Django's built-in JSONField to avoid presenting "null" as the default text.
  537. """
  538. def __init__(self, *args, **kwargs):
  539. super().__init__(*args, **kwargs)
  540. if not self.help_text:
  541. self.help_text = 'Enter context data in <a href="https://json.org/">JSON</a> format.'
  542. self.widget.attrs['placeholder'] = ''
  543. def prepare_value(self, value):
  544. if isinstance(value, InvalidJSONInput):
  545. return value
  546. if value is None:
  547. return ''
  548. return json.dumps(value, sort_keys=True, indent=4)
  549. #
  550. # Forms
  551. #
  552. class BootstrapMixin(forms.BaseForm):
  553. """
  554. Add the base Bootstrap CSS classes to form elements.
  555. """
  556. def __init__(self, *args, **kwargs):
  557. super().__init__(*args, **kwargs)
  558. exempt_widgets = [
  559. forms.CheckboxInput, forms.ClearableFileInput, forms.FileInput, forms.RadioSelect
  560. ]
  561. for field_name, field in self.fields.items():
  562. if field.widget.__class__ not in exempt_widgets:
  563. css = field.widget.attrs.get('class', '')
  564. field.widget.attrs['class'] = ' '.join([css, 'form-control']).strip()
  565. if field.required and not isinstance(field.widget, forms.FileInput):
  566. field.widget.attrs['required'] = 'required'
  567. if 'placeholder' not in field.widget.attrs:
  568. field.widget.attrs['placeholder'] = field.label
  569. class ReturnURLForm(forms.Form):
  570. """
  571. Provides a hidden return URL field to control where the user is directed after the form is submitted.
  572. """
  573. return_url = forms.CharField(required=False, widget=forms.HiddenInput())
  574. class ConfirmationForm(BootstrapMixin, ReturnURLForm):
  575. """
  576. A generic confirmation form. The form is not valid unless the confirm field is checked.
  577. """
  578. confirm = forms.BooleanField(required=True, widget=forms.HiddenInput(), initial=True)
  579. class BulkEditForm(forms.Form):
  580. """
  581. Base form for editing multiple objects in bulk
  582. """
  583. def __init__(self, model, *args, **kwargs):
  584. super().__init__(*args, **kwargs)
  585. self.model = model
  586. self.nullable_fields = []
  587. # Copy any nullable fields defined in Meta
  588. if hasattr(self.Meta, 'nullable_fields'):
  589. self.nullable_fields = self.Meta.nullable_fields
  590. class ImportForm(BootstrapMixin, forms.Form):
  591. """
  592. Generic form for creating an object from JSON/YAML data
  593. """
  594. data = forms.CharField(
  595. widget=forms.Textarea,
  596. help_text="Enter object data in JSON or YAML format. Note: Only a single object/document is supported."
  597. )
  598. format = forms.ChoiceField(
  599. choices=(
  600. ('json', 'JSON'),
  601. ('yaml', 'YAML')
  602. ),
  603. initial='yaml'
  604. )
  605. def clean(self):
  606. data = self.cleaned_data['data']
  607. format = self.cleaned_data['format']
  608. # Process JSON/YAML data
  609. if format == 'json':
  610. try:
  611. self.cleaned_data['data'] = json.loads(data)
  612. # Check for multiple JSON objects
  613. if type(self.cleaned_data['data']) is not dict:
  614. raise forms.ValidationError({
  615. 'data': "Import is limited to one object at a time."
  616. })
  617. except json.decoder.JSONDecodeError as err:
  618. raise forms.ValidationError({
  619. 'data': "Invalid JSON data: {}".format(err)
  620. })
  621. else:
  622. # Check for multiple YAML documents
  623. if '\n---' in data:
  624. raise forms.ValidationError({
  625. 'data': "Import is limited to one object at a time."
  626. })
  627. try:
  628. self.cleaned_data['data'] = yaml.load(data, Loader=yaml.SafeLoader)
  629. except yaml.error.YAMLError as err:
  630. raise forms.ValidationError({
  631. 'data': "Invalid YAML data: {}".format(err)
  632. })