diff --git a/accounts/management/commands/get_user_id.py b/accounts/management/commands/get_user_id.py new file mode 100644 index 0000000..cae97a5 --- /dev/null +++ b/accounts/management/commands/get_user_id.py @@ -0,0 +1,43 @@ +# Copyright © 2022 Aravinth Manivannan +# +# 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 . +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")) diff --git a/accounts/tests.py b/accounts/tests.py index 06f44c7..ec1c0d6 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -15,8 +15,10 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import time +from io import StringIO from django.contrib.auth import get_user_model +from django.core.management import call_command from django.urls import reverse from django.test import TestCase, Client, override_settings from django.utils.http import urlencode @@ -376,3 +378,21 @@ class ConfirmAccessDecorator(TestCase): ConfirmAccess.set(req) # request has sudo authorization data and is valid for this time duration self.assertEqual(fn(req), True) + + +class GetUserIDTest(TestCase): + def setUp(self): + self.username = "getuserid" + register_util(t=self, username=self.username) + + def test_command_output(self): + stdout = StringIO() + stderr = StringIO() + # username exists + call_command(f"get_user_id {self.username}", stdout=stdout, stderr=stderr) + self.assertIn(self.user.id, out.getvalue()) + + # username doesn't exist + nouser = "fooabr" + call_command(f"get_user_id {nouser}", stdout=out, stderr=stderr) + self.assertIn(f"username {nouser} doesn't exist", stderr.getvalue()) diff --git a/dash/migrations/0006_auto_20220619_0800.py b/dash/migrations/0006_auto_20220619_0800.py index 5e4b6c3..c9a4e54 100644 --- a/dash/migrations/0006_auto_20220619_0800.py +++ b/dash/migrations/0006_auto_20220619_0800.py @@ -3,8 +3,9 @@ from django.db import migrations from django.db.utils import IntegrityError + def create_default_configs(apps, schema_editor): - InstanceConfiguration = apps.get_model('dash', 'InstanceConfiguration') + InstanceConfiguration = apps.get_model("dash", "InstanceConfiguration") configs = [ InstanceConfiguration(name="s1-2", rent=10, ram=2, cpu=1, storage=10), @@ -23,13 +24,10 @@ def create_default_configs(apps, schema_editor): raise e - class Migration(migrations.Migration): dependencies = [ - ('dash', '0005_alter_instance_owned_by'), + ("dash", "0005_alter_instance_owned_by"), ] - operations = [ - migrations.RunPython(create_default_configs) - ] + operations = [migrations.RunPython(create_default_configs)] diff --git a/dash/models.py b/dash/models.py index 27b9782..a747f28 100644 --- a/dash/models.py +++ b/dash/models.py @@ -18,6 +18,7 @@ from django.contrib.auth.models import User from django.utils.http import urlencode from django.urls import reverse + class InstanceConfiguration(models.Model): ID = models.AutoField(primary_key=True) name = models.CharField( @@ -41,7 +42,6 @@ class InstanceConfiguration(models.Model): created_at = models.DateTimeField(auto_now_add=True, blank=True) - def __str__(self): return f"{self.name}" diff --git a/dash/tests.py b/dash/tests.py index 895bded..fbaacea 100644 --- a/dash/tests.py +++ b/dash/tests.py @@ -21,6 +21,7 @@ from django.db.utils import IntegrityError from .models import InstanceConfiguration, Instance + def register_util(t: TestCase, username: str): t.password = "password121231" t.username = username