def post(self): serializer = SqlSerializer(data=self.data) serializer.is_valid(raise_exception=True) try: response = Response(serializer.execute()) self.write_response(response) except SqlError as e: self.set_status(400) self.write_response(Response({'error': e.detail.string}))
def group(self, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) x_column = self.get_argument('_x_column') x_lookup_name = self.get_argument('_x_lookup', None) y_func = self.get_argument('_y_func').lower() y_column = self.get_argument('_y_column', self.lookup_field) model_serializer = self.get_serializer() # x_serializers = list(filter(lambda x: x.field_name == x_column, model_serializer.fields)) # x_serializer = x_serializers[0] # y_serializers = list(filter(lambda x: x.field_name == y_column, model_serializer.fields)) # y_serializer = y_serializers[0] filter_instance = ModelGroupFilter() filter_instance.model = self.model queryset = filter_instance.filter(queryset, { 'x_column': x_column, 'x_lookup': x_lookup_name, 'y_func': y_func, 'y_column': y_column }) serializer = ModelGroupSerializer( instance=queryset, many=True, # TODO: Refactor serializer # group_serializer=x_serializer, # y_func_serializer=y_serializer ) self.write_response(Response(serializer.representation_data))
def get(self, *args, **kwargs): try: session = Session() token, created = register_token(session) if not token: return if is_token_activated(session): response = Response( {'message': 'Project token is already activated'}) self.write_response(response) return if settings.WEB_BASE_URL.startswith( 'https' ) and not self.request.full_url().startswith('https'): web_base_url = 'http{}'.format(settings.WEB_BASE_URL[5:]) else: web_base_url = settings.WEB_BASE_URL url = '{}/projects/register/{}'.format(web_base_url, token.token) query_string = 'referrer={}'.format( quote(self.request.full_url().encode('utf8'))) self.redirect('%s?%s' % (url, query_string)) finally: session.close()
def post(self): # TODO: Move to serializer file = self.request.files.get('file', [])[0] path = self.get_body_argument('path') filename = self.get_body_argument('filename', file.filename) upload_path = os.path.join(settings.MEDIA_ROOT, path, filename) upload_path = self.get_available_name(upload_path) relative_upload_path = upload_path[len(settings.MEDIA_ROOT) + 1:] # TODO: Add validations if not os.path.exists(os.path.dirname(upload_path)): try: os.makedirs(os.path.dirname(upload_path)) except OSError: raise with open(upload_path, 'wb') as f: f.write(file['body']) response = Response({ 'uploaded_path': relative_upload_path, 'uploaded_url': self.build_absolute_uri('/media/{}'.format(relative_upload_path)) }) self.write_response(response)
def create(self, *args, **kwargs): serializer = self.get_serializer(data=self.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) self.write_response( Response(serializer.representation_data, status=status.HTTP_201_CREATED))
def reset_order(self, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) ResetOrderSerializer = get_reset_order_serializer(self.get_model(), queryset, self.session) serializer = ResetOrderSerializer(data=self.data) serializer.is_valid(raise_exception=True) serializer.save() self.write_response(Response(serializer.representation_data))
def get(self): media_url_base = settings.MEDIA_BASE_URL + '{}' if settings.MEDIA_BASE_URL \ else self.build_absolute_uri('/media/{}') response = Response({ 'version': VERSION, 'type': 'jet_bridge', 'media_url_template': media_url_base }) self.write_response(response)
def get_paginated_response(self, data): return Response( OrderedDict([ ('count', self.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data), ('num_pages', self.get_pages_count()), ('per_page', self.page_size), ]))
def get(self): response = Response({ 'version': VERSION, 'type': 'jet_bridge', 'media_url_template': self.build_absolute_uri('/media/{}') }) self.write_response(response)
def get_siblings(self, *args, **kwargs): lookup_url_kwarg = self.lookup_url_kwarg or 'pk' assert lookup_url_kwarg in self.path_kwargs model_field = getattr(self.get_model(), self.lookup_field) obj = self.get_queryset().filter(getattr(model_field, '__eq__')(self.path_kwargs[lookup_url_kwarg])).first() self.check_object_permissions(obj) queryset = self.filter_queryset(self.get_queryset()) self.write_response(Response(get_model_siblings(self.model, obj, queryset)))
def list(self, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(instance=page, many=True) response = self.get_paginated_response(serializer.representation_data) self.write_response(response) return serializer = self.get_serializer(instance=queryset, many=True) response = Response(serializer.representation_data) self.write_response(response)
def aggregate(self, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) y_func = self.get_argument('_y_func').lower() y_column = self.get_argument('_y_column', self.lookup_field) model_serializer = self.get_serializer() y_serializers = list(filter(lambda x: x.field_name == y_column, model_serializer.fields)) y_serializer = y_serializers[0] filter_instance = ModelAggregateFilter() filter_instance.model = self.model queryset = filter_instance.filter(queryset, { 'y_func': y_func, 'y_column': y_column }) result = y_serializer.to_representation(queryset[0]) # TODO: Refactor serializer self.write_response(Response({ 'y_func': result }))
def retrieve(self, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance=instance) self.write_response(Response(serializer.representation_data))
def get(self, *args, **kwargs): queryset = self.get_queryset() serializer = self.serializer_class(instance=queryset, many=True) response = Response(serializer.representation_data) self.write_response(response)
def post(self): serializer = SqlSerializer(data=self.data) serializer.is_valid(raise_exception=True) response = Response(serializer.execute()) self.write_response(response)
def delete(self, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) self.write_response(Response(status=status.HTTP_204_NO_CONTENT))