mirror of
https://github.com/etesync/server
synced 2025-02-17 19:01:59 +00:00
Change admin-cli as discussed in #99
This commit is contained in:
parent
b72d2121bd
commit
a9c0807a69
@ -9,10 +9,6 @@ class Command(BaseCommand):
|
||||
parser.add_argument( 'username'
|
||||
, type=str
|
||||
, help="New user's login username." )
|
||||
parser.add_argument( '-p'
|
||||
, '--password'
|
||||
, type=str
|
||||
, help="New user's plain text login password." )
|
||||
parser.add_argument( '-f'
|
||||
, '--first_name'
|
||||
, '--first'
|
||||
@ -57,16 +53,14 @@ class Command(BaseCommand):
|
||||
|
||||
def handle(self, *args, **options):
|
||||
try:
|
||||
user = User.objects.create_user( username = options["username" ]
|
||||
, password = options["password" ]
|
||||
, email = options["email" ]
|
||||
, first_name = options["first_name" ]
|
||||
, last_name = options["last_name" ]
|
||||
, is_superuser = options["is_superuser" ]
|
||||
, is_staff = options["is_staff" ]
|
||||
, is_active = options["is_active" ] )
|
||||
user.save()
|
||||
except (IntegrityError,Group.DoesNotExist,Permission.DoesNotExist) as exception:
|
||||
User.objects.create_user( username = options["username" ]
|
||||
, email = options["email" ]
|
||||
, first_name = options["first_name" ]
|
||||
, last_name = options["last_name" ]
|
||||
, is_active = options["is_active" ]
|
||||
, is_staff = options["is_staff" ]
|
||||
, is_superuser = options["is_superuser"] )
|
||||
except IntegrityError as exception:
|
||||
self.stdout.write(self.style.ERROR(f'Unable to create user "{options["username"]}": ' + str(exception)))
|
||||
exit(1)
|
||||
|
33
django_etebase/admin-cli/management/commands/users-delete.py
Executable file
33
django_etebase/admin-cli/management/commands/users-delete.py
Executable file
@ -0,0 +1,33 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from myauth.models import User
|
||||
from django.db.models.deletion import ProtectedError
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument( 'username'
|
||||
, type=str
|
||||
, help="Login username of the user to be deleted." )
|
||||
parser.add_argument( '--delete-user-data'
|
||||
, action='store_true'
|
||||
, default=False
|
||||
, help="Delete all user's collections!" )
|
||||
|
||||
def handle(self, *args, **options):
|
||||
try:
|
||||
user = User.objects.get(username = options["username"])
|
||||
if options["delete_user_data"]:
|
||||
collections = user.collection_set.all()
|
||||
for collection in collections:
|
||||
collection.delete()
|
||||
user.delete()
|
||||
except User.DoesNotExist as exception:
|
||||
self.stdout.write(self.style.ERROR(f'Unable to delete user "{options["username"]}": ' + str(exception)))
|
||||
exit(1)
|
||||
except ProtectedError as exception:
|
||||
self.stdout.write(self.style.ERROR(f'Unable to delete user "{options["username"]}": ' + str(exception)))
|
||||
self.stdout.write(self.style.NOTICE('Use --delete-user-data to overcome this protection.'))
|
||||
exit(2)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f'User "{options["username"]}" has been deleted.'))
|
||||
exit(0)
|
@ -6,3 +6,4 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
for user in User.objects.all():
|
||||
print(user.username)
|
||||
exit(0)
|
||||
|
Loading…
Reference in New Issue
Block a user