From 3b6e11979e9af26d0ecf3ecb2e878f3eb219abc2 Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Thu, 16 Nov 2017 14:02:19 +0100 Subject: [PATCH] add application name and slug in association context (#19649) --- mandayejs/applications.py | 12 ++++++++++++ mandayejs/mandaye/views.py | 4 ++-- tests/test_mandayejs.py | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/mandayejs/applications.py b/mandayejs/applications.py index ff02ff3..7125e41 100644 --- a/mandayejs/applications.py +++ b/mandayejs/applications.py @@ -24,6 +24,7 @@ from django.conf.urls import patterns, url from django.http import Http404 from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import resolve +from django.utils.text import slugify def get_app_settings(): @@ -75,6 +76,17 @@ class AppSettingsMeta(type): dct['SITE_LOGIN_PATH'] = os.path.join(dct['SITE_LOGIN_PATH_PREFIX'], parent.SITE_LOGIN_PATH.strip('/')) return super(AppSettingsMeta, cls).__new__(cls, name, bases, dct) + @property + def name(self): + app_name = getattr(settings, 'SITE_APP_NAME', None) + if app_name: + return app_name + return self.__name__ + + @property + def slug(self): + return slugify(self.__name__) + class AppSettings(object): __metaclass__ = AppSettingsMeta diff --git a/mandayejs/mandaye/views.py b/mandayejs/mandaye/views.py index f5bc1a3..8c898c0 100644 --- a/mandayejs/mandaye/views.py +++ b/mandayejs/mandaye/views.py @@ -123,9 +123,9 @@ def associate(request, *args, **kwargs): return HttpResponseRedirect(resolve_url('post-login')) else: form = FormFactory() - + app_settings = get_app_settings() response = render(request, 'mandaye/associate.html', { - 'form': form, + 'form': form, 'appname': app_settings.name, 'appslug': app_settings.slug }) return response diff --git a/tests/test_mandayejs.py b/tests/test_mandayejs.py index 8469337..f6a4628 100644 --- a/tests/test_mandayejs.py +++ b/tests/test_mandayejs.py @@ -15,7 +15,7 @@ from django.core.urlresolvers import reverse from mandayejs.mandaye.models import UserCredentials from mandayejs.mandaye.utils import exec_phantom from mandayejs.mandaye.forms import FormFactory -from mandayejs.mandaye.views import post_login_do +from mandayejs.mandaye.views import associate, post_login_do from utils import create_user, create_credentials, get_uuid, get_user @@ -409,3 +409,16 @@ def test_post_login_do_with_next_url(mocked_popen, user_john): request.user = user_john response = post_login_do(request) assert 'window.top.location = "http://example.net/"' in response.content + + +@mock.patch('mandayejs.applications.Test.SITE_LOCATORS', MOCKED_SITE_LOCATORS) +def test_app_name_slug_in_association_context(settings, user_john): + client = Client() + client.login(username='john', password='john') + response = client.get(reverse('associate')) + assert response.context['appname'] == 'Test' + assert response.context['appslug'] == 'test' + # when overidding the appname in settings + settings.SITE_APP_NAME = 'Test A' + response = client.get(reverse('associate')) + assert response.context['appname'] == 'Test A' -- 2.11.0