def put(self, request, pk, format=None): category = Category.objects.get(pk=pk) serializer = CategorySerializer(category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_category_serializer(self): data = {'name': 'food'} category_serializer = CategorySerializer(data=data) self.assertTrue(category_serializer.is_valid()) category = category_serializer.save() self.assertEqual(category.name, data['name'])
def put(self, request, pk): category = self.get_object(pk) serializer = CategorySerializer(category, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data serializer = CategorySerializer(data=data) if serializer.is_valid(): serializer.save() response = generateresponse('Success', 'Category', serializer.data) return Response(response) else: raise InvalidInformation(serializer.errors)
def get(self, request, category_id): try: category = Category.objects.get(id=category_id) except: raise ObjectDoesNotExist('Categories do not exist') serializer = CategorySerializer(category) response = generateresponse('Success', 'category', serializer.data) return Response(response)
def get(self, request): try: categories = Category.objects.all() except: raise ObjectDoesNotExist('Categories do not exist') serializer = CategorySerializer(categories, many=True) response = generateresponse('Success', 'Categories', serializer.data) return Response(response)
def categories(request, pk): """ **Get all the product_categories data- Ignore** > GET Returns the product_categories data. * Possible HTTP status codes and JSON response: * `HTTP_200_OK` - Returns the products data: { "id": Integer, "name": String, "description": String, "image": Url, "is_available": Boolean } * `HTTP_500_INTERNAL_SERVER_ERROR` - Internal server error :param pk: :param request: """ if Category.objects.filter(item_type_id=pk).exists( ): # Checks if product_category exists with given id. all_categories = Category.objects.filter(item_type_id=pk) else: return Response(messages.CATEGORY_ITEM_DOES_NOT_EXIST, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': category_serializer = CategorySerializer(all_categories, many=True) return Response(category_serializer.data[::-1], status=status.HTTP_200_OK)
def category_list(request): """ :GET get all main categories :POST add new category :param request: :return: Response obj """ if request.method == 'GET': categories = Categories.objects.all() serializer = CategorySerializer(categories, many=True) return Response(serializer.data, status=status.HTTP_200_OK) if request.method == 'POST' and 'user_name' in request.data.keys( ) and 'data' in request.data.keys(): user = Users.objects.get(user_name=request.data['user_name']) data = request.data['data'] if user and 'category_name' in data.keys(): serializer = CategorySerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(EMPTY_DICT, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): serializer = CategorySerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): snippets = Category.objects.all() serializer = CategorySerializer(snippets, many=True) return Response(serializer.data)
def index(request): categories = Category.objects.all() serializier = CategorySerializer(categories, many=True) return JsonResponse(serializier.data, safe=False)
def list(self, request): categorys = Category.objects.all() serializer = CategorySerializer(categorys, many=True) data = JSONRenderer().render(serializer.data) return Response(data)
def post(self, request): serialized = CategorySerializer(data=request.data) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=status.HTTP_201_CREATED) return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)