def group(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) x_column = request.GET['_x_column'] x_lookup_name = request.GET.get('_x_lookup') y_func = request.GET['_y_func'].lower() y_column = request.GET.get('_y_column', 'id') x_field = self.model._meta.get_field(x_column) x_lookup = x_field.class_lookups.get(x_lookup_name) y_field = self.model._meta.get_field(y_column) if x_lookup: x_field = x_lookup('none').output_field x_serializer_class, x_serializer_kwargs = ModelSerializer().build_standard_field(x_column, x_field) x_serializer = x_serializer_class(**x_serializer_kwargs) y_serializer_class, y_serializer_kwargs = ModelSerializer().build_standard_field(y_column, y_field) y_serializer = y_serializer_class(**y_serializer_kwargs) queryset = GroupFilter().filter(queryset, { 'x_column': x_column, 'x_lookup': x_lookup, 'y_func': y_func, 'y_column': y_column }) serializer = self.get_serializer( queryset, many=True, group_serializer=x_serializer, y_func_serializer=y_serializer ) return Response(serializer.data)
def post(self, request, *args, **kwargs): serializer = FileUploadSerializer(data=request.data, context={'request': self.request}) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] token, created = Token.objects.get_or_create(user=user) return Response({'token': token.key})
def get(self, request, *args, **kwargs): unique_placeholder = '__FILE_PATH___' return Response({ 'version': VERSION, 'type': 'jet_django', 'media_url_template': request.build_absolute_uri(default_storage.url(unique_placeholder)).replace(unique_placeholder, '{}') })
def get_paginated_response(self, data): return Response(OrderedDict([ ('count', self.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data) ]))
def reset_order(self, request, *args, **kwargs): i = 1 for instance in build_queryset: setattr(instance, request.data.get('ordering_field'), i) instance.save() i += 1 return Response({})
def route(self, request): form = action(data=request.data) if not form.is_valid(): return Response(form.errors, status=status.HTTP_400_BAD_REQUEST) queryset = form.filer_queryset(self.get_queryset()) try: result = form.save(queryset) except Exception as e: return Response({NON_FIELD_ERRORS: str(e)}, status=status.HTTP_400_BAD_REQUEST) return Response({'action': form._meta.name, 'result': result})
def post(self, request, *args, **kwargs): serializer = SqlsSerializer(data=request.data) if 'queries' in request.data \ else SqlSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.execute(serializer.validated_data))
def exception_handler(exc, context): """ Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also Django's built-in `Http404` and `PermissionDenied` exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be raised. """ if isinstance(exc, Http404): exc = exceptions.NotFound() elif isinstance(exc, PermissionDenied): exc = exceptions.PermissionDenied() if isinstance(exc, exceptions.APIException): headers = {} if getattr(exc, 'auth_header', None): headers['WWW-Authenticate'] = exc.auth_header if getattr(exc, 'wait', None): headers['Retry-After'] = '%d' % exc.wait if isinstance(exc.detail, (list, dict)): data = exc.detail else: data = {'detail': exc.detail} set_rollback() return Response(data, status=exc.status_code, headers=headers) return None
def options(self, request, *args, **kwargs): """ Handler method for HTTP 'OPTIONS' request. """ if self.metadata_class is None: return self.http_method_not_allowed(request, *args, **kwargs) data = self.metadata_class().determine_metadata(request, self) return Response(data, status=status.HTTP_200_OK)
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data)
def post(self, request, *args, **kwargs): serializer = MessageSerializer(data=request.data) serializer.is_valid(raise_exception=True) result = serializer.save() if isinstance(result, HttpResponseBase): return result else: return Response(result)
def get_paginated_response(self, data): return Response( OrderedDict([ ('count', self.page.paginator.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data), ('num_pages', self.page.paginator.num_pages), ('per_page', self.page.paginator.per_page), ]))
def get_siblings(self, request, *args, **kwargs): lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]} obj = get_object_or_404(self.get_queryset(), **filter_kwargs) # May raise a permission denied self.check_object_permissions(self.request, obj) queryset = self.filter_queryset(self.get_queryset()) return Response(get_model_siblings(self.model, obj, queryset))
def jet_exception_handler(exc, context): response = exception_handler(exc, context) if not response: data = {'detail': 'Server Error'} set_rollback() response = Response(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) logger.exception('Jet view exception', exc_info=exc) traceback.print_exc() if context and 'request' in context and context[ 'request'].method == 'OPTIONS': response.status_code = 204 response['Access-Control-Max-Age'] = '1728000' response['Content-Type'] = 'text/plain; charset=utf-8' response['Content-Length'] = 0 return response
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return Response(serializer.data)
def get(self, request, *args, **kwargs): # Return a plain {"name": "hyperlink"} response. ret = OrderedDict() namespace = request.resolver_match.namespace for key, url_name in self.api_root_dict.items(): if namespace: url_name = namespace + ':' + url_name try: ret[key] = reverse( url_name, args=args, kwargs=kwargs, request=request, format=kwargs.get('format', None) ) except NoReverseMatch: # Don't bail out if eg. no list routes exist, only detail routes. continue return Response(ret)
def aggregate(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) y_func = request.GET['_y_func'].lower() y_column = request.GET.get('_y_column', 'id') y_field = self.model._meta.get_field(y_column) y_serializer_class, y_serializer_kwargs = ModelSerializer( ).build_standard_field(y_column, y_field) y_serializer = y_serializer_class(**y_serializer_kwargs) queryset = AggregateFilter().filter(queryset, { 'y_func': y_func, 'y_column': y_column }) serializer = self.get_serializer(queryset, y_func_serializer=y_serializer) return Response(serializer.data)
def post(self, request, *args, **kwargs): serializer = SqlSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.execute())
def get(self, request, *args, **kwargs): schema = self.schema_generator.get_schema(request, self.public) if schema is None: raise exceptions.PermissionDenied() return Response(schema)
def reorder(self, request, *args, **kwargs): serializer = ReorderSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def post(self, request, *args, **kwargs): serializer = MessageSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.save())
def destroy(self, request, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) return Response(status=status.HTTP_204_NO_CONTENT)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def get(self, request, *args, **kwargs): return Response(map(lambda x: x.serialize(), jet.models))
def get(self, request, *args, **kwargs): return Response({'version': VERSION})
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) return Response(serializer.data)