diff --git a/api/handlers/v2.py b/api/handlers/v2.py index 26a654a..a1d9009 100644 --- a/api/handlers/v2.py +++ b/api/handlers/v2.py @@ -3,7 +3,7 @@ from django.contrib.auth.models import User from piston.handler import BaseHandler from piston.utils import rc -from eve_api.models import EVEAccount +from eve_api.models import EVEAccount, EVEPlayerCharacter from eve_proxy.models import CachedDocument @@ -32,7 +32,7 @@ class V2AuthenticationHandler(BaseHandler): return {'userid': user.id, 'username': user.username, 'email': user.email, - 'groups': user.groups.all().values_list('id', 'name'), + 'groups': user.groups.all().values('id', 'name'), 'staff': user.is_staff, 'superuser': user.is_superuser} @@ -42,9 +42,20 @@ class V2AuthenticationHandler(BaseHandler): class V2EveAPIProxyHandler(BaseHandler): + """ + Provides a proxy handler to the EVE API using 'eve_proxy' + """ + allowed_methods = ('GET') def read(self, request): + """ + Acts as a EVE API proxy, all params are passed to the + API, for the exception of 'apikey' which is dervived from the + stored information and 'userid' + + 'apikey' field should be populated with the Auth API key. + """ url_path = request.META['PATH_INFO'].url_path.replace(reverse('v2-api-eveapiproxy'), "/") params = {} @@ -64,3 +75,33 @@ class V2EveAPIProxyHandler(BaseHandler): return HttpResponse(status=500) else: return HttpResponse(cached_doc.body) + + +class V2UserHandler(BaseHandler): + allowed_methods = ('GET') + + def read(self, request, userid): + try: + u = User.objects.get(id=userid) + except (User.DoesNotExist, ValueError): + resp = rc.NOT_FOUND + + charlist = [] + for acc in u.eveaccount_set.all(): + for char in acc.characters.all().select_related('characters').values('id', 'name', 'corporation', 'corporation_date', 'corporation__name'): + d = dict(char) + d['eveaccount'] = acc.id + charlist.append(d) + + d = {'id': u.id, + 'username': u.username, + 'email': u.email, + 'eveaccounts': u.eveaccount_set.all().values('api_user_id', 'description', 'api_status', 'api_keytype'), + 'serviceaccounts': u.serviceaccount_set.all().values('service', 'service__name', 'service__api', 'service_uid'), + 'characters': charlist, + 'groups': u.groups.all().values('id', 'name'), + 'staff': u.is_staff, + 'superuser': u.is_superuser} + + return d + diff --git a/api/urls.py b/api/urls.py index 8df5401..a883523 100755 --- a/api/urls.py +++ b/api/urls.py @@ -30,9 +30,10 @@ urlpatterns = patterns('', # v2 APIs v2_authenticate_resource = Resource(handler=V2AuthenticationHandler, **noauth) v2_eveapiproxy_resource = Resource(handler=V2EveAPIProxyHandler, **apikeyauth) +v2_user_resource = Resource(handler=V2UserHandler, **apikeyauth) urlpatterns += patterns('', url(r'^v2/authenticate/$', v2_authenticate_resource), - url(r'^v2/proxy/', eveapiproxy_resource, name='v2-api-eveapiproxy'), - + url(r'^v2/proxy/', v2_eveapiproxy_resource, name='v2-api-eveapiproxy'), + url(r'^v2/user/(?P\d+)/$', v2_user_resource), )