attachments.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django import forms
  2. from django.utils.translation import ugettext_lazy as _
  3. from misago.acl import algebra
  4. from misago.acl.models import Role
  5. from misago.core.forms import YesNoSwitch
  6. from misago.threads.models import Attachment
  7. """
  8. Admin Permissions Form
  9. """
  10. class PermissionsForm(forms.Form):
  11. legend = _("Attachments")
  12. max_attachment_size = forms.IntegerField(
  13. label=_("Max attached file size (in kb)"),
  14. help_text=_("Enter 0 to don't allow uploading end deleting attachments."),
  15. initial=500,
  16. min_value=0
  17. )
  18. can_download_other_users_attachments = YesNoSwitch(
  19. label=_("Can download other users attachments")
  20. )
  21. can_delete_other_users_attachments = YesNoSwitch(label=_("Can delete other users attachments"))
  22. class AnonymousPermissionsForm(forms.Form):
  23. legend = _("Attachments")
  24. can_download_other_users_attachments = YesNoSwitch(label=_("Can download attachments"))
  25. def change_permissions_form(role):
  26. if isinstance(role, Role):
  27. if role.special_role != 'anonymous':
  28. return PermissionsForm
  29. else:
  30. return AnonymousPermissionsForm
  31. else:
  32. return None
  33. """
  34. ACL Builder
  35. """
  36. def build_acl(acl, roles, key_name):
  37. new_acl = {
  38. 'max_attachment_size': 0,
  39. 'can_download_other_users_attachments': False,
  40. 'can_delete_other_users_attachments': False,
  41. }
  42. new_acl.update(acl)
  43. return algebra.sum_acls(
  44. new_acl,
  45. roles=roles,
  46. key=key_name,
  47. max_attachment_size=algebra.greater,
  48. can_download_other_users_attachments=algebra.greater,
  49. can_delete_other_users_attachments=algebra.greater
  50. )
  51. """
  52. ACL's for targets
  53. """
  54. def add_acl_to_attachment(user, attachment):
  55. if user.is_authenticated and user.id == attachment.uploader_id:
  56. attachment.acl.update({
  57. 'can_delete': True,
  58. })
  59. else:
  60. user_can_delete = user.acl_cache['can_delete_other_users_attachments']
  61. attachment.acl.update({
  62. 'can_delete': user.is_authenticated and user_can_delete,
  63. })
  64. def register_with(registry):
  65. registry.acl_annotator(Attachment, add_acl_to_attachment)