0003_categories_roles.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.db import migrations, models
  4. from django.utils.translation import ugettext as _
  5. from misago.core import serializer
  6. def pickle_permissions(role, permissions):
  7. role.pickled_permissions = serializer.dumps(permissions)
  8. def create_default_categories_roles(apps, schema_editor):
  9. """
  10. Crete roles
  11. """
  12. CategoryRole = apps.get_model('misago_categories', 'CategoryRole')
  13. see_only = CategoryRole(name=_('See only'))
  14. pickle_permissions(see_only,
  15. {
  16. # categories perms
  17. 'misago.categories.permissions': {
  18. 'can_see': 1,
  19. 'can_browse': 0
  20. },
  21. })
  22. see_only.save()
  23. read_only = CategoryRole(name=_('Read only'))
  24. pickle_permissions(read_only,
  25. {
  26. # categories perms
  27. 'misago.categories.permissions': {
  28. 'can_see': 1,
  29. 'can_browse': 1
  30. },
  31. # threads perms
  32. 'misago.threads.permissions.threads': {
  33. 'can_see_all_threads': 1,
  34. 'can_see_posts_likes': 2,
  35. 'can_download_other_users_attachments': 1,
  36. 'can_like_posts': 1
  37. },
  38. })
  39. read_only.save()
  40. reply_only = CategoryRole(name=_('Reply to threads'))
  41. pickle_permissions(reply_only,
  42. {
  43. # categories perms
  44. 'misago.categories.permissions': {
  45. 'can_see': 1,
  46. 'can_browse': 1
  47. },
  48. # threads perms
  49. 'misago.threads.permissions.threads': {
  50. 'can_see_all_threads': 1,
  51. 'can_reply_threads': 1,
  52. 'can_edit_posts': 1,
  53. 'can_download_other_users_attachments': 1,
  54. 'max_attachment_size': 500,
  55. 'can_see_posts_likes': 2,
  56. 'can_like_posts': 1
  57. },
  58. })
  59. reply_only.save()
  60. standard = CategoryRole(name=_('Start and reply threads'))
  61. pickle_permissions(standard,
  62. {
  63. # categories perms
  64. 'misago.categories.permissions': {
  65. 'can_see': 1,
  66. 'can_browse': 1
  67. },
  68. # threads perms
  69. 'misago.threads.permissions.threads': {
  70. 'can_see_all_threads': 1,
  71. 'can_start_threads': 1,
  72. 'can_reply_threads': 1,
  73. 'can_edit_threads': 1,
  74. 'can_edit_posts': 1,
  75. 'can_download_other_users_attachments': 1,
  76. 'max_attachment_size': 500,
  77. 'can_see_posts_likes': 2,
  78. 'can_like_posts': 1
  79. },
  80. })
  81. standard.save()
  82. standard_with_polls = CategoryRole(name=_('Start and reply threads, make polls'))
  83. pickle_permissions(standard_with_polls,
  84. {
  85. # categories perms
  86. 'misago.categories.permissions': {
  87. 'can_see': 1,
  88. 'can_browse': 1,
  89. },
  90. # threads perms
  91. 'misago.threads.permissions.threads': {
  92. 'can_see_all_threads': 1,
  93. 'can_start_threads': 1,
  94. 'can_reply_threads': 1,
  95. 'can_edit_threads': 1,
  96. 'can_edit_posts': 1,
  97. 'can_download_other_users_attachments': 1,
  98. 'max_attachment_size': 500,
  99. 'can_see_posts_likes': 2,
  100. 'can_like_posts': 1
  101. },
  102. })
  103. standard_with_polls.save()
  104. moderator = CategoryRole(name=_('Moderator'))
  105. pickle_permissions(moderator,
  106. {
  107. # categories perms
  108. 'misago.categories.permissions': {
  109. 'can_see': 1,
  110. 'can_browse': 1
  111. },
  112. # threads perms
  113. 'misago.threads.permissions.threads': {
  114. 'can_see_all_threads': 1,
  115. 'can_start_threads': 1,
  116. 'can_reply_threads': 1,
  117. 'can_edit_threads': 2,
  118. 'can_edit_posts': 2,
  119. 'can_hide_own_threads': 2,
  120. 'can_hide_own_posts': 2,
  121. 'thread_edit_time': 0,
  122. 'post_edit_time': 0,
  123. 'can_hide_threads': 2,
  124. 'can_hide_posts': 2,
  125. 'can_protect_posts': 1,
  126. 'can_move_posts': 1,
  127. 'can_merge_posts': 1,
  128. 'can_announce_threads': 1,
  129. 'can_pin_threads': 2,
  130. 'can_close_threads': 1,
  131. 'can_move_threads': 1,
  132. 'can_merge_threads': 1,
  133. 'can_approve_content': 1,
  134. 'can_download_other_users_attachments': 1,
  135. 'max_attachment_size': 2500,
  136. 'can_delete_other_users_attachments': 1,
  137. 'can_see_posts_likes': 2,
  138. 'can_like_posts': 1,
  139. 'can_report_content': 1,
  140. 'can_see_reports': 1,
  141. 'can_hide_events': 2
  142. },
  143. })
  144. moderator.save()
  145. """
  146. Assign category roles to roles
  147. """
  148. Category = apps.get_model('misago_categories', 'Category')
  149. Role = apps.get_model('misago_acl', 'Role')
  150. RoleCategoryACL = apps.get_model('misago_categories', 'RoleCategoryACL')
  151. moderators = Role.objects.get(name=_('Moderator'))
  152. members = Role.objects.get(special_role='authenticated')
  153. guests = Role.objects.get(special_role='anonymous')
  154. category = Category.objects.get(tree_id=1, level=1)
  155. RoleCategoryACL.objects.bulk_create([
  156. RoleCategoryACL(
  157. role=moderators,
  158. category=category,
  159. category_role=moderator
  160. ),
  161. RoleCategoryACL(
  162. role=members,
  163. category=category,
  164. category_role=standard
  165. ),
  166. RoleCategoryACL(
  167. role=guests,
  168. category=category,
  169. category_role=read_only
  170. ),
  171. ])
  172. class Migration(migrations.Migration):
  173. dependencies = [
  174. ('misago_categories', '0002_default_categories'),
  175. ('misago_acl', '0003_default_roles'),
  176. ]
  177. operations = [
  178. migrations.RunPython(create_default_categories_roles),
  179. ]