Skip to content

Commit

Permalink
refactor: merge AppP and ModC body roles
Browse files Browse the repository at this point in the history
 - Remove the AppP body role
 - Rename the ModC body role
 - Replace all occurences of AppP with ModC
 - Migrate all records in DB that refereced AppP to now use ModC
  • Loading branch information
sethigeet committed Dec 17, 2023
1 parent 0f0f00c commit 2835de8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
6 changes: 4 additions & 2 deletions community/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
self.test_body_2 = create_body()

self.body_1_role = BodyRole.objects.create(
name="Body1Role", body=self.test_body_1, permissions="AppP,ModC"
name="Body1Role", body=self.test_body_1, permissions="ModC"
)
self.user1.profile.roles.add(self.body_1_role)

Expand Down Expand Up @@ -89,7 +89,9 @@ def test_communitypost_yourlist(self):
self.assertEqual(
response.data["count"],
CommunityPost.objects.filter(
thread_rank=1, posted_by=self.user1.profile, community=self.test_community_1
thread_rank=1,
posted_by=self.user1.profile,
community=self.test_community_1,
).count(),
)
self.assertListEqual(
Expand Down
13 changes: 5 additions & 8 deletions community/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ def change_status(self, request, pk):
post = self.get_community_post(pk)

if (
user_has_privilege(request.user.profile, post.community.body.id, "AppP")
and post.thread_rank == 1
) or (
user_has_privilege(request.user.profile, post.community.body.id, "ModC")
and post.thread_rank > 1
and post.thread_rank > 0
):
# Get query param
status = request.data["status"]
Expand Down Expand Up @@ -72,7 +69,7 @@ def retrieve_full(self, request, pk):
)
post = self.get_community_post(pk)
return_for_mod = False
if user_has_privilege(request.user.profile, post.community.body.id, "AppP"):
if user_has_privilege(request.user.profile, post.community.body.id, "ModC"):
return_for_mod = True
serialized = CommunityPostSerializers(
post, context={"return_for_mod": return_for_mod}
Expand Down Expand Up @@ -112,7 +109,7 @@ def list(self, request):
queryset = query_search(request, 3, queryset, ["content"], "posts")
queryset = query_from_num(request, 20, queryset)
return_for_mod = False
if user_has_privilege(request.user.profile, community.body.id, "AppP"):
if user_has_privilege(request.user.profile, community.body.id, "ModC"):
return_for_mod = True

serializer = CommunityPostSerializerMin(
Expand Down Expand Up @@ -153,7 +150,7 @@ def perform_action(self, request, action, pk):
if all(
[
user_has_privilege(
request.user.profile, post.community.body.id, "AppP"
request.user.profile, post.community.body.id, "ModC"
)
]
):
Expand Down Expand Up @@ -184,7 +181,7 @@ def perform_action(self, request, action, pk):
if all(
[
user_has_privilege(
request.user.profile, post.community.body.id, "AppP"
request.user.profile, post.community.body.id, "ModC"
)
]
):
Expand Down
2 changes: 1 addition & 1 deletion other/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def notify_new_commpostadmin(pk):
roles = instance.community.body.roles.all()
users = []
for role in roles:
if "AppP" in role.permissions:
if "ModC" in role.permissions:
users.extend(map(lambda user: user.user, role.users.all()))
print(users)
notify.send(
Expand Down
40 changes: 40 additions & 0 deletions roles/migrations/0022_alter_bodyrole_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 3.2.16 on 2023-12-16 12:02

from django.db import migrations
import multiselectfield.db.fields


def migrate_AppP_role_to_ModC_role(apps, schema_editor):
BodyRole = apps.get_model("roles", "BodyRole")

for obj in BodyRole.objects.filter(permissions__contains="AppP"):
obj.permissions.remove("AppP")
if "ModC" not in obj.permissions:
obj.permissions.append("ModC")
obj.save()


class Migration(migrations.Migration):
dependencies = [
("roles", "0021_delete_communityrole"),
]

operations = [
migrations.AlterField(
model_name="bodyrole",
name="permissions",
field=multiselectfield.db.fields.MultiSelectField(
choices=[
("AddE", "Add Event"),
("UpdE", "Update Event"),
("DelE", "Delete Event"),
("UpdB", "Update Body"),
("Role", "Modify Roles"),
("VerA", "Verify Achievements"),
("ModC", "Moderate Community"),
],
max_length=34,
),
),
migrations.RunPython(migrate_AppP_role_to_ModC_role),
]
3 changes: 1 addition & 2 deletions roles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
("UpdB", "Update Body"),
("Role", "Modify Roles"),
("VerA", "Verify Achievements"),
("AppP", "Moderate Post"),
("ModC", "Moderate Comment"),
("ModC", "Moderate Community"),
)


Expand Down

0 comments on commit 2835de8

Please sign in to comment.