From 62af28669a05156c825e9e42fef1633360b02df0 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 8 Mar 2019 02:16:13 +0100 Subject: [PATCH 5/9] environment: factorize get_setting_variable from franceconnect (#29240) --- hobo/environment/utils.py | 22 ++++++++++++++++++++++ hobo/franceconnect/views.py | 15 ++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/hobo/environment/utils.py b/hobo/environment/utils.py index 537c865..5a1d1a4 100644 --- a/hobo/environment/utils.py +++ b/hobo/environment/utils.py @@ -17,6 +17,7 @@ from django.conf import settings from django.core.urlresolvers import reverse from django.db import connection +from django.utils.encoding import force_text from hobo.middleware.utils import StoreRequestMiddleware @@ -79,3 +80,24 @@ def get_variable(name): 'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or '' }) return variable + + +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.20.1