from django.core.urlresolvers import reverse from django.http import HttpResponse, HttpResponseNotFound, HttpResponseServerError from eve_proxy.models import CachedDocument def retrieve_xml(request): """ A view that forwards EVE API requests through the cache system, either retrieving a cached document or querying and caching as needed. """ # This is the URL path (minus the parameters). url_path = request.META['PATH_INFO'].replace(reverse('eve_proxy.views.retrieve_xml'),"/") # The parameters attached to the end of the URL path. if request.method == 'POST': p = request.POST else: p = request.GET # Convert the QuerySet object into a dict params = {} for key,value in p.items(): if value.isdigits(): value = long(value) params[key] = value if url_path == '/' or url_path == '': # If they don't provide any kind of query, shoot a quick error message. return HttpResponseNotFound('No API query specified.') if 'userID' in params and not 'service' in params: return HttpResponse('No Service ID provided.') try: cached_doc = CachedDocument.objects.api_query(url_path, params, exceptions=False) except: return HttpResponseServerError('Error occured') if cached_doc: return HttpResponse(cached_doc.body, mimetype='text/xml') return HttpResponseNotFound('Error retrieving the document')