refactored the code to pass both enable and disable flags

This commit is contained in:
Gabe Muniz
2023-01-30 21:07:17 -05:00
parent d64b6d4dfe
commit 2d9da11443

View File

@@ -1,6 +1,5 @@
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
import argparse
class Command(BaseCommand):
@@ -8,25 +7,29 @@ class Command(BaseCommand):
def add_arguments(self, parser):
"""
This adds the --enable functionality to the command using argparse to allow either enable or no-enable
This adds the --enable --disable functionalities to the command using mutally_exclusive to avoid situations in which users pass both flags
"""
parser.add_argument('--enable', action=argparse.BooleanOptionalAction, help='to disable local auth --no-enable to enable --enable')
group = parser.add_mutually_exclusive_group()
group.add_argument('--enable', dest='enable', action='store_true', help='Pass --enable to enable local authentication')
group.add_argument('--disable', dest='disable', action='store_true', help='Pass --disable to disable local authentication')
def _enable_disable_auth(self, enable):
def _enable_disable_auth(self, enable, disable):
"""
this method allows the disabling or enabling of local authenication based on the argument passed into the parser
if no arguments throw a command error, if --enable set the DISABLE_LOCAL_AUTH to False
if --no-enable set to True. Realizing that the flag is counterintuitive to what is expected.
"""
if enable is None:
raise CommandError('Please pass --enable flag to allow local auth or --no-enable flag to disable local auth')
if enable:
settings.DISABLE_LOCAL_AUTH = False
print("Setting has changed to {} allowing local authentication".format(settings.DISABLE_LOCAL_AUTH))
return
settings.DISABLE_LOCAL_AUTH = True
print("Setting has changed to {} disallowing local authentication".format(settings.DISABLE_LOCAL_AUTH))
elif disable:
settings.DISABLE_LOCAL_AUTH = True
print("Setting has changed to {} disallowing local authentication".format(settings.DISABLE_LOCAL_AUTH))
else:
raise CommandError('Please pass --enable flag to allow local auth or --disable flag to disable local auth')
def handle(self, **options):
self._enable_disable_auth(options.get('enable'))
self._enable_disable_auth(options.get('enable'), options.get('disable'))