feat: get_user_id management command
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
DESCRIPTION Gets the DB assigned ID associated with a usernamewip-payments
parent
38619babc7
commit
faa7e924cc
|
@ -0,0 +1,43 @@
|
|||
# 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"))
|
|
@ -15,8 +15,10 @@
|
|||
# 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/>.
|
||||
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())
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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}"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue