misc: improve application configuration
This commit is contained in:
parent
e10c859365
commit
d320ab039e
|
@ -16,7 +16,6 @@
|
|||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
from importlib import import_module
|
||||
|
||||
from django.conf import settings
|
||||
|
@ -41,53 +40,86 @@ def app_web_services(request, path):
|
|||
raise Http404
|
||||
|
||||
|
||||
# App Settings
|
||||
class AppSettingsMeta(type):
|
||||
REQUIRED_PARAMS = [
|
||||
'SITE_LOGIN_PATH',
|
||||
'SITE_LOCATORS',
|
||||
'SITE_AUTH_CHECKER',
|
||||
'SITE_AUTH_COOKIE_KEYS',
|
||||
]
|
||||
|
||||
REQUIRED_KEYS = [
|
||||
'SITE_LOGIN_PATH',
|
||||
'SITE_LOCATORS',
|
||||
'SITE_AUTH_CHECKER',
|
||||
'SITE_AUTH_COOKIE_KEYS',
|
||||
]
|
||||
|
||||
def __new__(cls, name, bases, dct):
|
||||
if name != 'AppSettings':
|
||||
if AppSettings in bases: # Avoid checking keys for inherited classes
|
||||
class Param(object):
|
||||
|
||||
missing_key = set(cls.REQUIRED_KEYS).difference(set(dct.keys())) or None
|
||||
if missing_key:
|
||||
raise ImproperlyConfigured('{} is mandatory'.format(missing_key.pop()))
|
||||
def __init__(self, initval=None, name=None):
|
||||
self.value = initval
|
||||
if name:
|
||||
self.name = name
|
||||
|
||||
if not set(('SITE_FORCE_REDIRECT_URL', 'SITE_FORCE_REDIRECT_LOCATOR')).intersection(set(dct.keys())):
|
||||
raise ImproperlyConfigured(
|
||||
'one of these settings ({}) must be defined'
|
||||
.format(('SITE_FORCE_REDIRECT_URL', 'SITE_FORCE_REDIRECT_LOCATOR')))
|
||||
def __set__(self, instance, value):
|
||||
self.value = value
|
||||
|
||||
# Default form submit element
|
||||
if not dct.get('SITE_FORM_SUBMIT_ELEMENT', None):
|
||||
dct['SITE_FORM_SUBMIT_ELEMENT'] = 'input[type=submit], button'
|
||||
def __get__(self, instance, owner):
|
||||
if instance is None:
|
||||
return self
|
||||
if hasattr(settings, self.name):
|
||||
return getattr(settings, self.name)
|
||||
return self.value
|
||||
|
||||
return super(AppSettingsMeta, cls).__new__(cls, name, bases, dct)
|
||||
|
||||
class LoginPathParam(Param):
|
||||
name = 'SITE_LOGIN_PATH'
|
||||
|
||||
|
||||
class LocatorsParam(Param):
|
||||
name = 'SITE_LOCATORS'
|
||||
|
||||
|
||||
class AuthCheckerParam(Param):
|
||||
name = 'SITE_AUTH_CHECKER'
|
||||
|
||||
|
||||
class AuthCookieKeysParam(Param):
|
||||
name = 'SITE_AUTH_COOKIE_KEYS'
|
||||
|
||||
|
||||
class ForceRedirctUrlParam(Param):
|
||||
name = 'SITE_FORCE_REDIRECT_URL'
|
||||
|
||||
|
||||
class ForceRedirectLocatorParam(Param):
|
||||
name = 'SITE_FORCE_REDIRECT_LOCATOR'
|
||||
|
||||
|
||||
class FormSubmitElementParam(Param):
|
||||
name = 'SITE_FORM_SUBMIT_ELEMENT'
|
||||
|
||||
|
||||
class LogoutLocatorParam(Param):
|
||||
name = 'LOGOUT_LOCATOR'
|
||||
|
||||
|
||||
class AppScriptParam(Param):
|
||||
name = 'APP_SCRIPT'
|
||||
|
||||
|
||||
class AppSettings(object):
|
||||
__metaclass__ = AppSettingsMeta
|
||||
|
||||
def __getattribute__(self, name):
|
||||
value = getattr(settings, name, None)
|
||||
if value is not None:
|
||||
return value
|
||||
return super(AppSettings, self).__getattribute__(name)
|
||||
SITE_FORM_SUBMIT_ELEMENT = FormSubmitElementParam('input[type=submit], button')
|
||||
|
||||
def get_name(self):
|
||||
name = getattr(settings, 'SITE_APP_NAME', None)
|
||||
if name:
|
||||
return name
|
||||
return type(self).__name__
|
||||
def __init__(self, *args, **kwargs):
|
||||
for param in REQUIRED_PARAMS:
|
||||
if not hasattr(self, param):
|
||||
raise ImproperlyConfigured('missing required parameter <%s>' % param)
|
||||
|
||||
def get_slug(self):
|
||||
return slugify(type(self).__name__)
|
||||
@property
|
||||
def name(self):
|
||||
if hasattr(settings, 'SITE_APP_NAME'):
|
||||
return settings.SITE_APP_NAME
|
||||
return self.__class__.__name__
|
||||
|
||||
@property
|
||||
def slug(self):
|
||||
return slugify(self.__class__.__name__)
|
||||
|
||||
|
||||
class AppWebservice(object):
|
||||
|
@ -114,8 +146,8 @@ class AppWebservice(object):
|
|||
|
||||
# Test App Settings
|
||||
class Test(AppSettings):
|
||||
SITE_LOGIN_PATH = '/'
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOGIN_PATH = LoginPathParam('/')
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#login',
|
||||
'label': 'Login',
|
||||
|
@ -128,16 +160,16 @@ class Test(AppSettings):
|
|||
'name': 'password',
|
||||
'kind': 'password'
|
||||
}
|
||||
]
|
||||
SITE_AUTH_CHECKER = 'js/test/auth.checker.js'
|
||||
SITE_AUTH_COOKIE_KEYS = ['test']
|
||||
SITE_FORCE_REDIRECT_URL = '/whatever'
|
||||
])
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('js/test/auth.checker.js')
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam(['test'])
|
||||
SITE_FORCE_REDIRECT_URL = ForceRedirctUrlParam('/whatever')
|
||||
|
||||
|
||||
# Duonet App Settings
|
||||
class Duonet(AppSettings):
|
||||
SITE_LOGIN_PATH = '/'
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOGIN_PATH = LoginPathParam('/')
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#txtNomFoyer',
|
||||
'label': 'Nom de famille',
|
||||
|
@ -162,26 +194,26 @@ class Duonet(AppSettings):
|
|||
'required': True,
|
||||
'help': ''
|
||||
},
|
||||
]
|
||||
SITE_AUTH_CHECKER = 'duonet/js/auth.checker.js'
|
||||
SITE_AUTH_COOKIE_KEYS = [
|
||||
])
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('duonet/js/auth.checker.js')
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam([
|
||||
'ASP.NET_SessionId',
|
||||
]
|
||||
SITE_APP_SCRIPTS = [
|
||||
])
|
||||
SITE_APP_SCRIPTS = AppScriptParam([
|
||||
'duonet/js/duonet.js',
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORCE_REDIRECT_URL = '/Connect.aspx'
|
||||
SITE_FORCE_REDIRECT_URL = ForceRedirctUrlParam('/Connect.aspx')
|
||||
|
||||
SITE_FORM_SUBMIT_ELEMENT = 'input[type=submit]'
|
||||
SITE_FORM_SUBMIT_ELEMENT = FormSubmitElementParam('input[type=submit]')
|
||||
|
||||
SITE_LOGOUT_LOCATOR = '#lnkDisconnect'
|
||||
SITE_LOGOUT_LOCATOR = LogoutLocatorParam('#lnkDisconnect')
|
||||
|
||||
|
||||
# Archimed App Settings
|
||||
class Archimed(AppSettings):
|
||||
SITE_LOGIN_PATH = '/'
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOGIN_PATH = LoginPathParam('/')
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#carte',
|
||||
'label': 'Identifiant',
|
||||
|
@ -198,17 +230,17 @@ class Archimed(AppSettings):
|
|||
'required': True,
|
||||
'help': ''
|
||||
},
|
||||
]
|
||||
SITE_AUTH_CHECKER = 'archimed/js/auth.checker.js'
|
||||
SITE_AUTH_COOKIE_KEYS = [
|
||||
])
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('archimed/js/auth.checker.js')
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam([
|
||||
'S_ARCHIMED_CRYSTAL_AUTH'
|
||||
]
|
||||
])
|
||||
|
||||
SITE_APP_SCRIPTS = [
|
||||
SITE_APP_SCRIPTS = AppScriptParam([
|
||||
'archimed/js/archimed.js'
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORCE_REDIRECT_LOCATOR = '.connectBox'
|
||||
SITE_FORCE_REDIRECT_LOCATOR = ForceRedirectLocatorParam('.connectBox')
|
||||
|
||||
SITE_WEBSERVICES = AppWebservice(
|
||||
[
|
||||
|
@ -221,13 +253,13 @@ class Archimed(AppSettings):
|
|||
]
|
||||
)
|
||||
|
||||
SITE_LOGOUT_LOCATOR = '.account_logoff'
|
||||
SITE_LOGOUT_LOCATOR = LogoutLocatorParam('.account_logoff')
|
||||
|
||||
|
||||
# Arpege App Settings
|
||||
class Arpege(AppSettings):
|
||||
SITE_LOGIN_PATH = '/index.do'
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOGIN_PATH = LoginPathParam('/index.do')
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#cdfmll',
|
||||
'label': 'Code famille',
|
||||
|
@ -244,25 +276,25 @@ class Arpege(AppSettings):
|
|||
'required': True,
|
||||
'help': '',
|
||||
}
|
||||
]
|
||||
SITE_AUTH_CHECKER = 'arpege/js/auth.checker.js'
|
||||
SITE_AUTH_COOKIE_KEYS = [
|
||||
])
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('arpege/js/auth.checker.js')
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam([
|
||||
'JSESSIONID',
|
||||
]
|
||||
])
|
||||
|
||||
SITE_APP_SCRIPTS = [
|
||||
SITE_APP_SCRIPTS = AppScriptParam([
|
||||
'arpege/js/arpege.js',
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORCE_REDIRECT_LOCATOR = '.formulaire'
|
||||
SITE_FORCE_REDIRECT_LOCATOR = ForceRedirectLocatorParam(r'.formulaire')
|
||||
|
||||
SITE_LOGOUT_LOCATOR = '#espace-login form input[type=submit]'
|
||||
SITE_LOGOUT_LOCATOR = LogoutLocatorParam('#espace-login form input[type=submit]')
|
||||
|
||||
|
||||
class Imuse(AppSettings):
|
||||
SITE_LOGIN_PATH = 'extranet/login/gen_index_groupe.php?nav=autre'
|
||||
SITE_LOGIN_PATH = LoginPathParam('extranet/login/gen_index_groupe.php?nav=autre')
|
||||
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#INDEX_USER_ID',
|
||||
'label': 'Identifiant',
|
||||
|
@ -279,23 +311,23 @@ class Imuse(AppSettings):
|
|||
'required': True,
|
||||
'help': ''
|
||||
}
|
||||
]
|
||||
])
|
||||
|
||||
SITE_AUTH_CHECKER = 'imuse/js/auth.checker.js'
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('imuse/js/auth.checker.js')
|
||||
|
||||
SITE_AUTH_COOKIE_KEYS = [
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam([
|
||||
'iMuse-extranet'
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORCE_REDIRECT_LOCATOR = '#INDEX_TBL_LOGIN'
|
||||
SITE_FORCE_REDIRECT_LOCATOR = ForceRedirectLocatorParam('#INDEX_TBL_LOGIN')
|
||||
|
||||
SITE_FORM_SUBMIT_ELEMENT = '#INDEX_BT_LOGIN'
|
||||
SITE_FORM_SUBMIT_ELEMENT = FormSubmitElementParam('#INDEX_BT_LOGIN')
|
||||
|
||||
|
||||
class Sezhame(AppSettings):
|
||||
SITE_LOGIN_PATH = '/sezhame/page/connexion-abonne?destination=user'
|
||||
SITE_LOGIN_PATH = LoginPathParam('/sezhame/page/connexion-abonne?destination=user')
|
||||
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#edit-user',
|
||||
'label': 'Numero de cqrte',
|
||||
|
@ -312,22 +344,22 @@ class Sezhame(AppSettings):
|
|||
'required': True,
|
||||
'help': ''
|
||||
}
|
||||
]
|
||||
])
|
||||
|
||||
SITE_AUTH_CHECKER = 'sezhame/js/auth.checker.js'
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('sezhame/js/auth.checker.js')
|
||||
|
||||
SITE_AUTH_COOKIE_KEYS = [
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam([
|
||||
'SESSf36da25307ad6240a58ddd4f4b138952',
|
||||
'ASPSESSIONIDQSDRASTR'
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORCE_REDIRECT_LOCATOR = '#dk-opac15-login-form'
|
||||
SITE_FORCE_REDIRECT_LOCATOR = ForceRedirectLocatorParam('#dk-opac15-login-form')
|
||||
|
||||
|
||||
class Teamnet(AppSettings):
|
||||
SITE_LOGIN_PATH = '/auth/teamnetauth'
|
||||
SITE_LOGIN_PATH = LoginPathParam('/auth/teamnetauth')
|
||||
|
||||
SITE_LOCATORS = [
|
||||
SITE_LOCATORS = LocatorsParam([
|
||||
{
|
||||
'id': '#login',
|
||||
'label': 'Mon identifiant',
|
||||
|
@ -344,16 +376,16 @@ class Teamnet(AppSettings):
|
|||
'required': True,
|
||||
'help': ''
|
||||
}
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORM_SUBMIT_ELEMENT = "input[type=submit][value='Me connecter']"
|
||||
SITE_FORM_SUBMIT_ELEMENT = FormSubmitElementParam("input[type=submit][value='Me connecter']")
|
||||
|
||||
SITE_AUTH_CHECKER = 'teamnet/js/auth.checker.js'
|
||||
SITE_AUTH_CHECKER = AuthCheckerParam('teamnet/js/auth.checker.js')
|
||||
|
||||
SITE_AUTH_COOKIE_KEYS = [
|
||||
SITE_AUTH_COOKIE_KEYS = AuthCookieKeysParam([
|
||||
'JSESSIONID',
|
||||
]
|
||||
])
|
||||
|
||||
SITE_FORCE_REDIRECT_LOCATOR = '#loginForm'
|
||||
SITE_FORCE_REDIRECT_LOCATOR = ForceRedirectLocatorParam('#loginForm')
|
||||
|
||||
SITE_LOGOUT_LOCATOR = ".infoUtilisateur[alt=Deconnexion]"
|
||||
SITE_LOGOUT_LOCATOR = LogoutLocatorParam(".infoUtilisateur[alt=Deconnexion]")
|
||||
|
|
|
@ -126,7 +126,7 @@ def associate(request, *args, **kwargs):
|
|||
app_settings = get_app_settings()
|
||||
response = render(request, 'mandaye/associate.html', {
|
||||
'form': form, 'app': {
|
||||
'name': app_settings.get_name(), 'slug': app_settings.get_slug()}})
|
||||
'name': app_settings.name, 'slug': app_settings.slug}})
|
||||
return response
|
||||
|
||||
|
||||
|
|
Reference in New Issue