Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring for constance cli command #561

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions constance/management/commands/constance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django import VERSION
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.management import BaseCommand
Expand Down Expand Up @@ -30,67 +29,56 @@
class Command(BaseCommand):
help = _('Get/Set In-database config settings handled by Constance')

GET = 'get'
SET = 'set'
LIST = 'list'
REMOVE_STALE_KEYS = 'remove_stale_keys'

def add_arguments(self, parser):
subparsers = parser.add_subparsers(dest='command')
# API changed in Django>=2.1. cmd argument was removed.
parser_list = self._subparsers_add_parser(
subparsers, 'list', cmd=self, help='list all Constance keys and their values'
)
subparsers.add_parser(self.LIST, help='list all Constance keys and their values')

parser_get = self._subparsers_add_parser(subparsers, 'get', cmd=self, help='get the value of a Constance key')
parser_get = subparsers.add_parser(self.GET, help='get the value of a Constance key')
parser_get.add_argument('key', help='name of the key to get', metavar='KEY')

parser_set = self._subparsers_add_parser(subparsers, 'set', cmd=self, help='set the value of a Constance key')
parser_set.add_argument('key', help='name of the key to get', metavar='KEY')
parser_set = subparsers.add_parser(self.SET, help='set the value of a Constance key')
parser_set.add_argument('key', help='name of the key to set', metavar='KEY')
# use nargs='+' so that we pass a list to MultiValueField (eg SplitDateTimeField)
parser_set.add_argument('value', help='value to set', metavar='VALUE', nargs='+')

self._subparsers_add_parser(
subparsers,
'remove_stale_keys',
cmd=self,
subparsers.add_parser(
self.REMOVE_STALE_KEYS,
help='delete all Constance keys and their values if they are not in settings.CONSTANCE_CONFIG (stale keys)',
)

def _subparsers_add_parser(self, subparsers, name, **kwargs):
# API in Django >= 2.1 changed and removed cmd parameter from add_parser
if VERSION >= (2, 1) and 'cmd' in kwargs:
kwargs.pop('cmd')
return subparsers.add_parser(name, **kwargs)

def handle(self, command, key=None, value=None, *args, **options):
if command == 'get':
if command == self.GET:
try:
self.stdout.write(str(getattr(config, key)), ending='\n')
except AttributeError as e:
raise CommandError(key + ' is not defined in settings.CONSTANCE_CONFIG')

elif command == 'set':
except AttributeError:
raise CommandError(f'{key} is not defined in settings.CONSTANCE_CONFIG')
elif command == self.SET:
try:
if len(value) == 1:
# assume that if a single argument was passed, the field doesn't expect a list
value = value[0]

_set_constance_value(key, value)
except KeyError as e:
raise CommandError(key + ' is not defined in settings.CONSTANCE_CONFIG')
except KeyError:
raise CommandError(f'{key} is not defined in settings.CONSTANCE_CONFIG')
except ValidationError as e:
raise CommandError(', '.join(e))

elif command == 'list':
elif command == self.LIST:
for k, v in get_values().items():
self.stdout.write(f'{k}\t{v}', ending='\n')

elif command == 'remove_stale_keys':
elif command == self.REMOVE_STALE_KEYS:
actual_keys = settings.CONSTANCE_CONFIG.keys()

stale_records = Constance.objects.exclude(key__in=actual_keys)
if stale_records:
self.stdout.write('The following record will be deleted:', ending='\n')
else:
self.stdout.write('There are no stale records in database.', ending='\n')

self.stdout.write('There are no stale records in the database.', ending='\n')

Check warning on line 79 in constance/management/commands/constance.py

View check run for this annotation

Codecov / codecov/patch

constance/management/commands/constance.py#L79

Added line #L79 was not covered by tests
for stale_record in stale_records:
self.stdout.write(f'{stale_record.key}\t{stale_record.value}', ending='\n')

stale_records.delete()
else:
raise CommandError('Invalid command')

Check warning on line 84 in constance/management/commands/constance.py

View check run for this annotation

Codecov / codecov/patch

constance/management/commands/constance.py#L84

Added line #L84 was not covered by tests