Skip to content
Snippets Groups Projects

Add django command to get users active on the site

Merged Samuel GAIST requested to merge get_active_users_command into 1.4.x
1 file
+ 78
0
Compare changes
  • Side-by-side
  • Inline
 
#!/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
Loading