Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if 'fields' in params:
fields = params.pop('fields').split(",")
related, config = get_fields(self.serialize_config, fields=fields)
self.start_debug()
try:
obj = self.model.objects.prefetch_related(*related).get(pk=pk)
except ObjectDoesNotExist as e:
raise HttpError(404, "Error: {}".format(e))
serialized = serialize(obj, **config)
serialized['debug'] = self.get_debug()
response = Http200(serialized)
response['Access-Control-Allow-Origin'] = "*"
return response
def get(self, request):
return Http200(serialize(request.user, fields=self.user_fields))
def put(self, request, *args, **kwargs):
"""Update the object represented by this endpoint."""
if 'PUT' not in self.methods:
raise HttpError(405, 'Method Not Allowed')
Form = _get_form(self.form, self.model)
instance = self.get_instance(request, *args, **kwargs)
form = Form(request.data or None, request.FILES,
instance=instance)
if form.is_valid():
obj = form.save()
return Http200(self.serialize(obj))
raise HttpError(400, 'Invalid data', errors=form.errors)
self._parse_body(request)
authentication_required = self._process_authenticate(request)
if authentication_required:
return authentication_required
response = super(Endpoint, self).dispatch(request, *args, **kwargs)
except HttpError as err:
response = err.response
except Exception as ex:
if settings.DEBUG:
response = Http500(str(ex), traceback=traceback.format_exc())
else:
raise
if not isinstance(response, HttpResponse):
response = Http200(response)
return response
"total_count": count,
}, "results": [
serialize(x, **config) for x in data_page.object_list
]
}
if settings.DEBUG:
response['debug'] = self.get_debug()
response['debug'].update({
"prefetch_fields": list(related),
"page": page,
"sort": sort_by,
"field": fields,
})
response = Http200(response)
response['Access-Control-Allow-Origin'] = "*"
return response