Skip to content

Commit

Permalink
use migration to set contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
marthamareal committed May 3, 2021
1 parent 1716aaf commit 3fe0e32
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
4 changes: 2 additions & 2 deletions geonode/base/fixtures/group_test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
},
{
"pk": 2,
"pk": 3,
"model": "auth.group",
"fields": {
"name": "registered-members",
Expand All @@ -22,7 +22,7 @@
"slug": "registered-members",
"last_modified": "2019-09-09 15:45:34",
"created": "2019-09-09 15:45:34",
"group": 2,
"group": 3,
"title": "Registered Members"
},
"model": "groups.groupprofile",
Expand Down
28 changes: 0 additions & 28 deletions geonode/people/management/commands/setcontributorsgroup.py

This file was deleted.

49 changes: 49 additions & 0 deletions geonode/people/migrations/0032_set_contributors_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Assign the contributors group to users according to #7364
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType

from django.db import migrations
from django.db.migrations.operations import RunPython


def forwards_func(apps, schema_editor):
# assign contributors group to users
cont_group, created = Group.objects.get_or_create(name='contributors')
ctype = ContentType.objects.get_for_model(cont_group)
perm, created = Permission.objects.get_or_create(
codename='base_addresourcebase',
name='Can add resources',
content_type=ctype,
)
if perm:
cont_group.permissions.add(perm)
# Exclude admins and anonymous user
users_to_update = get_user_model().objects.filter(
pk__gt=0, is_staff=False, is_superuser=False
)
for user in users_to_update:
cont_group.user_set.add(user)

def reverse_func(apps, schema_editor):
# remove contributors group from users
try:
cont_group = Group.objects.get(name='contributors')
users_to_update = get_user_model().objects.filter(
pk__gt=0, is_staff=False, is_superuser=False
)
for user in users_to_update:
user.groups.remove(cont_group)
except Exception:
pass


class Migration(migrations.Migration):

dependencies = [
('people', '0031_merge_20210205_0824'),
]

operations = [
RunPython(forwards_func, reverse_func)
]

0 comments on commit 3fe0e32

Please sign in to comment.