Skip to content

Commit

Permalink
[AAP-11699] Add update_password command
Browse files Browse the repository at this point in the history
  • Loading branch information
Dostonbek1 committed Nov 16, 2023
1 parent 70529ad commit 5b8442e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/aap_eda/core/management/commands/update_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.core.management import BaseCommand, CommandError
from django.db import transaction

from aap_eda.core import models


class Command(BaseCommand):
help = "Update user password."

def add_arguments(self, parser):
parser.add_argument("-u", "--username", required=True)
parser.add_argument("-p", "--password", required=True)

@transaction.atomic
def handle(self, *args, **options):
username = options["username"]
password = options["password"]
try:
user = models.User.objects.get(username=username)
user.set_password(password)
user.save()
except models.User.DoesNotExist:
raise CommandError(f"User '{username}' does not exist. ")

self.stdout.write(
f"Successfully updated the password of user '{username}'"
)

0 comments on commit 5b8442e

Please sign in to comment.