From 9635e4c8f514737103a51591ef921a8e81db61ee Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 8 Mar 2019 02:16:13 +0100 Subject: [PATCH 06/10] environment: factorize get_setting_variable from franceconnect (#29240) --- hobo/environment/utils.py | 23 +++++++++++++++++++++++ hobo/franceconnect/views.py | 15 ++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/hobo/environment/utils.py b/hobo/environment/utils.py index e1e5c76..ca07029 100644 --- a/hobo/environment/utils.py +++ b/hobo/environment/utils.py @@ -18,6 +18,7 @@ from django.conf import settings from django.core.urlresolvers import reverse from django.db import connection from django.utils.six.moves.urllib.parse import urlparse +from django.utils.encoding import force_text from hobo.middleware.utils import StoreRequestMiddleware @@ -81,6 +82,7 @@ def get_variable(name): }) return variable + def create_base_url(hostname, service): """ Distinguish mutualised domains (matching a "-" in the first part of the netloc) @@ -93,3 +95,24 @@ def create_base_url(hostname, service): else: netloc = '%s.%s' % (service, '.'.join(parts[1:])) return '%s://%s' % (ph.scheme, netloc) + + +def get_setting_variable(setting_name, label=None, service=None): + from .models import Variable, ContentType + + kwargs = { + 'name': 'SETTING_' + setting_name, + 'defaults': { + 'auto': True, + } + } + if label: + kwargs['defaults']['label'] = force_text(label) + if service: + kwargs['service_type'] = ContentType.objects.get_for_model(service) + kwargs['service_pk'] = service.pk + else: + kwargs['service_type__isnull'] = True + kwargs['service_pk__isnull'] = True + variable, created = Variable.objects.get_or_create(**kwargs) + return variable diff --git a/hobo/franceconnect/views.py b/hobo/franceconnect/views.py index a48f816..1a3b904 100644 --- a/hobo/franceconnect/views.py +++ b/hobo/franceconnect/views.py @@ -17,18 +17,15 @@ from django.core.urlresolvers import reverse_lazy from django.views.generic import FormView -from hobo.environment.models import Variable, Authentic +from hobo.environment.models import Authentic +from hobo.environment.utils import get_setting_variable from .forms import SettingsForm, EnableForm -def get_variable(name): - variable, created = Variable.objects.get_or_create( - name='SETTING_' + name, - defaults={ - 'auto': True, - 'service': Authentic.objects.get(secondary=False), - }) - return variable +def get_variable(setting_name): + return get_setting_variable( + setting_name, + service=Authentic.objects.get(secondary=False)) class HomeView(FormView): -- 2.23.0