Skip to content
Snippets Groups Projects
Commit d1c87711 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

Merge branch 'get_active_users_command' into '1.4.x'

Add django command to get users active on the site

See merge request !247
parents 2fb3c397 c4cd85bb
No related branches found
No related tags found
2 merge requests!2551.4.x,!247Add django command to get users active on the site
Pipeline #
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# encoding: utf-8
###############################################################################
# #
# Copyright (c) 2018 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from django.utils import timezone
def get_current_users():
"""
Get the list of users active on the site.
Works better if the configuration of the site has:
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
Inspired from:
https://www.codingforentrepreneurs.com/blog/django-tutorial-get-list-of-current-users/
"""
active_sessions = Session.objects.filter(expire_date__gte=timezone.now())
user_id_list = []
for session in active_sessions:
data = session.get_decoded()
user_id_list.append(data.get('_auth_user_id', None))
# Query all logged in users based on id list
return User.objects.filter(id__in=user_id_list)
class Command(BaseCommand):
help = 'Get active users'
def add_arguments(self, parser):
parser.add_argument('--email', '-e', action='store_true',
dest='show_email', default=False, help='Set this flag'\
'to also print email addresse')
parser.add_argument('--only-email', '-o', action='store_true',
dest='show_only_email', default=False, help='Set this'\
'flag to only print email addresse')
def handle(self, *args, **options):
current_users = get_current_users()
for user in current_users:
if options['show_only_email']:
print(user.email)
else:
user_data = "{} {}".format(user.first_name, user.last_name)
if options['show_email']:
user_data += " {}".format(user.email)
print(user_data)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment