Projet

Général

Profil

0001-misc-add-context-processor-user_urls-61192.patch

Benjamin Dauvergne, 27 janvier 2022 19:23

Télécharger (4,77 ko)

Voir les différences:

Subject: [PATCH] misc: add context-processor user_urls (#61192)

It introduces three new templates variables:
- login_url
- logout_url
- registration_url

Each one is adapter to return to the current page.
 debian/debian_config_common.py                |  1 +
 hobo/context_processors.py                    | 14 ++++++++++++++
 hobo/multitenant/management/commands/shell.py |  3 ++-
 hobo/multitenant/settings_loaders.py          |  2 +-
 tests/test_context_processors.py              | 12 +++++++++++-
 5 files changed, 29 insertions(+), 3 deletions(-)
debian/debian_config_common.py
241 241
        'hobo.context_processors.template_vars',
242 242
        'hobo.context_processors.theme_base',
243 243
        'hobo.context_processors.portal_agent_url',
244
        'hobo.context_processors.user_urls',
244 245
    ) + TEMPLATE_CONTEXT_PROCESSORS
245 246
else:
246 247
    assert len(TEMPLATES)
hobo/context_processors.py
10 10
from django.core.cache import cache
11 11
from django.template import Template
12 12
from django.utils.encoding import smart_bytes
13
from django.utils.http import urlencode
13 14
from django.utils.six.moves.urllib import parse as urlparse
14 15

  
15 16
from hobo.scrutiny.wsgi.middleware import VersionMiddleware
......
187 188
    context = TemplateVars.get_hobo_json_variables(get_hobo_json())
188 189
    context['manager_homepage_url'] = context.get(settings.HOBO_MANAGER_HOMEPAGE_URL_VAR)
189 190
    return context
191

  
192

  
193
def user_urls(request):
194
    template_vars = getattr(settings, 'TEMPLATE_VARS', {})
195
    full_path = request.get_full_path()
196
    query_string = urlencode({'next': full_path})
197
    context = {
198
        'login_url': '/login/?' + query_string,
199
        'logout_url': '/logout/?' + query_string,
200
    }
201
    if 'idp_registration_url' in template_vars:
202
        context['registration_url'] = template_vars['idp_registration_url'] + '?' + query_string
203
    return context
hobo/multitenant/management/commands/shell.py
7 7
    COMMAND = shell.Command
8 8

  
9 9
    def handle(self, *args, **kwargs):
10
        disable_global_logging()
10
        import pdb
11

  
11 12
        super(Command, self).handle(*args, **kwargs)
hobo/multitenant/settings_loaders.py
183 183
                    # expect authentic to search first in the default ou
184 184
                    params['service'] = variables['portal_user_slug']
185 185
            if params:
186
                variables['idp_registration_url'] += '?%s' % urlencode(params)
187 186
                variables['idp_service_params'] = urlencode({k: v for k, v in params.items() if k != 'next'})
188 187
                variables['idp_service_and_next_params'] = urlencode(params)
188
                variables['portal_slug_on_idp'] = params['service']
189 189

  
190 190
        if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_TITLE_VAR', None):
191 191
            variables['manager_homepage_title'] = variables.get(settings.HOBO_MANAGER_HOMEPAGE_TITLE_VAR)
tests/test_context_processors.py
6 6
from django.test import override_settings
7 7
from httmock import HTTMock, urlmatch
8 8

  
9
from hobo.context_processors import theme_base
9
from hobo.context_processors import theme_base, user_urls
10 10

  
11 11

  
12 12
def test_theme_base(settings, rf):
......
66 66
        context = theme_base(rf.get('/page1/page2/'))
67 67
        check(context, TEMPLATE_PAGE2)
68 68
        assert len(seen_urls) == 0
69

  
70

  
71
def test_user_urls(settings, rf):
72
    settings.TEMPLATE_VARS = {'idp_registration_url': 'https://idp/register/'}
73
    request = rf.get('/page/')
74
    assert user_urls(request) == {
75
        'login_url': '/login/?next=%2Fpage%2F',
76
        'logout_url': '/logout/?next=%2Fpage%2F',
77
        'registration_url': 'https://idp/register/?next=%2Fpage%2F',
78
    }
69
-