forms.py 4.1 KB

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