Projet

Général

Profil

Télécharger (9,59 ko) Statistiques
| Branche: | Tag: | Révision:

mandayejs / mandayejs / applications / __init__.py @ 83c9a56a

1
# mandayejs - saml reverse proxy
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

    
17
from __future__ import unicode_literals
18

    
19
import os
20
from importlib import import_module
21

    
22
from django.conf import settings
23
from django.conf.urls import url
24
from django.http import Http404
25
from django.core.exceptions import ImproperlyConfigured
26
from django.core.urlresolvers import resolve
27
from django.utils.text import slugify
28

    
29
from mandayejs.applications import views
30

    
31

    
32
def get_app_settings():
33
    module_name, app_settings = tuple(settings.SITE_APP.rsplit('.', 1))
34
    module = import_module(module_name)
35
    return getattr(module, app_settings)()
36

    
37

    
38
def app_web_services(request, path):
39
    app = get_app_settings()
40
    if hasattr(app, 'SITE_WEBSERVICES'):
41
        view, args, kwargs = resolve(request.path, urlconf=app.SITE_WEBSERVICES)
42
        return view(request, *args, **kwargs)
43
    raise Http404
44

    
45

    
46
# App Settings
47
class AppSettingsMeta(type):
48

    
49
    REQUIRED_KEYS = [
50
        'SITE_LOGIN_PATH',
51
        'SITE_LOCATORS',
52
        'SITE_AUTH_CHECKER',
53
        'SITE_AUTH_COOKIE_KEYS',
54
    ]
55

    
56
    def __new__(cls, name, bases, dct):
57
        if name != 'AppSettings':
58
            if AppSettings in bases:  # Avoid checking keys for inherited classes
59

    
60
                missing_key = set(cls.REQUIRED_KEYS).difference(set(dct.keys())) or None
61
                if missing_key:
62
                    raise ImproperlyConfigured('{} is mandatory'.format(missing_key.pop()))
63

    
64
                if not set(('SITE_FORCE_REDIRECT_URL', 'SITE_FORCE_REDIRECT_LOCATOR')).intersection(set(dct.keys())):
65
                    raise ImproperlyConfigured(
66
                        'one of these settings ({}) must be defined'
67
                        .format(('SITE_FORCE_REDIRECT_URL', 'SITE_FORCE_REDIRECT_LOCATOR')))
68

    
69
                # Default form submit element
70
                if not dct.get('SITE_FORM_SUBMIT_ELEMENT', None):
71
                    dct['SITE_FORM_SUBMIT_ELEMENT'] = 'input[type=submit], button'
72

    
73
        return super(AppSettingsMeta, cls).__new__(cls, name, bases, dct)
74

    
75

    
76
class AppSettings(object):
77
    __metaclass__ = AppSettingsMeta
78

    
79
    def __getattribute__(self, name):
80
        value = getattr(settings, name, None)
81
        if value is not None:
82
            return value
83
        return super(AppSettings, self).__getattribute__(name)
84

    
85
    def get_name(self):
86
        name = getattr(settings, 'SITE_APP_NAME', None)
87
        if name:
88
            return name
89
        return type(self).__name__
90

    
91
    def get_slug(self):
92
        return slugify(type(self).__name__)
93

    
94

    
95
class AppWebservice(object):
96
    '''- endpoints = [
97
        {'foo': 'https://example.org/foo/'},
98
        {'bar': 'https://example.org/bar/'},
99
    ]
100
    - urlpatterns = [
101
        url('user/(?P<username>\w+)/$', views.myapp.user_details, name='user-details'),
102
        url('user/(?P<username>\w+/books/$)', views.myapp.user_books, name='user-books'),
103
    ]
104
    '''
105

    
106
    def __init__(self, endpoints=None, urlpatterns=None):
107
        self.endpoints = endpoints
108
        self.urlpatterns = urlpatterns
109

    
110
    def get_endpoint(self, name):
111
        for endpoint in self.endpoints:
112
            if endpoint.get(name, None):
113
                return endpoint[name]
114
        return None
115

    
116

    
117
# Test App Settings
118
class Test(AppSettings):
119
    SITE_LOGIN_PATH = '/'
