From d7675e75a0eee6789d683bc7f2a1e76c43869db0 Mon Sep 17 00:00:00 2001 From: lorygold Date: Thu, 28 Sep 2023 15:50:40 +0200 Subject: [PATCH] Refactoring --- .../management/commands/setup_config.py | 59 +++++++++++++------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/buffalogs/impossible_travel/management/commands/setup_config.py b/buffalogs/impossible_travel/management/commands/setup_config.py index 62fe4a1..0a79230 100644 --- a/buffalogs/impossible_travel/management/commands/setup_config.py +++ b/buffalogs/impossible_travel/management/commands/setup_config.py @@ -21,29 +21,52 @@ def add_arguments(self, parser): def handle(self, *args, **options): """Setup the configurations into the Config model""" logger = logging.getLogger() + if Config.objects.all().exists(): config_obj = Config.objects.all()[0] - if not options["ignored_users"] and not options["ignored_ips"] and not options["allowed_countries"] and not options["vip_users"]: - # Set default values - config_obj.ignored_users = IGNORED_USERS - config_obj.ignored_ips = IGNORED_IPS - else: - config_obj.ignored_users = options["ignored_users"].split(",") - # config_obj.ignored_ips = options["ignored_ips"].split(",") - # config_obj.allowed_countries = options["allowed_countries"].split(",") - # config_obj.vip_users = options["vip_users"].split(",") else: - if not options["ignored_users"] and not options["ignored_ips"] and not options["allowed_countries"] and not options["vip_users"]: - config_obj = Config.objects.create(ignored_users=IGNORED_USERS, ignored_ips=IGNORED_IPS) - else: - config_obj = Config.objects.create( - ignored_users=options["ignored_users"].split(","), - # ignored_ips=options["ignored_ips"].split(","), - # allowed_countries=options["allowed_countries"].split(","), - # vip_users=options["vip_users"].split(","), - ) + config_obj = Config.objects.create() + if not options["ignored_users"] and not options["ignored_ips"] and not options["allowed_countries"] and not options["vip_users"]: + # Set default values + config_obj.ignored_users = IGNORED_USERS + config_obj.ignored_ips = IGNORED_IPS + else: + for opt in options: + if opt in ["ignored_users", "ignored_ips", "allowed_countries", "vip_users"]: + if not options[opt]: + setattr(config_obj, opt, []) + else: + setattr(config_obj, opt, options[opt].split(",")) + config_obj.save() + # if Config.objects.all().exists(): + # config_obj = Config.objects.all()[0] + # if not options["ignored_users"] and not options["ignored_ips"] and not options["allowed_countries"] and not options["vip_users"]: + # # Set default values + # config_obj.ignored_users = IGNORED_USERS + # config_obj.ignored_ips = IGNORED_IPS + # else: + # for opt in options: + # if options[opt] and type(options[opt]) is str: + # try: + # setattr(config_obj, opt, options[opt].split(',')) + # except AttributeError: + # pass + + # else: + # if not options["ignored_users"] and not options["ignored_ips"] and not options["allowed_countries"] and not options["vip_users"]: + # config_obj = Config.objects.create(ignored_users=IGNORED_USERS, ignored_ips=IGNORED_IPS) + # else: + # config_obj = Config.objects.create() + # for opt in options: + # if options[opt] and type(options[opt]) is str: + # try: + # setattr(config_obj, opt, options[opt].split(',')) + # except AttributeError: + # pass + # config_obj.save() + logger.info( f"Updated Config values - Ignored users: {config_obj.ignored_users}, Ignored IPs: {config_obj.ignored_ips}, Allowed countries: {config_obj.allowed_countries}, Vip users: {config_obj.vip_users}" )