Example #1
0
 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)
Example #2
0
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)
Example #3
0
 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)
Example #4
0
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)
Example #5
0
 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)
Example #6
0
 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)
Example #7
0
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)
Example #8
0
 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)
Example #9
0
	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)
Example #10
0
 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)
Example #11
0
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)
Example #12
0
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)
Example #13
0
 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)