0001-fix-pep8-errors-and-remove-unused-imports-13294.patch
mandayejs/applications.py | ||
---|---|---|
20 | 20 |
from importlib import import_module |
21 | 21 | |
22 | 22 |
from django.conf import settings |
23 |
from django.conf.urls import patterns, include, url
|
|
23 |
from django.conf.urls import patterns, url |
|
24 | 24 |
from django.http import Http404 |
25 | 25 |
from django.core.exceptions import ImproperlyConfigured |
26 | 26 |
from django.core.urlresolvers import resolve |
27 | 27 | |
28 | 28 | |
29 | 29 |
def get_app_settings(): |
30 |
module_name,app_settings = tuple(settings.SITE_APP.rsplit('.',1))
|
|
30 |
module_name, app_settings = tuple(settings.SITE_APP.rsplit('.', 1))
|
|
31 | 31 |
module = import_module(module_name) |
32 | 32 |
return getattr(module, app_settings) |
33 | 33 | |
34 | ||
34 | 35 |
def app_web_services(request, path): |
35 | 36 |
app = get_app_settings() |
36 | 37 |
if hasattr(app, 'urlpatterns'): |
37 |
view, args, kwargs = resolve(request.path, urlconf= app)
|
|
38 |
view, args, kwargs = resolve(request.path, urlconf=app) |
|
38 | 39 |
return view(request, *args, **kwargs) |
39 | 40 |
raise Http404 |
40 | 41 | |
... | ... | |
51 | 52 | |
52 | 53 |
def __new__(cls, name, bases, dct): |
53 | 54 |
if name != 'AppSettings': |
54 |
if AppSettings in bases: # Avoid checking keys for inherited classes |
|
55 |
if AppSettings in bases: # Avoid checking keys for inherited classes
|
|
55 | 56 | |
56 | 57 |
missing_key = set(cls.REQUIRED_KEYS).difference(set(dct.keys())) or None |
57 | 58 |
if missing_key: |
58 | 59 |
raise ImproperlyConfigured('{} is mandatory'.format(missing_key.pop())) |
59 | 60 | |
60 |
if not set(('SITE_FORCE_REDIRECT_URL','SITE_FORCE_REDIRECT_LOCATOR'))\ |
|
61 |
.intersection(set(dct.keys())): |
|
62 |
raise ImproperlyConfigured('one of these settings ({}) must be defined' |
|
63 |
.format(('SITE_FORCE_REDIRECT_URL','SITE_FORCE_REDIRECT_LOCATOR'))) |
|
61 |
if not set(('SITE_FORCE_REDIRECT_URL', 'SITE_FORCE_REDIRECT_LOCATOR')).intersection(set(dct.keys())): |
|
62 |
raise ImproperlyConfigured( |
|
63 |
'one of these settings ({}) must be defined' |
|
64 |
.format(('SITE_FORCE_REDIRECT_URL', 'SITE_FORCE_REDIRECT_LOCATOR'))) |
|
65 | ||
64 | 66 |
# Script asserting phantomjs authentication |
65 |
dct['SITE_AUTH_CHECKER'] = os.path.join(settings.STATIC_ROOT, |
|
66 |
dct['SITE_AUTH_CHECKER']) |
|
67 |
dct['SITE_AUTH_CHECKER'] = os.path.join(settings.STATIC_ROOT, dct['SITE_AUTH_CHECKER'])
|
|
68 | ||
67 | 69 |
# Default form submit element |
68 | 70 |
if not dct.get('SITE_FORM_SUBMIT_ELEMENT', None): |
69 | 71 |
dct['SITE_FORM_SUBMIT_ELEMENT'] = 'input[type=submit], button' |
... | ... | |
135 | 137 |
] |
136 | 138 |
SITE_APP_SCRIPTS = [ |
137 | 139 |
'duonet/js/duonet.js', |
138 |
]
|
|
140 |
] |
|
139 | 141 | |
140 | 142 |
SITE_FORCE_REDIRECT_URL = '/Connect.aspx' |
141 | 143 | |
... | ... | |
180 | 182 |
'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount', |
181 | 183 |
} |
182 | 184 | |
183 |
urlpatterns = patterns('', |
|
185 |
urlpatterns = patterns( |
|
186 |
'', |
|
184 | 187 |
url( |
185 | 188 |
r'account/(?P<username>[\w+]*)/$', |
186 | 189 |
'mandayejs.views.archimed_account_details', |
... | ... | |
228 | 231 | |
229 | 232 | |
230 | 233 |
class Imuse(AppSettings): |
231 |
SITE_LOGIN_PATH = 'extranet/login/gen_index_groupe.php?nav=autre'
|
|
234 |
SITE_LOGIN_PATH = 'extranet/login/gen_index_groupe.php?nav=autre' |
|
232 | 235 | |
233 | 236 |
SITE_LOCATORS = [ |
234 | 237 |
{ |
mandayejs/mandaye/api.py | ||
---|---|---|
22 | 22 |
from rest_framework import status |
23 | 23 |
from rest_framework.views import APIView |
24 | 24 |
from rest_framework.response import Response |
25 |
from rest_framework.permissions import IsAuthenticated |
|
26 | 25 | |
27 | 26 |
from mandayejs.applications import get_app_settings |
28 | 27 |
from mandayejs.mandaye.models import UserCredentials |
... | ... | |
38 | 37 |
def get(self, request, *args, **kwargs): |
39 | 38 |
app_settings = get_app_settings() |
40 | 39 |
locators = { |
41 |
locator.get('name') : '' \
|
|
40 |
locator.get('name'): ''
|
|
42 | 41 |
for locator in app_settings.SITE_LOCATORS |
43 | 42 |
} |
44 | 43 |
return Response(locators) |
... | ... | |
47 | 46 |
data = request.data |
48 | 47 | |
49 | 48 |
user, created = User.objects.get_or_create( |
50 |
username= data.get('name_id_content'),
|
|
51 |
email= data.get('email'),
|
|
52 |
first_name= data.get('first_name'),
|
|
53 |
last_name= data.get('last_name')
|
|
49 |
username=data.get('name_id_content'), |
|
50 |
email=data.get('email'), |
|
51 |
first_name=data.get('first_name'), |
|
52 |
last_name=data.get('last_name') |
|
54 | 53 |
) |
55 | 54 | |
56 | 55 |
uuid, created = UserSAMLIdentifier.objects.get_or_create( |
57 |
user_id= user.id,
|
|
58 |
name_id= data.get('name_id_content'),
|
|
59 |
issuer= get_idp()
|
|
56 |
user_id=user.id, |
|
57 |
name_id=data.get('name_id_content'), |
|
58 |
issuer=get_idp() |
|
60 | 59 |
) |
61 | 60 | |
62 | 61 |
cred = UserCredentials(user=user, locators=data.get('locators')) |
... | ... | |
68 | 67 |
return Response(status=status.HTTP_401_UNAUTHORIZED) |
69 | 68 | |
70 | 69 |
cred.save() |
71 |
return Response(status=status.HTTP_200_OK)
|
|
70 |
return Response(status=status.HTTP_200_OK) |
|
72 | 71 | |
73 | 72 |
def delete(self, request, *args, **kwargs): |
74 | 73 |
data = request.data |
mandayejs/mandaye/forms.py | ||
---|---|---|
16 | 16 |
from __future__ import absolute_import |
17 | 17 | |
18 | 18 |
from django import forms |
19 |
from django.conf import settings |
|
20 | 19 | |
21 | 20 |
from mandayejs.applications import get_app_settings |
22 |
from mandayejs.mandaye.models import UserCredentials |
|
23 | 21 | |
24 | 22 |
app_settings = get_app_settings() |
25 | 23 | |
24 | ||
26 | 25 |
class FormFactory(forms.Form): |
27 |
def __init__(self, *args, **kwargs):
|
|
26 |
def __init__(self, *args, **kwargs): |
|
28 | 27 |
super(FormFactory, self).__init__(*args, **kwargs) |
29 | 28 |
fields = getattr(app_settings, 'SITE_LOCATORS', []) |
30 |
if fields :
|
|
31 |
for field in fields :
|
|
29 |
if fields:
|
|
30 |
for field in fields: |
|
32 | 31 |
if field['kind'] == 'string': |
33 | 32 |
self.fields[field['name']] = forms.CharField( |
34 |
label=field['label'],
|
|
35 |
max_length=32,
|
|
36 |
help_text=field['help']
|
|
33 |
label=field['label'],
|
|
34 |
max_length=32,
|
|
35 |
help_text=field['help'] |
|
37 | 36 |
) |
38 | 37 |
elif field['kind'] == 'password': |
39 | 38 |
self.fields[field['name']] = forms.CharField( |
40 |
label=field['label'],
|
|
41 |
widget=forms.PasswordInput(),
|
|
42 |
help_text=field['help']
|
|
39 |
label=field['label'],
|
|
40 |
widget=forms.PasswordInput(),
|
|
41 |
help_text=field['help'] |
|
43 | 42 |
) |
44 | 43 |
else: |
45 | 44 |
self.fields[field['name']] = forms.DateField( |
46 |
label=field['label'],
|
|
47 |
help_text=field['help']
|
|
45 |
label=field['label'],
|
|
46 |
help_text=field['help'] |
|
48 | 47 |
) |
49 |
mandayejs/mandaye/models.py | ||
---|---|---|
83 | 83 |
def to_login_info(self, decrypt=False): |
84 | 84 |
if decrypt: |
85 | 85 |
self.decrypt() |
86 |
return {'#'+k : v for k,v in self.locators.items() } |
|
86 |
return {'#' + k: v for k, v in self.locators.items()} |
mandayejs/mandaye/utils.py | ||
---|---|---|
22 | 22 | |
23 | 23 |
from mandayejs.applications import get_app_settings |
24 | 24 | |
25 | ||
25 | 26 |
def exec_phantom(data, script='do_login.js'): |
26 |
phantom = subprocess.Popen([settings.PHANTOM_JS_BINARY,
|
|
27 |
'--ignore-ssl-errors=yes',
|
|
28 |
'--ssl-protocol=any', |
|
27 |
phantom = subprocess.Popen([ |
|
28 |
settings.PHANTOM_JS_BINARY,
|
|
29 |
'--ignore-ssl-errors=yes', '--ssl-protocol=any',
|
|
29 | 30 |
os.path.join(settings.BASE_DIR, 'mandayejs', script)], |
30 | 31 |
close_fds=True, |
31 | 32 |
stdin=subprocess.PIPE, |
... | ... | |
39 | 40 |
phantom.terminate() |
40 | 41 |
return result |
41 | 42 | |
43 | ||
42 | 44 |
def cookie_builder(headers): |
43 | 45 |
"""Build Cookies from list of headers |
44 | 46 |
""" |
... | ... | |
48 | 50 | |
49 | 51 |
return cookie |
50 | 52 | |
53 | ||
51 | 54 |
def get_logout_info(request): |
52 | 55 |
"""Returns phantomjs logout prerequis |
53 | 56 |
""" |
... | ... | |
65 | 68 |
'value': value.value, |
66 | 69 |
'domain': domain, |
67 | 70 |
'path': '/' |
68 |
} for key, value in cookies.items() ]
|
|
71 |
} for key, value in cookies.items()] |
|
69 | 72 | |
70 | 73 |
return data |
71 | 74 | |
75 | ||
72 | 76 |
def get_password_field(): |
73 | 77 |
"""Return name of the password field |
74 | 78 |
""" |
75 | 79 |
app_settings = get_app_settings() |
76 | 80 |
try: |
77 |
field_name = [ field.get('name') for field in app_settings.SITE_LOCATORS if field.get('kind') == 'password' ] |
|
81 |
field_name = [field.get('name') for field in app_settings.SITE_LOCATORS |
|
82 |
if field.get('kind') == 'password'] |
|
78 | 83 |
return field_name[0] |
79 | 84 |
except (IndexError,): |
80 | 85 |
return None |
81 | 86 | |
87 | ||
82 | 88 |
def get_login_info(request, credentials): |
83 | 89 |
"""Returns phantomjs login prerequis |
84 | 90 |
""" |
... | ... | |
87 | 93 |
return { |
88 | 94 |
'address': request.build_absolute_uri(app_settings.SITE_LOGIN_PATH), |
89 | 95 |
'cookies': [], |
90 |
'locators': [ credentials.to_login_info() ],
|
|
96 |
'locators': [credentials.to_login_info()],
|
|
91 | 97 |
'auth_checker': app_settings.SITE_AUTH_CHECKER, |
92 | 98 |
'form_submit_element': app_settings.SITE_FORM_SUBMIT_ELEMENT |
93 | 99 |
} |
94 | 100 | |
101 | ||
95 | 102 |
def get_idp(): |
96 | 103 |
"""Return idp |
97 | 104 |
""" |
mandayejs/mandaye/views.py | ||
---|---|---|
18 | 18 | |
19 | 19 |
import logging |
20 | 20 | |
21 |
from django.conf import settings |
|
22 |
from django.contrib.auth import views as auth_views |
|
23 |
from django.contrib.auth import logout as auth_logout |
|
24 | 21 |
from django.contrib.auth.models import User |
25 | 22 |
from django.contrib.auth.decorators import login_required |
26 | 23 |
from django.contrib import messages |
27 |
from django.forms import PasswordInput |
|
28 |
from django.forms import models as model_forms |
|
29 | 24 |
from django.http import HttpResponseRedirect, HttpResponse |
30 |
from django.shortcuts import get_object_or_404, render, resolve_url
|
|
25 |
from django.shortcuts import render, resolve_url |
|
31 | 26 |
from django.template import RequestContext |
32 | 27 |
from django.views.generic.base import TemplateView |
33 | 28 |
from django.views.decorators.csrf import csrf_exempt |
34 |
from django.db import IntegrityError |
|
35 | 29 |
from django.utils.translation import ugettext_lazy as _ |
36 |
from django.template import RequestContext, Template
|
|
30 |
from django.template import Template |
|
37 | 31 | |
38 | 32 |
from .models import UserCredentials |
39 | 33 |
from mandayejs.mandaye.forms import FormFactory |
40 | 34 |
from mandayejs.mandaye.utils import exec_phantom, cookie_builder,\ |
41 |
get_login_info, get_logout_info
|
|
35 |
get_login_info, get_logout_info |
|
42 | 36 |
from mandayejs.applications import get_app_settings |
43 | 37 | |
44 | 38 |
from mellon.views import logout as mellon_logout |
45 | 39 | |
46 |
import requests |
|
47 | 40 | |
48 | 41 |
app_settings = get_app_settings() |
49 | 42 | |
50 | 43 |
logger = logging.getLogger(__name__) |
51 | 44 | |
45 | ||
52 | 46 |
def logout(request, *args, **kwargs): |
53 | 47 |
logger.debug("running slo") |
54 | 48 |
data = get_logout_info(request) |
... | ... | |
72 | 66 |
context = super(Panel, self).get_context_data(**kwargs) |
73 | 67 |
scripts = getattr(app_settings, 'SITE_APP_SCRIPTS', None) |
74 | 68 |
context['site_scripts'] = scripts |
75 |
context['force_redirect_url'] = getattr(app_settings, |
|
76 |
'SITE_FORCE_REDIRECT_URL', '') |
|
77 |
context['force_redirect_locator'] = getattr(app_settings, |
|
78 |
'SITE_FORCE_REDIRECT_LOCATOR', '') |
|
79 |
context['logout_locator'] = getattr(app_settings, |
|
80 |
'SITE_LOGOUT_LOCATOR','') |
|
69 |
context['force_redirect_url'] = getattr(app_settings, 'SITE_FORCE_REDIRECT_URL', '') |
|
70 |
context['force_redirect_locator'] = getattr(app_settings, 'SITE_FORCE_REDIRECT_LOCATOR', '') |
|
71 |
context['logout_locator'] = getattr(app_settings, 'SITE_LOGOUT_LOCATOR', '') |
|
81 | 72 |
context['is_linked'] = self.is_account_linked() |
82 | 73 |
return context |
83 | 74 | |
... | ... | |
93 | 84 | |
94 | 85 |
panel = Panel.as_view() |
95 | 86 | |
87 | ||
96 | 88 |
@login_required |
97 | 89 |
def post_login(request, *args, **kwargs): |
98 | 90 |
try: |
99 | 91 |
user = User.objects.get(username=request.user.username) |
100 | 92 |
logger.debug(user) |
101 |
credentials = UserCredentials.objects.get( |
|
102 |
user=user) |
|
93 |
credentials = UserCredentials.objects.get(user=user) |
|
103 | 94 |
logger.debug(credentials) |
104 | 95 |
except (UserCredentials.DoesNotExist,): |
105 | 96 |
return HttpResponseRedirect(resolve_url('associate')) |
106 | 97 | |
107 | 98 |
return render(request, 'mandaye/post-login.html', {}) |
108 | 99 | |
100 | ||
109 | 101 |
@login_required |
110 | 102 |
@csrf_exempt |
111 | 103 |
def associate(request, *args, **kwargs): |
... | ... | |
119 | 111 |
credentials.save() |
120 | 112 | |
121 | 113 |
return HttpResponseRedirect(resolve_url('post-login')) |
122 |
else :
|
|
114 |
else: |
|
123 | 115 |
form = FormFactory() |
124 | 116 | |
125 | 117 |
response = render(request, 'mandaye/associate.html', { |
... | ... | |
131 | 123 |
@login_required |
132 | 124 |
def dissociate(request, *args, **kwargs): |
133 | 125 |
try: |
134 |
c_user = UserCredentials.objects.get( |
|
135 |
user__username=request.user.username) |
|
126 |
c_user = UserCredentials.objects.get(user__username=request.user.username) |
|
136 | 127 |
c_user.delete() |
137 | 128 |
logger.debug("{} dissacioted".format(c_user.user.username)) |
138 | 129 |
return HttpResponseRedirect( |
... | ... | |
142 | 133 |
except (UserCredentials.DoesNotExist,): |
143 | 134 |
return HttpResponseRedirect(resolve_url('associate')) |
144 | 135 | |
136 | ||
145 | 137 |
@login_required |
146 | 138 |
def post_login_do(request, *args, **kwargs): |
147 | 139 |
user = User.objects.get(username=request.user.username) |
... | ... | |
153 | 145 |
login_info = get_login_info(request, credentials) |
154 | 146 | |
155 | 147 |
logger.debug(login_info) |
156 |
login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
|
|
148 |
login_info['locators'] = [credentials.to_login_info(decrypt=True)] |
|
157 | 149 |
result = exec_phantom(login_info) |
158 | 150 |
logger.debug(result) |
159 | 151 | |
... | ... | |
172 | 164 |
window.top.location = "{{url}}";</script>') |
173 | 165 |
context = RequestContext(request, {'url': url}) |
174 | 166 |
response = HttpResponse(template.render(context)) |
175 |
if result.get('headers',None): |
|
167 |
if result.get('headers', None):
|
|
176 | 168 |
response.cookies = cookie_builder(result.get('headers')) |
177 | 169 | |
178 | 170 |
return response |
mandayejs/views.py | ||
---|---|---|
23 | 23 | |
24 | 24 |
from django.contrib.auth.models import User |
25 | 25 |
from django.shortcuts import get_object_or_404 |
26 |
from django.core.exceptions import ImproperlyConfigured |
|
27 | 26 | |
28 | 27 |
from rest_framework import status |
29 | 28 |
from rest_framework.views import APIView |
30 | 29 |
from rest_framework.response import Response |
31 | 30 | |
32 | 31 |
from mandayejs.mandaye.models import UserCredentials |
33 |
from mandayejs.mandaye.utils import exec_phantom, cookie_builder, get_login_info
|
|
32 |
from mandayejs.mandaye.utils import exec_phantom, get_login_info |
|
34 | 33 |
from mandayejs.applications import get_app_settings |
35 | 34 | |
36 | 35 | |
... | ... | |
41 | 40 |
def get(self, request, *args, **kwargs): |
42 | 41 |
logger = logging.getLogger(__name__) |
43 | 42 |
app_settings = get_app_settings() |
44 |
|
|
43 | ||
45 | 44 |
ws_uri = app_settings.SITE_WS_ENDPOINT['account_details'] |
46 | 45 | |
47 | 46 |
username = kwargs['username'] |
... | ... | |
50 | 49 | |
51 | 50 |
login_info = get_login_info(request, credentials) |
52 | 51 |
logger.debug(login_info) |
53 |
login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
|
|
52 |
login_info['locators'] = [credentials.to_login_info(decrypt=True)] |
|
54 | 53 |
result = exec_phantom(login_info) |
55 | 54 | |
56 | 55 |
if result.get('result') != 'ok': |
... | ... | |
74 | 73 |
} |
75 | 74 |
content = '{"codeConfig":"", "xslPath":"Services/LectorShortAccount.xslt"}' |
76 | 75 |
url = request.build_absolute_uri(ws_uri) |
77 |
logger.debug(url)
|
|
78 |
request_response = session.post(url, headers=headers, data=content, verify=False)
|
|
76 |
logger.debug(url) |
|
77 |
request_response = session.post(url, headers=headers, data=content, verify=False) |
|
79 | 78 |
data = request_response.json() |
80 | 79 |
logger.debug(pprint(data)) |
81 | 80 |
return Response(data) |
82 | 81 | |
83 | ||
84 | 82 |
archimed_account_details = ArchimedAccountDetails.as_view() |
85 |
- |