forms.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from django import forms
  2. from django.utils.translation import ugettext as _
  3. from .models import AttachmentType
  4. def get_searchable_filetypes():
  5. choices = [(0, _("All types"))]
  6. choices += [(a.id, a.name) for a in AttachmentType.objects.order_by('name')]
  7. return choices
  8. class SearchAttachmentsForm(forms.Form):
  9. uploader = forms.CharField(label=_("Uploader name contains"), required=False)
  10. filename = forms.CharField(label=_("Filename contains"), required=False)
  11. filetype = forms.TypedChoiceField(
  12. label=_("File type"),
  13. coerce=int,
  14. choices=get_searchable_filetypes,
  15. empty_value=0,
  16. required=False
  17. )
  18. is_orphan = forms.ChoiceField(
  19. label=_("State"),
  20. required=False,
  21. choices=(
  22. ('', _("All")),
  23. ('yes', _("Only orphaned")),
  24. ('no', _("Not orphaned")),
  25. ),
  26. )
  27. def filter_queryset(self, criteria, queryset):
  28. if criteria.get('uploader'):
  29. queryset = queryset.filter(uploader_slug__contains=criteria['uploader'].lower())
  30. if criteria.get('filename'):
  31. queryset = queryset.filter(filename__icontains=criteria['filename'])
  32. if criteria.get('filetype'):
  33. queryset = queryset.filter(filetype_id=criteria['filetype'])
  34. if criteria.get('is_orphan') == 'yes':
  35. queryset = queryset.filter(post__isnull=True)
  36. elif criteria.get('is_orphan') == 'no':
  37. queryset = queryset.filter(post__isnull=False)
  38. return queryset
  39. class AttachmentTypeForm(forms.ModelForm):
  40. class Meta:
  41. model = AttachmentType
  42. fields = '__all__'
  43. labels = {
  44. 'name': _("Type name"),
  45. 'extensions': _("File extensions"),
  46. 'mimetypes': _("Mimetypes"),
  47. 'size_limit': _("Maximum allowed uploaded file size"),
  48. 'status': _("Status"),
  49. 'limit_uploads_to': _("Limit uploads to"),
  50. 'limit_downloads_to': _("Limit downloads to"),
  51. }
  52. help_texts = {
  53. 'extensions': _("List of comma separated file extensions associated with this attachment type."),
  54. 'mimetypes': _("Optional list of comma separated mime types associated with this attachment type."),
  55. 'size_limit': _("Maximum allowed uploaded file size for this type, in kb. "
  56. "May be overriden via user permission."),
  57. 'status': _("Controls this attachment type availability on your site."),
  58. 'limit_uploads_to': _("If you wish to limit option to upload files of this type to users with specific "
  59. "roles, select them on this list. Otherwhise don't select any roles to allow all "
  60. "users with permission to upload attachments to be able to upload attachments of "
  61. "this type."),
  62. 'limit_downloads_to': _("If you wish to limit option to download files of this type to users with "
  63. "specific roles, select them on this list. Otherwhise don't select any roles to "
  64. "allow all users with permission to download attachments to be able to download "
  65. " attachments of this type."),
  66. }
  67. widgets = {
  68. 'limit_uploads_to': forms.CheckboxSelectMultiple,
  69. 'limit_downloads_to': forms.CheckboxSelectMultiple,
  70. }
  71. def clean_extensions(self):
  72. data = self.clean_list(self.cleaned_data['extensions'])
  73. if not data:
  74. raise forms.ValidationError(_("This field is required."))
  75. return data
  76. def clean_mimetypes(self):
  77. return self.clean_list(self.cleaned_data['mimetypes'])
  78. def clean_list(self, value):
  79. items = [v.lstrip('.') for v in value.lower().replace(' ', '').split(',')]
  80. return ','.join(set(filter(bool, items)))