Files
kasu/membership/management/commands/get-user.py
2014-12-10 00:23:36 +01:00

61 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
When you get an error email from your Django app telling you someone got a
server error,
its not always easy to tell which user had a problem.
It might help your debugging to know or you might want to contact the user to
tell them you have fixed the problem.
"""
from optparse import make_option
from django.conf import settings
from django.contrib import auth
from django.contrib.sessions.models import Session
from django.core.management.base import BaseCommand
class Command(BaseCommand):
args = '<session_key session_key ...>'
help = 'If the session still exists find it and show the related User'
option_list = BaseCommand.option_list + (
make_option(
'--delete',
action='store_true',
dest='delete',
default=False,
help='Delete the Useraccount'
),
make_option(
'--ban',
action='store_true',
dest='ban',
default=False,
help='Ban the Useraccount'
),
)
def handle(self, *args, **options):
for session_key in args:
try:
session = Session.objects.get(session_key=session_key)
uid = session.get_decoded().get('_auth_user_id')
user = auth.get_user_model().objects.get(pk=uid)
except Session.DoesNotExist:
self.stderr.write('Session "%s" does not exist' % session_key)
continue
except settings.AUTH_USER_MODEL.DoesNotExist:
self.stderr.write(
'Session "%s" has no registed User' % session_key)
continue
if options['delete']:
self.stdout.write('deleting %s' % user.username)
user.delete()
elif options['ban']:
self.stdout.write('banning %s' % user.username)
user.is_active = False
user.save()
else:
self.stdout.write("Username: %s" % user.username)