def retrieve(self, request, pk=None): try: obj = cacheCategoryOrFigur(request, 'figure', FigureField) queryset = obj.get(pk=pk) serializer = FigureFieldDetailSerializer(queryset) return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def list(self, request): try: obj = cacheCategoryOrFigur(request, 'figure', FigureField) serializer = FigureFieldSerializer(obj, context={'request': request}, many=True) return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def update(self, request, pk=None): try: obj = cacheCategoryOrFigur(request, 'figure', FigureField) figure = obj.get(pk=pk) serializer = FigureFieldDetailSerializer(figure, data=request.data) if serializer.is_valid() and request.user.is_superuser: serializer.save() return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def destroy(self, request, pk=None): """ delete figure field :param request: :param pk: :return: """ try: obj = cacheCategoryOrFigur(request, 'figure', FigureField) figure = obj.get(pk=pk) figure.delete() return response.SuccessResponse(message='Deleted object').send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def destroy(self, request, slug=None): """ delete object :param request: :param slug: :return: """ try: obj = cacheCategoryOrFigur(request, 'category', Category) category = obj.filter(slug=slug).first() print(category) category.delete() return response.SuccessResponse(message='Delete object').send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def list(self, request): """ list all category for all user :param request: :return: """ try: obj = cacheCategoryOrFigur(request, 'category', Category) category = obj.filter(status=True) serializer = CategoryListSerializer(category, context={'request': request}, many=True) return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def retrieve(self, request, slug=None): """ detail category :param request: :param slug: :return: """ try: obj = cacheCategoryOrFigur(request, 'category', Category) queryset = obj.filter(slug=slug) serializer = CategoryDetailSerializer(queryset, context={'request': request}, many=True) return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def update(self, request, slug=None): """ update category :param request: :param slug: :return: """ try: obj = cacheCategoryOrFigur(request, 'category', Category) category = obj.get(slug=slug) serializer = CategoryDetailSerializer(category, context={'request': request}, data=request.data) if serializer.is_valid() and request.user.is_superuser: serializer.save() return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()
def product_category(self, request, slug=None): """ all products that in this category for example user click to mobile category and shows to user all products that be mobile category :param request: :param slug: :return: """ try: obj_cat = cacheCategoryOrFigur(request, 'category', Category) obj_pro = productCacheDatabase(request, 'products', Product) queryset = obj_cat.get(slug=slug, status=True) products = obj_pro.filter(category=queryset) serializer = ProductSerializer(products, context={'request': request}, many=True) return response.SuccessResponse(serializer.data).send() except CustomException as e: return response.ErrorResponse(message=e.detail, status=e.status_code).send()