refactor: Ran pyupgrade on openedx/core/djangoapps/site_configuration

This commit is contained in:
Usama Sadiq
2021-03-11 16:58:25 +05:00
parent ebcf204d60
commit c14eab1332
19 changed files with 33 additions and 58 deletions

View File

@@ -15,7 +15,7 @@ class SiteConfigurationAdmin(admin.ModelAdmin):
list_display = ('site', 'enabled', 'site_values')
search_fields = ('site__domain', 'site_values')
class Meta(object):
class Meta:
"""
Meta class for SiteConfiguration admin model
"""
@@ -33,7 +33,7 @@ class SiteConfigurationHistoryAdmin(admin.ModelAdmin):
ordering = ['-created']
class Meta(object):
class Meta:
"""
Meta class for SiteConfigurationHistory admin model
"""

View File

@@ -249,6 +249,6 @@ def page_title_breadcrumbs(*crumbs, **kwargs):
separator = kwargs.get("separator", " | ")
crumbs = [c for c in crumbs if c is not None]
if crumbs:
return u'{}{}{}'.format(separator.join(crumbs), separator, platform_name)
return '{}{}{}'.format(separator.join(crumbs), separator, platform_name)
else:
return platform_name

View File

@@ -83,9 +83,9 @@ class Command(BaseCommand):
name=domain,
)
if created:
LOG.info("Site does not exist. Created new site '{site_name}'".format(site_name=site.domain))
LOG.info(f"Site does not exist. Created new site '{site.domain}'")
else:
LOG.info("Found existing site for '{site_name}'".format(site_name=site.domain))
LOG.info(f"Found existing site for '{site.domain}'")
site_configuration, created = SiteConfiguration.objects.get_or_create(site=site)
if created:
@@ -96,7 +96,7 @@ class Command(BaseCommand):
)
else:
LOG.info(
"Found existing site configuration for '{site_name}'. Updating it.".format(site_name=site.domain)
f"Found existing site configuration for '{site.domain}'. Updating it."
)
site_configuration_values = configuration or config_file_data

View File

