From 1172dd00e53514bf05f05e79bf8e6ba9a07b28fc Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Wed, 25 May 2022 21:41:40 +0530 Subject: [PATCH] fix: [BB-6261] warn and trim name for site configuration before saving --- .../commands/create_or_update_site_configuration.py | 12 +++++++++++- .../test_create_or_update_site_configuration.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/site_configuration/management/commands/create_or_update_site_configuration.py b/openedx/core/djangoapps/site_configuration/management/commands/create_or_update_site_configuration.py index fd3f97f86a..c97944b910 100644 --- a/openedx/core/djangoapps/site_configuration/management/commands/create_or_update_site_configuration.py +++ b/openedx/core/djangoapps/site_configuration/management/commands/create_or_update_site_configuration.py @@ -70,6 +70,7 @@ class Command(BaseCommand): def handle(self, *args, **options): site_id = options.get('site_id') domain = options.get('domain') + name = domain configuration = options.get('configuration') config_file_data = options.get('config_file_data') @@ -78,9 +79,18 @@ class Command(BaseCommand): if site_id is not None: site, created = Site.objects.get_or_create(id=site_id) else: + name_max_length = Site._meta.get_field("name").max_length + if name: + if len(str(name)) > name_max_length: + LOG.warning( + f"The name {name} is too long, truncating to {name_max_length}" + " characters. Please update site name in admin." + ) + # trim name as the column has a limit of 50 characters + name = name[:name_max_length] site, created = Site.objects.get_or_create( domain=domain, - name=domain, + name=name, ) if created: LOG.info(f"Site does not exist. Created new site '{site.domain}'") diff --git a/openedx/core/djangoapps/site_configuration/management/commands/tests/test_create_or_update_site_configuration.py b/openedx/core/djangoapps/site_configuration/management/commands/tests/test_create_or_update_site_configuration.py index 5b4d3f8ae1..e572c3631f 100644 --- a/openedx/core/djangoapps/site_configuration/management/commands/tests/test_create_or_update_site_configuration.py +++ b/openedx/core/djangoapps/site_configuration/management/commands/tests/test_create_or_update_site_configuration.py @@ -107,6 +107,19 @@ class CreateOrUpdateSiteConfigurationTest(TestCase): assert not site_configuration.site_values assert not site_configuration.enabled + def test_site_created_when_domain_longer_than_50_characters(self): + """ + Verify that a SiteConfiguration instance is created with name trimmed + to 50 characters when domain is longer than 50 characters + """ + self.assert_site_configuration_does_not_exist() + + domain = "studio.newtestserverwithlongname.development.opencraft.hosting" + call_command(self.command, f"{domain}") + site = Site.objects.filter(domain=domain) + assert site.exists() + assert site[0].name == domain[:50] + def test_both_enabled_disabled_flags(self): """ Verify the error on providing both the --enabled and --disabled flags.