attachments.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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(label=_("Can download other users attachments"))
  19. can_delete_other_users_attachments = YesNoSwitch(label=_("Can delete other users attachments"))
  20. class AnonymousPermissionsForm(forms.Form):
  21. legend = _("Attachments")
  22. can_download_other_users_attachments = YesNoSwitch(label=_("Can download attachments"))
  23. def change_permissions_form(role):
  24. if isinstance(role, Role):
  25. if role.special_role != 'anonymous':
  26. return PermissionsForm
  27. else:
  28. return AnonymousPermissionsForm
  29. else:
  30. return None
  31. """
  32. ACL Builder
  33. """
  34. def build_acl(acl, roles, key_name):
  35. new_acl = {
  36. 'max_attachment_size': 0,
  37. 'can_download_other_users_attachments': False,
  38. 'can_delete_other_users_attachments': False,
  39. }
  40. new_acl.update(acl)
  41. return algebra.sum_acls(new_acl, roles=roles, key=key_name,
  42. max_attachment_size=algebra.greater,
  43. can_download_other_users_attachments=algebra.greater,
  44. can_delete_other_users_attachments=algebra.greater
  45. )
  46. """
  47. ACL's for targets
  48. """
  49. def add_acl_to_attachment(user, attachment):
  50. if user.is_authenticated and user.id == attachment.uploader_id:
  51. attachment.acl.update({
  52. 'can_delete': True,
  53. })
  54. else:
  55. attachment.acl.update({
  56. 'can_delete': user.is_authenticated and user.acl['can_delete_other_users_attachments'],
  57. })
  58. def register_with(registry):
  59. registry.acl_annotator(Attachment, add_acl_to_attachment)