Skip to content

Commit

Permalink
fixup! feat: add create_or_update_tenant_config management command
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed May 18, 2023
1 parent 68e609d commit 07b5029
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
24 changes: 11 additions & 13 deletions eox_tenant/management/commands/create_or_update_tenant_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
import logging

from django.core.management import BaseCommand, CommandError
from django.core.management import BaseCommand
from jsonfield.fields import JSONField

from eox_tenant.models import Route, TenantConfig
Expand All @@ -24,7 +24,7 @@ def load_json_from_file(filename):

class Command(BaseCommand):
"""
Management command to create or update SiteConfiguration.
Management command to create or update TenantConfig.
"""
help = 'Create or update TenantConfig'

Expand Down Expand Up @@ -60,9 +60,6 @@ def handle(self, *args, **options):
configuration = options.get('config')
config_file_data = options.get('config_file_data')
tenant_configuration_values = configuration or config_file_data
if tenant_configuration_values is None:
raise CommandError("Either --config or --config_file_data needs to be passed in.")

# pylint: disable=no-member,protected-access
external_key_length = TenantConfig._meta.get_field("external_key").max_length
if external_key:
Expand All @@ -84,15 +81,16 @@ def handle(self, *args, **options):
LOG.info("Found existing tenant for: '%s'", tenant.external_key)

# split out lms, studio, theme, meta from configuration json
for field in TenantConfig._meta.get_fields():
if isinstance(field, JSONField):
name = field.name
value = tenant_configuration_values.get(name)
if value is not None:
setattr(tenant, name, value)
if tenant_configuration_values:
for field in TenantConfig._meta.get_fields():
if isinstance(field, JSONField):
name = field.name
value = tenant_configuration_values.get(name)
if value is not None:
setattr(tenant, name, value)

tenant.save()
# next add create routes and link them
tenant.save()
# next add routes and link them
for route in routes:
route, created = Route.objects.update_or_create(
domain=route,
Expand Down
24 changes: 13 additions & 11 deletions eox_tenant/test/test_create_or_udpate_tenant_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,21 @@ def test_command_invalid_config(self):
self.assertFalse(Route.objects.filter(domain__contains="test.domain").exists())

def test_command_with_no_config(self):
"""Tests that command raises if config is not passed"""
self.assertFalse(TenantConfig.objects.filter(external_key="new.key").exists())
self.assertFalse(Route.objects.filter(domain__contains="test.domain").exists())
with self.assertRaises(CommandError):
call_command(
"create_or_update_tenant_config",
"--external-key",
"new.key",
"test.domain",
"studio.test.domain"
)
"""
Tests that command works even if config is not passed,
i.e. it adds/updates an entry with external_key and links given routes.
"""
self.assertFalse(TenantConfig.objects.filter(external_key="new.key").exists())
self.assertFalse(Route.objects.filter(domain__contains="test.domain").exists())
call_command(
"create_or_update_tenant_config",
"--external-key",
"new.key",
"test.domain",
"studio.test.domain"
)
self.assertTrue(TenantConfig.objects.filter(external_key="new.key").exists())
self.assertTrue(Route.objects.filter(domain__contains="test.domain").exists())

def test_command_with_long_external_key(self):
"""Tests that command runs successfully even if external key is longer than limit."""
Expand Down

0 comments on commit 07b5029

Please sign in to comment.