Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def test_post_logged_in(self):
- product = Product.objects.get(id=1)
- self.client.login(username='test', password='test123')
- data = {
- 'nick': self.user.id,
- 'rate': '1/5',
- 'content': 'here is comment',
- 'product': product.id
- }
- response = self.client.post(reverse('add_comments', kwargs={'id': product.id}), data, format='json')
- self.assertEqual(response.status_code, 201, f'expected Response code 201, instead get {response.status_code}')
- class CreateComment(APIView):
- def get_object(self, id):
- try:
- return Product.objects.get(id=id)
- except Product.DoesNotExist:
- raise Http404
- def get(self,request, id):
- product = self.get_object(id)
- serializer = ProductSerializer(product)
- return Response(serializer.data)
- def post(self, request,id):
- serializer = CommentSerializer(data=request.data)
- if serializer.is_valid():
- serializer.save(nick=request.user, product=self.get_object(id))
- return Response(serializer.data)
- return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement