misc: fix warning about deprecated connection.get_tenant() (#73294)
gitea-wip/hobo/pipeline/pr-main This commit looks good Details

This commit is contained in:
Benjamin Dauvergne 2023-01-11 21:09:32 +01:00
parent 760a8b1a7c
commit 157a69fab6
12 changed files with 20 additions and 22 deletions

View File

@ -6,7 +6,7 @@ from kombu.common import Broadcast
def notify_agents(data):
'''Send notifications to all other tenants'''
tenant = connection.get_tenant()
tenant = connection.tenant
if not hasattr(tenant, 'domain_url'):
# Fake tenant, certainly during migration, do nothing
return

View File

@ -122,7 +122,7 @@ class Command(BaseCommand):
wait_operationals(services, timeout, self.verbosity, self.terminal_width, notify_agents)
def create_hobo(self, url, primary=False, title=None, slug=None, **kwargs):
if connection.get_tenant().schema_name == 'public':
if connection.tenant.schema_name == 'public':
# if we're not currently in a tenant then we force the creation of
# a primary hobo
primary = True
@ -245,7 +245,7 @@ class Command(BaseCommand):
def set_theme(self, theme):
set_theme(theme)
HoboDeployCommand().configure_theme({'variables': {'theme': theme}}, connection.get_tenant())
HoboDeployCommand().configure_theme({'variables': {'theme': theme}}, connection.tenant)
def set_variable(self, name, value, label=None, auto=None):
if auto is None:
@ -299,7 +299,7 @@ class Command(BaseCommand):
attribute.save()
def cook(self, filename):
current_tenant = connection.get_tenant()
current_tenant = connection.tenant
self.run_cook(filename)
connection.set_tenant(current_tenant)

View File

@ -15,7 +15,7 @@ def populate_local_hobo(apps, schema_editor):
pass
if hasattr(connection, 'get_tenant'):
build_absolute_uri = getattr(connection.get_tenant(), 'build_absolute_uri', None)
build_absolute_uri = getattr(connection.tenant, 'build_absolute_uri', None)
if build_absolute_uri:
Hobo.objects.create(
secret_key=get_local_key(build_absolute_uri('/')),

View File

@ -13,7 +13,7 @@ def populate_local_hobo(apps, schema_editor):
pass
if hasattr(connection, 'get_tenant'):
build_absolute_uri = getattr(connection.get_tenant(), 'build_absolute_uri', None)
build_absolute_uri = getattr(connection.tenant, 'build_absolute_uri', None)
if build_absolute_uri:
Hobo.objects.create(
secret_key=get_local_key(build_absolute_uri('/')),

View File

@ -60,8 +60,8 @@ def get_or_create_local_hobo():
hobo = Hobo.objects.get(local=True)
except Hobo.DoesNotExist:
build_absolute_uri = None
if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
build_absolute_uri = connection.get_tenant().build_absolute_uri
if hasattr(connection, 'get_tenant') and hasattr(connection.tenant, 'build_absolute_uri'):
build_absolute_uri = connection.tenant.build_absolute_uri
else:
request = StoreRequestMiddleware.get_request()
if request:

View File

@ -28,7 +28,7 @@ from tenant_schemas.postgresql_backend.base import FakeTenant
class WhooshSearchBackend(haystack.backends.whoosh_backend.WhooshSearchBackend):
@property
def use_file_storage(self):
tenant = connection.get_tenant()
tenant = connection.tenant
return not (isinstance(connection.tenant, FakeTenant))
@use_file_storage.setter
@ -53,7 +53,7 @@ class WhooshSearchBackend(haystack.backends.whoosh_backend.WhooshSearchBackend):
@property
def path(self):
tenant = connection.get_tenant()
tenant = connection.tenant
return os.path.join(tenant.get_directory(), 'whoosh_index')
@path.setter

View File

@ -24,7 +24,7 @@ class AdminEmailHandler(django.utils.log.AdminEmailHandler):
subject = super().format_subject(subject)
try:
subject = '[%s] %s' % (connection.get_tenant().domain_url, subject)
subject = '[%s] %s' % (connection.tenant.domain_url, subject)
except AttributeError:
pass
return subject

View File

@ -105,8 +105,8 @@ https://django-tenant-schemas.readthedocs.org/en/latest/use.html#creating-a-tena
if options.get('domain'):
domain = options['domain']
else:
if connection.get_tenant().schema_name != 'public':
return connection.get_tenant()
if connection.tenant.schema_name != 'public':
return connection.tenant
displayed_tenants = all_tenants
while True:
domain = input("Enter Tenant Domain ('?' to list): ")

View File

@ -59,7 +59,7 @@ def run_command_from_argv(command, argv):
command.stderr.write(str(e), lambda x: x)
else:
command.stderr.write(
'%s: %s: %s' % (connection.get_tenant(), e.__class__.__name__, exception_to_text(e))
'%s: %s: %s' % (connection.tenant, e.__class__.__name__, exception_to_text(e))
)
return e

View File

@ -87,7 +87,7 @@ class TenantSettingsWrapper:
return self.default_settings
try:
self.local.in_get_wrapped = True
tenant = connection.get_tenant()
tenant = connection.tenant
if not hasattr(tenant, 'domain_url'):
return self.default_settings
return self.get_tenant_settings(tenant)

View File

@ -34,7 +34,7 @@ def _new__bootstrap_inner(self):
if tenant is not None:
from django.db import connection
old_tenant = connection.get_tenant()
old_tenant = connection.tenant
connection.set_tenant(self.tenant)
try:
_Thread__bootstrap_inner(self)

View File

@ -320,7 +320,7 @@ def test_create_hobo_primary(mocked_TenantMiddleware, mocked_call_command, mocke
tenant = Mock()
tenant.schema_name = 'public'
tenant.get_directory = Mock(return_value='/foo')
mocked_connection.get_tenant = Mock(return_value=tenant)
mocked_connection.tenant = tenant
mocked_connection.set_tenant = Mock()
mocked_TenantMiddleware.get_tenant_by_hostname = Mock(return_value=tenant)
mocked_call_command.side_effect = CommandError
@ -348,7 +348,7 @@ def test_create_hobo_not_primary(mocked_TenantMiddleware, mocked_call_command, m
tenant = Mock()
tenant.schema_name = 'other than public'
tenant.get_directory = Mock(return_value='/foo')
mocked_connection.get_tenant = Mock(return_value=tenant)
mocked_connection.tenant = tenant
mocked_connection.set_tenant = Mock()
mocked_TenantMiddleware.get_tenant_by_hostname = Mock(return_value=tenant)
@ -419,21 +419,19 @@ def test_set_idp(command, db):
@patch('hobo.environment.management.commands.cook.connection')
@patch('hobo.agent.common.management.commands.hobo_deploy.Command.configure_theme')
def test_set_theme(mocked_configure_theme, mocked_connection, mocked_set_theme, command):
mocked_connection.get_tenant = Mock(return_value='the tenant')
mocked_connection.tenant = 'the tenant'
command.set_theme('the theme')
assert mocked_set_theme.mock_calls == [call('the theme')]
assert len(mocked_connection.get_tenant.mock_calls) == 1
assert mocked_configure_theme.mock_calls == [call({'variables': {'theme': 'the theme'}}, 'the tenant')]
@patch('hobo.environment.management.commands.cook.connection')
def test_cook(mocked_connection, command):
mocked_connection.get_tenant = Mock(return_value='the tenant')
mocked_connection.tenant = 'the tenant'
mocked_connection.set_tenant = Mock()
command.run_cook = Mock()
command.cook('a-recipe-file.json')
assert len(mocked_connection.get_tenant.mock_calls) == 1
assert command.run_cook.mock_calls == [call('a-recipe-file.json')]
assert mocked_connection.set_tenant.mock_calls == [call('the tenant')]