120
    SITE_LOCATORS = [
121
        {
122
            'id': '#login',
123
            'label': 'Login',
124
            'name': 'login',
125
            'kind': 'string',
126
        },
127
        {
128
            'id': '#password',
129
            'label': 'Password',
130
            'name': 'password',
131
            'kind': 'password'
132
        }
133
    ]
134
    SITE_AUTH_CHECKER = 'js/test/auth.checker.js'
135
    SITE_AUTH_COOKIE_KEYS = ['test']
136
    SITE_FORCE_REDIRECT_URL = '/whatever'
137
    SITE_FORM_SUBMIT_ELEMENT = None
138

    
139

    
140
# Duonet App Settings
141
class Duonet(AppSettings):
142
    SITE_LOGIN_PATH = '/'
143
    SITE_LOCATORS = [
144
        {
145
            'id': '#txtNomFoyer',
146
            'label': 'Nom de famille',
147
            'name': 'txtNomFoyer',
148
            'kind': 'string',
149
            'required': True,
150
            'help': '',
151
        },
152
        {
153
            'id': '#txtDateNaissance',
154
            'label': 'Date de naissance',
155
            'name': 'txtDateNaissance',
156
            'kind': 'date',
157
            'required': True,
158
            'help': 'exemple 16/06/2008'
159
        },
160
        {
161
            'id': '#txtCode',
162
            'label': 'Mot de passe',
163
            'name': 'txtCode',
164
            'kind': 'password',
165
            'required': True,
166
            'help': ''
167
        },
168
    ]
169
    SITE_AUTH_CHECKER = 'duonet/js/auth.checker.js'
170
    SITE_AUTH_COOKIE_KEYS = [
171
        'ASP.NET_SessionId',
172
    ]
173
    SITE_APP_SCRIPTS = [
174
        'duonet/js/duonet.js',
175
    ]
176

    
177
    SITE_FORCE_REDIRECT_URL = '/Connect.aspx'
178

    
179
    SITE_FORM_SUBMIT_ELEMENT = 'input[type=submit]'
180

    
181
    SITE_LOGOUT_LOCATOR = '#lnkDisconnect'
182

    
183

    
184
# Archimed App Settings
185
class Archimed(AppSettings):
186
    SITE_LOGIN_PATH = '/'
187
    SITE_LOCATORS = [
188
        {
189
            'id': '#carte',
190
            'label': 'Identifiant',
191
            'name': 'carte',
192
            'kind': 'string',
193
            'required': True,
194
            'help': '',
195
        },
196
        {
197
            'id': '#code',
198
            'label': 'Mot de passe',
199
            'name': 'code',
200
            'kind': 'password',
201
            'required': True,
202
            'help': ''
203
        },
204
    ]
205
    SITE_AUTH_CHECKER = 'archimed/js/auth.checker.js'
206
    SITE_AUTH_COOKIE_KEYS = [
207
        'S_ARCHIMED_CRYSTAL_AUTH'
208
    ]
209

    
210
    SITE_APP_SCRIPTS = [
211
        'archimed/js/archimed.js'
212
    ]
213

    
214
    SITE_FORCE_REDIRECT_LOCATOR = '.connectBox'
215

    
216
    SITE_WEBSERVICES = AppWebservice(
217
        [
218
            {'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount'},
219
            {'login_url': '/DEFAULT/Ermes/Recherche/logon.svc/logon'}
220
        ],
221
        [
222
            url(r'account/(?P<username>\w+)/$', views.archimed_account_details,
223
                name='archimed-account-details')
224
        ]
225
    )
226

    
227
    SITE_LOGOUT_LOCATOR = '.account_logoff'
228

    
229

    
230
# Arpege App Settings
231
class Arpege(AppSettings):
232
    SITE_LOGIN_PATH = '/index.do'
233
    SITE_LOCATORS = [
234
        {
235
            'id': '#cdfmll',
236
            'label': 'Code famille',
237
            'name': 'cdfmll',
238
            'kind': 'string',
239
            'required': True,
240
            'help': '',
241
        },
242
        {
243
            'id': '#mtdpss',
244
            'label': 'Mot de passe',
245
            'name': 'mtdpss',
246
            'kind': 'password',
247
            'required': True,
248
            'help': '',
249
        }
250
    ]
