Skip to content

Commit

Permalink
Merge pull request #5680 from nyaruka/exclude_sys_users_from_notifica…
Browse files Browse the repository at this point in the history
…tions

Don't send notifications to system users
  • Loading branch information
rowanseymour authored Nov 22, 2024
2 parents 0f1aaf7 + 47256a0 commit 500e93f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
4 changes: 2 additions & 2 deletions temba/orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,9 @@ def get_users(self, *, roles: list = None, with_perm: str = None):

def get_admins(self):
"""
Convenience method for getting all org administrators
Convenience method for getting all org administrators, excluding system users
"""
return self.get_users(roles=[OrgRole.ADMINISTRATOR])
return self.get_users(roles=[OrgRole.ADMINISTRATOR]).exclude(settings__is_system=True)

def has_user(self, user: User) -> bool:
"""
Expand Down
16 changes: 3 additions & 13 deletions temba/orgs/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,7 @@ def save(self, obj):
team = (team or self.request.org.default_ticket_team) if role == OrgRole.AGENT else None

# don't update if user is the last administrator and role is being changed to something else
has_other_admins = (
self.request.org.get_users(roles=[OrgRole.ADMINISTRATOR])
.exclude(id=obj.id)
.exclude(settings__is_system=True)
.exists()
)
has_other_admins = self.request.org.get_admins().exclude(id=obj.id).exists()
if role != OrgRole.ADMINISTRATOR and not has_other_admins:
return obj

Expand Down Expand Up @@ -575,13 +570,8 @@ def get_context_data(self, **kwargs):
def post(self, request, *args, **kwargs):
user = self.get_object()

# only actually remove user if they're not the last administator or a system user only for staff
if (
self.request.org.get_users(roles=[OrgRole.ADMINISTRATOR])
.exclude(id=user.id)
.exclude(settings__is_system=True)
.exists()
):
# only actually remove user if they're not the last administator
if self.request.org.get_admins().exclude(id=user.id).exists():
self.request.org.remove_user(user)

return HttpResponseRedirect(self.get_redirect_url())
Expand Down

0 comments on commit 500e93f

Please sign in to comment.