Add a user_info view returning JSON and JSONP (fixes #7657)

This commit is contained in:
Benjamin Dauvergne 2015-06-22 23:41:23 +02:00
parent 6ea51c1914
commit d92c9cfc82
2 changed files with 33 additions and 8 deletions

View File

@ -51,4 +51,5 @@ urlpatterns = patterns('',
url('^manage/collectivities/(?P<collectivity_pk>\d+)/', include(collectivity_urlpatterns)),
url('^$', views.agent_homepage, name='auth_homepage'),
url('^services.json$', views.agent_homepage_jsonp, name='auth_homepage_jsonp'),
url('^user_info/$', views.user_info),
)

View File

@ -17,6 +17,7 @@ from authentic2.manager.views import AjaxFormViewMixin, \
ActionMixin, OtherActionsMixin, TitleMixin, Action
from authentic2.manager.user_views import UserEditView, UserAddView
from authentic2.utils import make_url
from authentic2.attributes_ng.engine import get_attributes
from . import models, tables, forms, constants
@ -407,6 +408,36 @@ def agent_homepage(request):
ctx = {'service_links': get_service_links(request)}
return render(request, 'authentic2_pratic/agent_homepage.html', ctx)
def return_json(request, data):
response = HttpResponse(content_type='application/json')
json_str = json.dumps(data)
for variable in ('jsonpCallback', 'callback'):
if variable in request.GET:
json_str = '%s(%s);' % (request.GET[variable], json_str)
break
response.write(json_str)
return response
def flatten(o):
if isinstance(o, dict):
return {k: flatten(v) for k, v in o.iteritems()}
elif isinstance(o, (tuple, list, set)):
return o.__class__(flatten(v) for v in o)
elif isinstance(o, (bool, unicode, int, long)):
return o
else:
return unicode(o)
@login_required
def user_info(request):
attributes = get_attributes({'user': request.user})
return return_json(request, flatten(attributes))
@login_required
def agent_homepage_jsonp(request):
service_links = get_service_links(request)
data = []
@ -417,11 +448,4 @@ def agent_homepage_jsonp(request):
'authentication_level_is_enough': not bool(needed_authent),
'authentication_levels': filter(None, needed_authent.split(', ')),
})
response = HttpResponse(content_type='application/json')
json_str = json.dumps(data)
for variable in ('jsonpCallback', 'callback'):
if variable in request.GET:
json_str = '%s(%s);' % (request.GET[variable], json_str)
break
response.write(json_str)
return response
return return_json(request, data)