Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorygold committed Sep 28, 2023
1 parent 60fe56e commit d7675e7
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions buffalogs/impossible_travel/management/commands/setup_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)

0 comments on commit d7675e7

Please sign in to comment.