diff --git a/awx/main/models/organization.py b/awx/main/models/organization.py index 220dae07813a..1c77c9e5be06 100644 --- a/awx/main/models/organization.py +++ b/awx/main/models/organization.py @@ -161,8 +161,8 @@ class Meta: class UserSessionMembership(BaseModel): ''' A lookup table for API session membership given user. Note, there is a - different session created by channels for websockets using the same - underlying model. + different session created by channels for websockets using the same + underlying model. ''' class Meta: @@ -177,14 +177,14 @@ class Meta: created = models.DateTimeField(default=None, editable=False) @staticmethod - def get_memberships_over_limit(user, now=None): + def get_memberships_over_limit(user_id, now=None): if settings.SESSIONS_PER_USER == -1: return [] if now is None: now = tz_now() query_set = UserSessionMembership.objects\ .select_related('session')\ - .filter(user=user)\ + .filter(user_id=user_id)\ .order_by('-created') non_expire_memberships = [x for x in query_set if x.session.expire_date > now] return non_expire_memberships[settings.SESSIONS_PER_USER:] diff --git a/awx/main/signals.py b/awx/main/signals.py index ab62d27d98fc..da2898e43fe2 100644 --- a/awx/main/signals.py +++ b/awx/main/signals.py @@ -652,20 +652,19 @@ def save_user_session_membership(sender, **kwargs): return if not session: return - user = session.get_decoded().get(SESSION_KEY, None) - if not user: + user_id = session.get_decoded().get(SESSION_KEY, None) + if not user_id: return - user = User.objects.get(pk=user) - if UserSessionMembership.objects.filter(user=user, session=session).exists(): + if UserSessionMembership.objects.filter(user=user_id, session=session).exists(): return - UserSessionMembership(user=user, session=session, created=timezone.now()).save() - expired = UserSessionMembership.get_memberships_over_limit(user) + UserSessionMembership(user_id=user_id, session=session, created=timezone.now()).save() + expired = UserSessionMembership.get_memberships_over_limit(user_id) for membership in expired: Session.objects.filter(session_key__in=[membership.session_id]).delete() membership.delete() if len(expired): consumers.emit_channel_notification( - 'control-limit_reached_{}'.format(user.pk), + 'control-limit_reached_{}'.format(user_id), dict(group_name='control', reason='limit_reached') ) @@ -680,7 +679,7 @@ def create_access_token_user_if_missing(sender, **kwargs): post_save.connect(create_access_token_user_if_missing, sender=OAuth2AccessToken) -# Connect the Instance Group to Activity Stream receivers. +# Connect the Instance Group to Activity Stream receivers. post_save.connect(activity_stream_create, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_create") pre_save.connect(activity_stream_update, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_update") pre_delete.connect(activity_stream_delete, sender=InstanceGroup, dispatch_uid=str(InstanceGroup) + "_delete")