attachments.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 don't allow uploading end deleting 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_delete': True,
  52. })
  53. else:
  54. attachment.acl.update({
  55. 'can_delete': user.is_authenticated() and user.acl['can_delete_other_users_attachments'],
  56. })
  57. def register_with(registry):
  58. registry.acl_annotator(Attachment, add_acl_to_attachment)