11# Assign the contributors group to users according to #7364
2- from django .contrib .auth import get_user_model
3- from django .contrib .auth .models import Group , Permission
4- from django .contrib .contenttypes .models import ContentType
5-
62from django .db import migrations
73from django .db .migrations .operations import RunPython
84
95
106def forwards_func (apps , schema_editor ):
117 from geonode .groups .conf import settings as groups_settings
8+ Group = apps .get_model ("auth" , "Group" )
9+ Permission = apps .get_model ("auth" , "Permission" )
10+ ContentType = apps .get_model ("contenttypes" , "ContentType" )
11+ Profile = apps .get_model ("people" , "Profile" )
1212
1313 # assign contributors group to users
1414 cont_group , _ = Group .objects .get_or_create (name = 'contributors' )
@@ -21,18 +21,22 @@ def forwards_func(apps, schema_editor):
2121 )
2222 if perm :
2323 cont_group .permissions .add (perm )
24- # Exclude admins and anonymous user
25- users_to_update = get_user_model ().objects .filter (pk__gt = 0 )
24+
25+ # Exclude anonymous user
26+ users_to_update = Profile .objects .exclude (username = "AnonymousUser" )
2627 for user in users_to_update :
2728 registeredmembers_group .user_set .add (user )
29+ # Exclude additional staff and superusers also
2830 for user in users_to_update .exclude (is_staff = True , is_superuser = True ):
2931 cont_group .user_set .add (user )
3032
3133def reverse_func (apps , schema_editor ):
3234 # remove contributors group from users
3335 try :
36+ Group = apps .get_model ("auth" , "Group" )
37+ Profile = apps .get_model ("people" , "Profile" )
3438 cont_group = Group .objects .get (name = 'contributors' )
35- users_to_update = get_user_model () .objects .filter (
39+ users_to_update = Profile .objects .filter (
3640 pk__gt = 0 , is_staff = False , is_superuser = False
3741 )
3842 for user in users_to_update :
0 commit comments