2013-11-13 03:22:54 +01:00
|
|
|
"""
|
|
|
|
reinitialize the test user account (and clean up)
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.conf import settings
|
2013-11-24 09:37:47 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-12-28 17:46:46 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2013-11-13 03:22:54 +01:00
|
|
|
|
|
|
|
|
2017-12-28 17:46:46 +01:00
|
|
|
class Command(BaseCommand):
|
2013-11-13 03:22:54 +01:00
|
|
|
help = 'reinitialize the test user'
|
|
|
|
|
2017-12-28 17:46:46 +01:00
|
|
|
def handle(self, *args, **options):
|
2013-11-24 09:37:47 +01:00
|
|
|
user_model = get_user_model()
|
2013-11-13 03:22:54 +01:00
|
|
|
try:
|
2013-11-24 09:37:47 +01:00
|
|
|
u = user_model.objects.get(username='test')
|
2013-11-13 03:22:54 +01:00
|
|
|
# delete test user and (via CASCADE behaviour) everything that
|
|
|
|
# points to it (has user as ForeignKey), e.g. via created_by.
|
|
|
|
u.delete()
|
2013-11-24 09:37:47 +01:00
|
|
|
except user_model.DoesNotExist:
|
2013-11-13 03:22:54 +01:00
|
|
|
pass
|
|
|
|
# create a fresh test user
|
2013-11-24 09:37:47 +01:00
|
|
|
u = user_model.objects.create_user('test', settings.DEFAULT_FROM_EMAIL, 'test')
|
2013-11-13 03:22:54 +01:00
|
|
|
u.save()
|
|
|
|
self.stdout.write('Successfully reinitialized the test user')
|