forms.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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=(('', _("All")), ('yes', _("Only orphaned")), ('no', _("Not orphaned")), ),
  22. )
  23. def filter_queryset(self, criteria, queryset):
  24. if criteria.get('uploader'):
  25. queryset = queryset.filter(uploader_slug__contains=criteria['uploader'].lower())
  26. if criteria.get('filename'):
  27. queryset = queryset.filter(filename__icontains=criteria['filename'])
  28. if criteria.get('filetype'):
  29. queryset = queryset.filter(filetype_id=criteria['filetype'])
  30. if criteria.get('is_orphan') == 'yes':
  31. queryset = queryset.filter(post__isnull=True)
  32. elif criteria.get('is_orphan') == 'no':
  33. queryset = queryset.filter(post__isnull=False)
  34. return queryset
  35. class AttachmentTypeForm(forms.ModelForm):
  36. class Meta:
  37. model = AttachmentType
  38. fields = '__all__'
  39. labels = {
  40. 'name': _("Type name"),
  41. 'extensions': _("File extensions"),
  42. 'mimetypes': _("Mimetypes"),
  43. 'size_limit': _("Maximum allowed uploaded file size"),
  44. 'status': _("Status"),
  45. 'limit_uploads_to': _("Limit uploads to"),
  46. 'limit_downloads_to': _("Limit downloads to"),
  47. }
  48. help_texts = {
  49. 'extensions':
  50. _("List of comma separated file extensions associated with this attachment type."),
  51. 'mimetypes':
  52. _(
  53. "Optional list of comma separated mime types associated with this attachment type."
  54. ),
  55. 'size_limit':
  56. _(
  57. "Maximum allowed uploaded file size for this type, in kb. "
  58. "May be overriden via user permission."
  59. ),
  60. 'status':
  61. _("Controls this attachment type availability on your site."),
  62. 'limit_uploads_to':
  63. _(
  64. "If you wish to limit option to upload files of this type to users with specific "
  65. "roles, select them on this list. Otherwhise don't select any roles to allow all "
  66. "users with permission to upload attachments to be able to upload attachments of "
  67. "this type."
  68. ),
  69. 'limit_downloads_to':
  70. _(
  71. "If you wish to limit option to download files of this type to users with "
  72. "specific roles, select them on this list. Otherwhise don't select any roles to "
  73. "allow all users with permission to download attachments to be able to download "
  74. " attachments of this type."
  75. ),
  76. }
  77. widgets = {
  78. 'limit_uploads_to': forms.CheckboxSelectMultiple,
  79. 'limit_downloads_to': forms.CheckboxSelectMultiple,
  80. }
  81. def clean_extensions(self):
  82. data = self.clean_list(self.cleaned_data['extensions'])
  83. if not data:
  84. raise forms.ValidationError(_("This field is required."))
  85. return data
  86. def clean_mimetypes(self):
  87. return self.clean_list(self.cleaned_data['mimetypes'])
  88. def clean_list(self, value):
  89. items = [v.lstrip('.') for v in value.lower().replace(' ', '').split(',')]
  90. return ','.join(set(filter(bool, items)))