forms.py 5.7 KB

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