Skip to content

Commit

Permalink
Add context processor for the TENANT_SCHEMA_NAME template variable
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jul 1, 2024
1 parent ae97c40 commit 651a421
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
15 changes: 8 additions & 7 deletions tcms_settings_dir/multi_tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@
# everybody can access the main instance
SHARED_APPS = INSTALLED_APPS # noqa: F821

# add tennants context processor
if (
"tcms_tenants.context_processors.tenant_navbar_processor"
not in TEMPLATES[0]["OPTIONS"]["context_processors"] # noqa: F821
# extra context processors
for processor in (
"tcms_tenants.context_processors.schema_name_processor",
"tcms_tenants.context_processors.tenant_navbar_processor",
):
TEMPLATES[0]["OPTIONS"]["context_processors"].append( # noqa: F821
"tcms_tenants.context_processors.tenant_navbar_processor"
) # noqa: F821
if processor not in TEMPLATES[0]["OPTIONS"]["context_processors"]: # noqa: F821
TEMPLATES[0]["OPTIONS"]["context_processors"].append( # noqa: F821
processor
) # noqa: F821
13 changes: 12 additions & 1 deletion tcms_tenants/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2022 Ivajlo Karabojkov <karabojkov@kitbg.com>
# Copyright (c) 2022 Alexander Todorov <atodorov@otb.bg>
# Copyright (c) 2022-2024 Alexander Todorov <atodorov@otb.bg>
#
# Licensed under GNU Affero General Public License v3 or later (AGPLv3+)
# https://www.gnu.org/licenses/agpl-3.0.html
Expand Down Expand Up @@ -29,3 +29,14 @@ def tenant_navbar_processor(request):
{"tenant_name": tenant_name, "tenant_logo": tenant_logo},
)
return {"CUSTOMIZED_LOGO_CONTENTS": customized_logo_contents}


def schema_name_processor(request):
"""
Provide tenant.schema_name for use in templates
"""
return {
"TENANT_SCHEMA_NAME": getattr(
getattr(request, "tenant", None), "schema_name", ""
)
}
12 changes: 10 additions & 2 deletions tcms_tenants/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2022 Alexander Todorov <atodorov@otb.bg>
# Copyright (c) 2019-2024 Alexander Todorov <atodorov@otb.bg>
#
# Licensed under GNU Affero General Public License v3 or later (AGPLv3+)
# https://www.gnu.org/licenses/agpl-3.0.html
Expand All @@ -14,11 +14,12 @@
from django.http import HttpResponseForbidden, HttpResponseNotFound

from tcms_tenants.tests import LoggedInTestCase
from tcms_tenants.context_processors import schema_name_processor
from tcms_tenants.context_processors import tenant_navbar_processor


class ContextProcessor(TestCase):
def test_without_tenant(self):
def test_navbar_processor_without_tenant(self):
"""
This simulates the case where TenantMainMiddleware returns a 404 for
non-existing tenant but there's no request.tenant attribute and the
Expand All @@ -30,6 +31,13 @@ def test_without_tenant(self):
result = tenant_navbar_processor(request)
self.assertEqual(result, {"CUSTOMIZED_LOGO_CONTENTS": ""})

def test_schema_name_processor_without_tenant(self):
factory = RequestFactory()
request = factory.get("/robots.txt", HTTP_HOST="non-existing.tenant.bg")

result = schema_name_processor(request)
self.assertEqual(result, {"TENANT_SCHEMA_NAME": ""})


class BlockUnauthorizedUserMiddlewareTestCase(LoggedInTestCase):
def test_unauthorized_user_cant_access_tenant(self):
Expand Down

0 comments on commit 651a421

Please sign in to comment.