forms.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """
  2. Enhancements to wagtail.contrib.forms.
  3. """
  4. import csv
  5. import os
  6. from django import forms
  7. from django.contrib.contenttypes.models import ContentType
  8. from django.core.exceptions import ValidationError
  9. from django.db import models
  10. from django.http import HttpResponse
  11. from django.utils.translation import gettext_lazy as _
  12. from wagtail.contrib.forms.views import SubmissionsListView as WagtailSubmissionsListView
  13. from wagtail.contrib.forms.forms import FormBuilder
  14. from wagtail.contrib.forms.models import AbstractFormField
  15. from wagtailcrx.settings import crx_settings
  16. from wagtailcrx.utils import attempt_protected_media_value_conversion
  17. FORM_FIELD_CHOICES = (
  18. (_("Text"), (
  19. ("singleline", _("Single line text")),
  20. ("multiline", _("Multi-line text")),
  21. ("email", _("Email")),
  22. ("number", _("Number - only allows integers")),
  23. ("url", _("URL")),
  24. ),),
  25. (_("Choice"), (
  26. ("checkboxes", _("Checkboxes")),
  27. ("dropdown", _("Drop down")),
  28. ("radio", _("Radio buttons")),
  29. ("multiselect", _("Multiple select")),
  30. ("checkbox", _("Single checkbox")),
  31. ),),
  32. (_("Date & Time"), (
  33. ("date", _("Date")),
  34. ("time", _("Time")),
  35. ("datetime", _("Date and time")),
  36. ),),
  37. (_("File Upload"), (
  38. ("file", _("Secure File - login required to access uploaded files")),
  39. ),),
  40. (_("Other"), (
  41. ("hidden", _("Hidden field")),
  42. ),),
  43. )
  44. # Files
  45. class SecureFileField(forms.FileField):
  46. custom_error_messages = {
  47. 'blacklist_file': _('Submitted file is not allowed.'),
  48. 'whitelist_file': _('Submitted file is not allowed.')
  49. }
  50. def __init__(self, **kwargs):
  51. super().__init__(**kwargs)
  52. self.error_messages.update(self.custom_error_messages)
  53. def validate(self, value):
  54. super(SecureFileField, self).validate(value)
  55. if value:
  56. self._check_whitelist(value)
  57. self._check_blacklist(value)
  58. def _check_whitelist(self, value):
  59. if crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_WHITELIST:
  60. if os.path.splitext(value.name)[1].lower() not in crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_WHITELIST: # noqa
  61. raise ValidationError(self.error_messages['whitelist_file'])
  62. def _check_blacklist(self, value):
  63. if crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_BLACKLIST:
  64. if os.path.splitext(value.name)[1].lower() in crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_BLACKLIST: # noqa
  65. raise ValidationError(self.error_messages['blacklist_file'])
  66. # Date
  67. class CoderedDateInput(forms.DateInput):
  68. template_name = 'wagtailcrx/formfields/date.html'
  69. class CoderedDateField(forms.DateField):
  70. widget = CoderedDateInput()
  71. # Datetime
  72. class CoderedDateTimeInput(forms.DateTimeInput):
  73. template_name = 'wagtailcrx/formfields/datetime.html'
  74. class CoderedDateTimeField(forms.DateTimeField):
  75. widget = CoderedDateTimeInput()
  76. input_formats = ['%Y-%m-%dT%H:%M', '%m/%d/%Y %I:%M %p', '%m/%d/%Y %I:%M%p', '%m/%d/%Y %H:%M']
  77. # Time
  78. class CoderedTimeInput(forms.TimeInput):
  79. template_name = 'wagtailcrx/formfields/time.html'
  80. class CoderedTimeField(forms.TimeField):
  81. widget = CoderedTimeInput()
  82. input_formats = ['%H:%M', '%I:%M %p', '%I:%M%p']
  83. class CoderedFormBuilder(FormBuilder):
  84. """
  85. Enhance wagtail FormBuilder with additional custom fields.
  86. """
  87. def create_file_field(self, field, options):
  88. return SecureFileField(**options)
  89. def create_date_field(self, field, options):
  90. return CoderedDateField(**options)
  91. def create_datetime_field(self, field, options):
  92. return CoderedDateTimeField(**options)
  93. def create_time_field(self, field, options):
  94. return CoderedTimeField(**options)
  95. class CoderedSubmissionsListView(WagtailSubmissionsListView):
  96. def get_csv_response(self, context):
  97. filename = self.get_csv_filename()
  98. response = HttpResponse(content_type='text/csv; charset=utf-8')
  99. response['Content-Disposition'] = 'attachment;filename={}'.format(filename)
  100. writer = csv.writer(response)
  101. writer.writerow(context['data_headings'])
  102. for data_row in context['data_rows']:
  103. modified_data_row = []
  104. for cell in data_row:
  105. modified_cell = attempt_protected_media_value_conversion(self.request, cell)
  106. modified_data_row.append(modified_cell)
  107. writer.writerow(modified_data_row)
  108. return response
  109. class CoderedFormField(AbstractFormField):
  110. class Meta:
  111. abstract = True
  112. field_type = models.CharField(
  113. verbose_name=_('field type'),
  114. max_length=16,
  115. choices=FORM_FIELD_CHOICES,
  116. blank=False,
  117. default='Single line text'
  118. )
  119. class SearchForm(forms.Form):
  120. s = forms.CharField(
  121. max_length=255,
  122. required=False,
  123. label=_('Search'),
  124. )
  125. t = forms.CharField(
  126. widget=forms.HiddenInput,
  127. max_length=255,
  128. required=False,
  129. label=_('Page type'),
  130. )
  131. def get_page_model_choices():
  132. """
  133. Returns a list of tuples of all creatable Codered pages
  134. in the format of (app_label:model, "Verbose Name")
  135. """
  136. from wagtailcrx.models import get_page_models
  137. rval = []
  138. for page in get_page_models():
  139. if page.is_creatable:
  140. ct = ContentType.objects.get_for_model(page)
  141. rval.append((f"{ct.app_label}:{ct.model}", ct.name))
  142. return rval