18 |
18 |
|
19 |
19 |
import logging
|
20 |
20 |
import requests
|
21 |
|
from pprint import pprint
|
22 |
|
from requests.cookies import RequestsCookieJar
|
23 |
21 |
|
24 |
22 |
from django.contrib.auth.models import User
|
25 |
23 |
from django.shortcuts import get_object_or_404
|
... | ... | |
29 |
27 |
from rest_framework.response import Response
|
30 |
28 |
|
31 |
29 |
from mandayejs.mandaye.models import UserCredentials
|
32 |
|
from mandayejs.mandaye.utils import exec_phantom, get_login_info
|
33 |
30 |
from mandayejs.applications import get_app_settings
|
34 |
31 |
|
35 |
32 |
|
... | ... | |
40 |
37 |
def get(self, request, *args, **kwargs):
|
41 |
38 |
logger = logging.getLogger(__name__)
|
42 |
39 |
app_settings = get_app_settings()
|
43 |
|
|
44 |
40 |
ws_uri = app_settings.SITE_WS_ENDPOINT['account_details']
|
45 |
41 |
|
46 |
42 |
username = kwargs['username']
|
47 |
43 |
user = get_object_or_404(User, username=username)
|
48 |
44 |
credentials = get_object_or_404(UserCredentials, user=user)
|
49 |
45 |
|
50 |
|
login_info = get_login_info(request, credentials)
|
51 |
|
logger.debug(login_info)
|
52 |
|
login_info['locators'] = [credentials.to_login_info(decrypt=True)]
|
53 |
|
result = exec_phantom(login_info)
|
54 |
|
|
55 |
|
if result.get('result') != 'ok':
|
56 |
|
return Response(status=status.HTTP_401_UNAUTHORIZED)
|
57 |
|
|
58 |
|
session = requests.session()
|
59 |
|
r_cookies = RequestsCookieJar()
|
60 |
|
|
61 |
|
for cookie in result.get('cookies'):
|
62 |
|
r_cookies.set(
|
63 |
|
cookie['name'],
|
64 |
|
cookie['value'],
|
65 |
|
domain=cookie['domain'],
|
66 |
|
path=cookie['path'],
|
67 |
|
secure=cookie['secure']
|
68 |
|
)
|
69 |
|
session.cookies = r_cookies
|
|
46 |
login_url = '/DEFAULT/Ermes/Recherche/logon.svc/logon'
|
|
47 |
login_url = request.build_absolute_uri(login_url)
|
|
48 |
|
|
49 |
with requests.Session() as session:
|
|
50 |
login_info = credentials.to_login_info(decrypt=True)
|
|
51 |
login_info = {'username': login_info['#carte'], 'password': login_info['#code']}
|
|
52 |
response = session.post(login_url, data=login_info)
|
|
53 |
logger.debug("Archimed login response {}".format(response.json()))
|
|
54 |
if not response.json()['success']:
|
|
55 |
return Response('Authentication failed', status=status.HTTP_401_UNAUTHORIZED)
|
|
56 |
|
|
57 |
content = {
|
|
58 |
'codeConfig': '',
|
|
59 |
'xslPath': 'Services/LectorShortAccount.xslt'
|
|
60 |
}
|
|
61 |
response = session.post(ws_uri, json=content)
|
|
62 |
logger.debug("Archimed ws response {}".format(response.json()))
|
|
63 |
return Response(response.json())
|
70 |
64 |
|
71 |
|
headers = {
|
72 |
|
'Content-Type': 'application/json',
|
73 |
|
}
|
74 |
|
content = '{"codeConfig":"", "xslPath":"Services/LectorShortAccount.xslt"}'
|
75 |
|
url = request.build_absolute_uri(ws_uri)
|
76 |
|
logger.debug(url)
|
77 |
|
request_response = session.post(url, headers=headers, data=content, verify=False)
|
78 |
|
data = request_response.json()
|
79 |
|
logger.debug(pprint(data))
|
80 |
|
return Response(data)
|
81 |
65 |
|
82 |
66 |
archimed_account_details = ArchimedAccountDetails.as_view()
|
83 |
|
-
|