251
    SITE_AUTH_CHECKER = 'arpege/js/auth.checker.js'
252
    SITE_AUTH_COOKIE_KEYS = [
253
        'JSESSIONID',
254
    ]
255

    
256
    SITE_APP_SCRIPTS = [
257
        'arpege/js/arpege.js',
258
    ]
259

    
260
    SITE_FORCE_REDIRECT_LOCATOR = '.formulaire'
261

    
262
    SITE_LOGOUT_LOCATOR = '#espace-login form input[type=submit]'
263

    
264

    
265
class Imuse(AppSettings):
266
    SITE_LOGIN_PATH = 'extranet/login/gen_index_groupe.php?nav=autre'
267

    
268
    SITE_LOCATORS = [
269
        {
270
            'id': '#INDEX_USER_ID',
271
            'label': 'Identifiant',
272
            'name': 'INDEX_USER_ID',
273
            'kind': 'string',
274
            'required': True,
275
            'help': ''
276
        },
277
        {
278
            'id': '#INDEX_USER_PWD',
279
            'label': 'Mot de passe',
280
            'name': 'INDEX_USER_PWD',
281
            'kind': 'password',
282
            'required': True,
283
            'help': ''
284
        }
285
    ]
286

    
287
    SITE_AUTH_CHECKER = 'imuse/js/auth.checker.js'
288

    
289
    SITE_AUTH_COOKIE_KEYS = [
290
        'iMuse-extranet'
291
    ]
292

    
293
    SITE_FORCE_REDIRECT_LOCATOR = '#INDEX_TBL_LOGIN'
294

    
295
    SITE_FORM_SUBMIT_ELEMENT = '#INDEX_BT_LOGIN'
296

    
297

    
298
class Sezhame(AppSettings):
299
    SITE_LOGIN_PATH = '/sezhame/page/connexion-abonne?destination=user'
300

    
301
    SITE_LOCATORS = [
302
        {
303
            'id': '#edit-user',
304
            'label': 'Numero de cqrte',
305
            'name': 'edit-user',
306
            'kind': 'string',
307
            'required': True,
308
            'help': ''
309
        },
310
        {
311
            'id': '#edit-password',
312
            'label': 'Mot de passe',
313
            'name': 'edit-password',
314
            'kind': 'password',
315
            'required': True,
316
            'help': ''
317
        }
318
    ]
319

    
320
    SITE_AUTH_CHECKER = 'sezhame/js/auth.checker.js'
321

    
322
    SITE_AUTH_COOKIE_KEYS = [
323
        'SESSf36da25307ad6240a58ddd4f4b138952',
324
        'ASPSESSIONIDQSDRASTR'
325
    ]
326

    
327
    SITE_FORCE_REDIRECT_LOCATOR = '#dk-opac15-login-form'
328

    
329

    
330
class Teamnet(AppSettings):
331
    SITE_LOGIN_PATH = '/auth/teamnetauth'
332

    
333
    SITE_LOCATORS = [
334
        {
335
            'id': '#login',
336
            'label': 'Mon identifiant',
337
            'name': 'login',
338
            'kind': 'string',
339
            'required': True,
340
            'help': ''
341
        },
342
        {
343
            'id': '#mdp',
344
            'label': 'Mon mot de passe',
345
            'name': 'mdp',
346
            'kind': 'password',
347
            'required': True,
348
            'help': ''
349
        }
350
    ]
351

    
352
    SITE_FORM_SUBMIT_ELEMENT = "input[type=submit][value='Me connecter']"
353

    
354
    SITE_AUTH_CHECKER = 'teamnet/js/auth.checker.js'
355

    
356
    SITE_AUTH_COOKIE_KEYS = [
357
        'JSESSIONID',
358
    ]
359

    
360
    SITE_FORCE_REDIRECT_LOCATOR = '#loginForm'
361

    
362
    SITE_LOGOUT_LOCATOR = ".infoUtilisateur[alt=Deconnexion]"
(1-1/2)