@@ -22,7 +22,7 @@ class CreateOrUpdateSiteConfigurationTest(TestCase):
command = 'create_or_update_site_configuration'
def setUp(self):
super(CreateOrUpdateSiteConfigurationTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments
super().setUp()
self.site_id = 1
self.site_id_arg = ['--site-id', str(self.site_id)]
self.json_file_path = Path(__file__).parent / "fixtures/config1.json"
@@ -123,7 +123,7 @@ class CreateOrUpdateSiteConfigurationTest(TestCase):
Verify that the SiteConfiguration instance is enabled/disabled as per the flag used.
"""
self.assert_site_configuration_does_not_exist()
call_command(self.command, '--{}'.format(flag), *self.site_id_arg)
call_command(self.command, f'--{flag}', *self.site_id_arg)
site_configuration = SiteConfiguration.objects.get(site=self.site)
assert not site_configuration.site_values
assert enabled == site_configuration.enabled

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations, models
import django.utils.timezone
import jsonfield.fields

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations, models
@@ -14,11 +11,11 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='siteconfiguration',
name='enabled',
field=models.BooleanField(default=False, verbose_name=u'Enabled'),
field=models.BooleanField(default=False, verbose_name='Enabled'),
),
migrations.AddField(
model_name='siteconfigurationhistory',
name='enabled',
field=models.BooleanField(default=False, verbose_name=u'Enabled'),
field=models.BooleanField(default=False, verbose_name='Enabled'),
),
]

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.28 on 2020-02-17 10:58

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.28 on 2020-02-19 16:50

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations
forward_sql = """

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations
from ..models import save_siteconfig_without_historical_record

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations

View File

@@ -30,7 +30,7 @@ class SiteConfiguration(models.Model):
.. no_pii:
"""
site = models.OneToOneField(Site, related_name='configuration', on_delete=models.CASCADE)
enabled = models.BooleanField(default=False, verbose_name=u"Enabled")
enabled = models.BooleanField(default=False, verbose_name="Enabled")
site_values = JSONField(
null=False,
blank=True,
@@ -42,7 +42,7 @@ class SiteConfiguration(models.Model):
)
def __str__(self):
return u"<SiteConfiguration: {site} >".format(site=self.site) # xss-lint: disable=python-wrap-html
return f"<SiteConfiguration: {self.site} >" # xss-lint: disable=python-wrap-html
def __repr__(self):
return self.__str__()
@@ -64,9 +64,9 @@ class SiteConfiguration(models.Model):
try:
return self.site_values.get(name, default)
except AttributeError as error:
logger.exception(u'Invalid JSON data. \n [%s]', error)
logger.exception('Invalid JSON data. \n [%s]', error)
else:
logger.info(u"Site Configuration is not enabled for site (%s).", self.site)
logger.info("Site Configuration is not enabled for site (%s).", self.site)
return default
@@ -171,7 +171,7 @@ class SiteConfigurationHistory(TimeStampedModel):
.. no_pii:
"""
site = models.ForeignKey(Site, related_name='configuration_histories', on_delete=models.CASCADE)
enabled = models.BooleanField(default=False, verbose_name=u"Enabled")
enabled = models.BooleanField(default=False, verbose_name="Enabled")
site_values = JSONField(
null=False,
blank=True,
@@ -184,7 +184,7 @@ class SiteConfigurationHistory(TimeStampedModel):
def __str__(self):
# pylint: disable=line-too-long
return u"<SiteConfigurationHistory: {site}, Last Modified: {modified} >".format( # xss-lint: disable=python-wrap-html
return "<SiteConfigurationHistory: {site}, Last Modified: {modified} >".format( # xss-lint: disable=python-wrap-html
modified=self.modified,
site=self.site,
)

View File

@@ -51,7 +51,7 @@ def microsite_css_overrides_file():
"""
file_path = configuration_helpers.get_value('css_overrides_file', None)
if file_path is not None:
return HTML(u"<link href='{}' rel='stylesheet' type='text/css'>").format(static(file_path))
return HTML("<link href='{}' rel='stylesheet' type='text/css'>").format(static(file_path))
else:
return ""

View File

@@ -14,7 +14,7 @@ class SiteFactory(DjangoModelFactory):
"""
Factory class for Site model
"""
class Meta(object):
class Meta:
model = Site
django_get_or_create = ('domain',)
@@ -26,7 +26,7 @@ class SiteConfigurationFactory(DjangoModelFactory):
"""
Factory class for SiteConfiguration model
"""
class Meta(object):
class Meta:
model = SiteConfiguration
enabled = True

View File

@@ -6,12 +6,12 @@ Mixins for TestCase classes that need to account for multiple sites
from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory, SiteFactory
class SiteMixin(object):
class SiteMixin:
"""
Mixin for setting up Site framework models
"""
def setUp(self):
super(SiteMixin, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments
super().setUp()
self.site = SiteFactory.create()
site_config = {

View File

@@ -3,7 +3,6 @@ Tests for helper function provided by site_configuration app.
"""
import six
from django.test import TestCase
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
@@ -74,8 +73,7 @@ class TestHelpers(TestCase):
Test that get_dict returns correct value for any given key.
"""
# Make sure entry is saved and retrieved correctly
six.assertCountEqual(
self,
self.assertCountEqual(
configuration_helpers.get_dict("REGISTRATION_EXTRA_FIELDS"),
test_config['REGISTRATION_EXTRA_FIELDS'],
)
@@ -85,8 +83,7 @@ class TestHelpers(TestCase):
expected.update(test_config['REGISTRATION_EXTRA_FIELDS'])
# Test that the default value is returned if the value for the given key is not found in the configuration
six.assertCountEqual(
self,
self.assertCountEqual(
configuration_helpers.get_dict("REGISTRATION_EXTRA_FIELDS", default),
expected,
)
@@ -124,8 +121,7 @@ class TestHelpers(TestCase):
assert configuration_helpers.get_value_for_org(test_org, 'css_overrides_file') ==\
test_config['css_overrides_file']
six.assertCountEqual(
self,
self.assertCountEqual(
configuration_helpers.get_value_for_org(test_org, "REGISTRATION_EXTRA_FIELDS"),
test_config['REGISTRATION_EXTRA_FIELDS']
)
@@ -155,8 +151,7 @@ class TestHelpers(TestCase):
"""
test_orgs = [test_config['course_org_filter']]
with with_site_configuration_context(configuration=test_config):
six.assertCountEqual(
self,
self.assertCountEqual(
list(configuration_helpers.get_all_orgs()),
test_orgs,
)
@@ -164,8 +159,7 @@ class TestHelpers(TestCase):
@with_site_configuration(configuration=test_config_multi_org)
def test_get_current_site_orgs(self):
test_orgs = test_config_multi_org['course_org_filter']
six.assertCountEqual(
self,
self.assertCountEqual(
list(configuration_helpers.get_current_site_orgs()),
test_orgs
)

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Test site_configuration middleware.
"""
@@ -23,7 +22,7 @@ class SessionCookieDomainTests(TestCase):
"""
def setUp(self):
super(SessionCookieDomainTests, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments
super().setUp()
# Create a test client, and log it in so that it will save some session
# data.
self.user = UserFactory.create()
@@ -62,7 +61,7 @@ class SessionCookieDomainSiteConfigurationOverrideTests(TestCase):
"""
def setUp(self):
super(SessionCookieDomainSiteConfigurationOverrideTests, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments
super().setUp()
# Create a test client, and log it in so that it will save some session data.
self.user = UserFactory.create()
self.user.set_password('password')

View File

@@ -1,12 +1,11 @@
"""
Tests for site configuration's django models.
"""
import six
from unittest.mock import patch
import pytest
from django.contrib.sites.models import Site
from django.db import IntegrityError, transaction
from django.test import TestCase
from mock import patch
from openedx.core.djangoapps.site_configuration.models import (
SiteConfiguration,
SiteConfigurationHistory,
@@ -48,7 +47,7 @@ class SiteConfigurationTests(TestCase):
@classmethod
def setUpClass(cls):
super(SiteConfigurationTests, cls).setUpClass()
super().setUpClass()
cls.site, _ = Site.objects.get_or_create(domain=cls.domain, name=cls.domain)
cls.site2, _ = Site.objects.get_or_create(
domain=cls.test_config2['SITE_NAME'],
@@ -300,7 +299,7 @@ class SiteConfigurationTests(TestCase):
)
# Test that the default value is returned if the value for the given key is not found in the configuration
six.assertCountEqual(self, SiteConfiguration.get_all_orgs(), expected_orgs)
self.assertCountEqual(SiteConfiguration.get_all_orgs(), expected_orgs)
def test_get_all_orgs_returns_only_enabled(self):
"""
@@ -319,4 +318,4 @@ class SiteConfigurationTests(TestCase):
)
# Test that the default value is returned if the value for the given key is not found in the configuration
six.assertCountEqual(self, SiteConfiguration.get_all_orgs(), expected_orgs)
self.assertCountEqual(SiteConfiguration.get_all_orgs(), expected_orgs)

View File

@@ -6,7 +6,7 @@ Test helpers for Site Configuration.
from functools import wraps
import contextlib
from mock import patch
from unittest.mock import patch
from django.contrib.sites.models import Site