forms.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from django import forms
  2. from django.utils.translation import ugettext as _
  3. from .models import AttachmentType
  4. class AttachmentTypeForm(forms.ModelForm):
  5. class Meta:
  6. model = AttachmentType
  7. fields = '__all__'
  8. labels = {
  9. 'name': _("Type name"),
  10. 'extensions': _("File extensions"),
  11. 'mimetypes': _("Mimetypes"),
  12. 'size_limit': _("Maximum allowed uploaded file size"),
  13. 'status': _("Status"),
  14. 'limit_uploads_to': _("Limit uploads to"),
  15. 'limit_downloads_to': _("Limit downloads to"),
  16. }
  17. help_texts = {
  18. 'extensions': _("List of comma separated file extensions associated with this attachment type."),
  19. 'mimetypes': _("Optional list of comma separated mime types associated with this attachment type."),
  20. 'size_limit': _("Maximum allowed uploaded file size for this type, in kb. "
  21. "May be overriden via user permission."),
  22. 'status': _("Controls this attachment type availability on your site."),
  23. 'limit_uploads_to': _("If you wish to limit option to upload files of this type to users with specific "
  24. "roles, select them on this list. Otherwhise don't select any roles to allow all "
  25. "users with permission to upload attachments to be able to upload attachments of "
  26. "this type."),
  27. 'limit_downloads_to': _("If you wish to limit option to download files of this type to users with "
  28. "specific roles, select them on this list. Otherwhise don't select any roles to "
  29. "allow all users with permission to download attachments to be able to download "
  30. " attachments of this type."),
  31. }
  32. widgets = {
  33. 'limit_uploads_to': forms.CheckboxSelectMultiple,
  34. 'limit_downloads_to': forms.CheckboxSelectMultiple,
  35. }
  36. def clean_extensions(self):
  37. data = self.clean_list(self.cleaned_data['extensions'])
  38. if not data:
  39. raise forms.ValidationError(_("This field is required."))
  40. return data
  41. def clean_mimetypes(self):
  42. return self.clean_list(self.cleaned_data['mimetypes'])
  43. def clean_list(self, value):
  44. items = [v.lstrip('.') for v in value.lower().replace(' ', '').split(',')]
  45. return ','.join(set(filter(bool, items)))