Пример #1
0
def create_post_view(request):
    account = request.user

    post = Post(account=account)

    if request.method == "POST":
        serializer = PostSerializer(post, data=request.data)
        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)
Пример #2
0
def post_update_api(request, pk):
    """
    Post Update/Edit API.
    """
    try:
        post = Post.objects.get(pk=pk)
        
    except Post.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = PostSerializer(post, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)
Пример #3
0
def get_post_view(request, id):
    try:
        post = Post.objects.get(id=id)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = PostSerializer(post)
        return Response(serializer.data)
Пример #4
0
def update_post_view(request, id):
    try:
        post = Post.objects.get(id=id)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    account = request.user
    if post.account != account:
        return Response({'response': "You don't have permission to edit"})

    if request.method == "PUT":
        serializer = PostSerializer(post, data=request.data)
        data = {}

        if serializer.is_valid():
            serializer.save()
            data["success"] = "updated successful"
            return Responsed(data=data)
        return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
class CategorySerializer(serializers.ModelSerializer):
    creator = serializers.SerializerMethodField('get_creator_username')
    posts = PostSerializer(many=True)

    class Meta:
        model = Category
        fields = ['tag', 'creator', 'posts']

    def get_creator_username(self, category):
        creator = category.creator.username
        return creator
Пример #6
0
def post_detailed_api(request, pk):
    """
    Post Detailed View API.
    """
    try:
        post = Post.objects.get(pk=pk)

    except Post.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = PostSerializer(post)
        return JsonResponse(serializer.data)
Пример #7
0
def api_detail_view(request, post_id):
    try:
        post = Post.objects.get(id=post_id)  #query single post with id
    except:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':  #no need of if statement because this is a GET request, for a double check using if statement
        #<----------- To get all liked users ----------------->
        total_liked_users = post.Likes.all(
        )  #This will give a queryset of USERS who liked this post
        contents = []
        for content in total_liked_users:  #converting the queryset to list so that it can returned as JSON response
            contents.append(content)
        all_liked_users = []
        for i in range(0, len(contents)):
            all_liked_users.append(str(contents[i]))  #converting to string
    # <--------------------------------------------------->

    # <---------- To get likes and unlikes number -------->
        if request.method == 'GET':  #no need of if statement because this is a GET request, for a double check using if statement
            user = request.user
            current_user = str(user)  #For getting current users

            liked = False
            if user in post.Likes.all():
                liked = True
            else:
                liked = False
            like = post.total_likes()
            unlike = post.total_unlikes()
            # <---------------------------------------------------->

            serialzer = PostSerializer(post)

            return Response(({
                'current_user': current_user
            }, serialzer.data, {
                'total likes': like
            }, {
                'total unlikes': unlike
            }, {
                'user liked': liked
            }, {
                'all liked users': all_liked_users
            }))  #This will give a Json response


# <-------------------------------------------------------------------------->
Пример #8
0
def post_list_api(request):
    """
    List all pot view API.
    """
    if request.method == 'GET':
        post_list = Post.objects.all()
        serializer = PostSerializer(post_list, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = PostSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)