Projet

Général

Profil

0001-improve-application-s-webservices-config-22064.patch

Josué Kouka, 22 février 2018 19:05

Télécharger (4,09 ko)

Voir les différences:

Subject: [PATCH] improve application's webservices config (#22064)

 mandayejs/applications/__init__.py | 47 +++++++++++++++++++++++++++-----------
 mandayejs/applications/views.py    |  4 ++--
 2 files changed, 36 insertions(+), 15 deletions(-)
mandayejs/applications/__init__.py
20 20
from importlib import import_module
21 21

  
22 22
from django.conf import settings
23
from django.conf.urls import patterns, url
23
from django.conf.urls import url
24 24
from django.http import Http404
25 25
from django.core.exceptions import ImproperlyConfigured
26 26
from django.core.urlresolvers import resolve
......
35 35

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

  
......
90 90
        return slugify(type(self).__name__)
91 91

  
92 92

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

  
104
    def __init__(self, endpoints=None, urlpatterns=None):
105
        self.endpoints = endpoints
106
        self.urlpatterns = urlpatterns
107

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

  
114

  
93 115
# Test App Settings
94 116
class Test(AppSettings):
95 117
    SITE_LOGIN_PATH = '/'
......
188 210

  
189 211
    SITE_FORCE_REDIRECT_LOCATOR = '.connectBox'
190 212

  
191
    SITE_WS_ENDPOINT = {
192
        'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount',
193
    }
194

  
195
    urlpatterns = patterns(
196
        '',
197
        url(
198
            r'account/(?P<username>\w+)/$',
199
            'mandayejs.applications.views.archimed_account_details',
200
            name='archimed-account-details'),
213
    SITE_WEBSERVICES = AppWebservice(
214
        [
215
            {'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount'},
216
            {'login_url': '/DEFAULT/Ermes/Recherche/logon.svc/logon'}
217
        ],
218
        [
219
            url(r'account/(?P<username>\w+)/$', 'mandayejs.applications.views.archimed_account_details',
220
                name='archimed-account-details')
221
        ]
201 222
    )
202 223

  
203 224
    SITE_LOGOUT_LOCATOR = '.account_logoff'
mandayejs/applications/views.py
44 44
        logger = logging.getLogger(__name__)
45 45
        app_settings = get_app_settings()
46 46
        ws_uri = request.build_absolute_uri(
47
            app_settings.SITE_WS_ENDPOINT['account_details'])
47
            app_settings.SITE_WEBSERVICES.get_endpoint('account_details'))
48 48

  
49 49
        # mellon truncates username to 30 characters
50 50
        # thus the passed username must be truncated to 30 characters
......
62 62
            return Response('User %s is not associated' % username, status=status.HTTP_404_NOT_FOUND)
63 63

  
64 64
        login_url = request.build_absolute_uri(
65
            '/DEFAULT/Ermes/Recherche/logon.svc/logon')
65
            app_settings.SITE_WEBSERVICES.get_endpoint('login_url'))
66 66

  
67 67
        with requests.Session() as session:
68 68
            login_info = credentials.to_login_info(decrypt=True)
69
-