dashboard/accounts/management/commands/get_user_id.py

44 lines
1.7 KiB
Python
Raw Normal View History

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, timedelta
from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.auth import get_user_model
class Command(BaseCommand):
help = "Get user ID from username"
username_key = "username"
def add_arguments(self, parser):
parser.add_argument(
self.username_key,
type=str,
help="The username for which database assigned ID needs to be queried",
)
def handle(self, *args, **options):
if self.username_key not in options:
self.stdout.write(self.style.ERROR("Please provide username"))
return
username = options[self.username_key]
User = get_user_model()
if User.objects.filter(username=username).exists():
user = get_user_model().objects.get(username=username)
self.stdout.write(self.style.SUCCESS(user.id))
return
else:
self.stderr.write(self.style.ERROR(f"user {username} not found"))