attachments.py 2.1 KB

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