def post(self, request, format=None): user = request.user serializer = PostSerializer(data=request.data, context={'user': user}) 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 updateUserPostLocation(request): if request.method == 'POST': longitude = request.POST.get('longitude') latitude = request.POST.get('latitude') city = request.POST.get('city') cityCode = request.POST.get('cityCode') address = request.POST.get('address') username = request.POST.get('username') tag = request.POST.get('username') try: post = Post( longitude=longitude, latitude=latitude, city=city, cityCode=cityCode, address=address, username=username, tag=tag, ) post.save() serializer = PostSerializer(post) redis = get_redis_connection() json_serializer = JSONRenderer().render(serializer.data) redis.hmset('Post',post.id, json_serializer) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self,request, format=None): user=request.user serializer = PostSerializer(data=request.data, context={'user':user}) 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 all_posts(request): """ Retorna um json com todos os post cadastrados """ response = { "status": "failure", } if request.method == 'GET': posts = Post.objects.all() serializer = PostSerializer(posts, many=True) response = { "status": "success", "shows": serializer.data, } return Response(response) elif request.method == 'POST': serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save() response = { "status": "success", "shows": serializer.data, } return Response(response, status=status.HTTP_201_CREATED) response = { "status": "failure", "errors": serializer.errors, } return Response(response, status=status.HTTP_400_BAD_REQUEST) return Response(response)
def put(self, request, pk): post = self.get_object(pk) serializer = PostSerializer(post, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def put(self, request, pk, format=None): post = self.get_object(pk) serializer = PostSerializer(post, 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, pk, format=None): serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.validated_data['created_by'] = request.user serializer.validated_data['category'] = Category.objects.get(pk=pk) serializer.validated_data['date_created'] = timezone.now() serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): username = request.COOKIES.get('username') user = User.objects.get(username=username) serializer = PostSerializer(data=request.data, context={'user':user}) print(serializer.is_valid()) if serializer.is_valid(): serializer.save() return Response(serializer.data) print(serializer.errors) return Response(serializer.errors)
def put(self, request, pk, format=None): try: post = Post.objects.get(pk=pk) if post.created_by == request.user: post_serializer = PostSerializer(post, data=request.data) if post_serializer.is_valid(): post_serializer.save() return Response(post_serializer.data) else: return Response(status.HTTP_400_BAD_REQUEST) else: return Response(status.HTTP_405_METHOD_NOT_ALLOWED) except Post.DoesNotExist: return Response(status.HTTP_404_NOT_FOUND)
def get_post(request, pk): """ Realiza as operações de select, update e delete no post dono da pk """ try: post = Post.objects.get(pk=pk) except Post.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = PostSerializer(post) return Response(serializer.data) elif request.method == 'PUT': serializer = PostSerializer(post, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': post.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)