def get(self, request, jurisdiction_id, id): base_qs = RoadEvent.objects.filter(jurisdiction__id=jurisdiction_id) if not can(request, 'view_internal'): base_qs = base_qs.filter(published=True) try: rdev = base_qs.get(id=id) except RoadEvent.DoesNotExist: raise Http404 return Resource(E.events(rdev.to_full_xml_element( accept_language=request.accept_language, remove_internal_elements=not can(request, 'view_internal') )))
def dispatch(self, request, *args, **kwargs): if request.method not in self.unauthenticated_methods: if not ( can(request, 'modify_data') and (request.method == 'DELETE' or request.META.get('CONTENT_TYPE').strip().startswith('application/json')) ): resp = HttpResponse("You need to be logged in to do that.", content_type='text/plain') resp.status_code = 401 return resp request.pretty_print = bool(request.GET.get('indent')) request.response_format = self.determine_response_format(request) request.response_version = self.determine_response_version(request) request.html_response = (request.response_format == 'text/html') if request.html_response: if request.COOKIES.get('open511_browser_format') == 'json': request.response_format = 'application/json' else: request.response_format = 'application/xml' request.pretty_print = True request.accept_language = self.determine_accept_language(request) try: result = super(APIView, self).dispatch(request, *args, **kwargs) except BadRequest as e: return HttpResponseBadRequest(unicode(e)) if isinstance(result, HttpResponse): return result self.remove_unselected_fields(request, result) if request.response_format == 'application/xml': resp = self.render_xml(request, result) elif request.response_format == 'application/json': resp = self.render_json(request, result) else: resp = HttpResponse('This API can only return data in XML or JSON.', status=406) if request.html_response: resp = self.render_api_browser(request, resp.content) # Set response headers if 'HTTP_ORIGIN' in request.META and request.method == 'GET': # Allow cross-domain requests resp['Access-Control-Allow-Origin'] = '*' if not resp.has_header('Expires'): resp['Expires'] = http_date(time.time()) patch_vary_headers(resp, ['Accept', 'Accept-Language', 'Open511-Version']) return resp
def get_qs(self, request, jurisdiction_id=None): qs = super(RoadEventListView, self).get_qs(request, jurisdiction_id) if not can(request, 'view_internal'): qs = qs.filter(published=True) if 'status' not in request.GET: # By default, show only active events qs = qs.filter(active=True) return qs
def object_to_xml(self, request, obj): return obj.to_full_xml_element( accept_language=request.accept_language, remove_internal_elements=not can(request, 'view_internal') )