Exemplo n.º 1
0
def get_post_by_time_interval(request):
    from_timestamp = request.data.get('from_timestamp')
    to_timestamp = request.data.get('to_timestamp')
    page_size = request.data.get('page_size')
    type = request.data.get('type')
    model = ModelName.POST
    if type == 'graph':
        model = ModelName.POST_GRAPH
    if request.user.is_authenticated:
        if from_timestamp is not None and from_timestamp != '' and to_timestamp != '' and to_timestamp is not None:
            from_timestamp = datetime.datetime.fromtimestamp(
                int(from_timestamp))
            to_timestamp = datetime.datetime.fromtimestamp(int(to_timestamp))

            top_community = Community.objects.filter(user=request.user).union(
                Community.objects.filter(community__state=True)).union(
                    Community.objects.filter(creator=request.user)).distinct()
            query = Post.objects.filter(
                user=request.user,
                timestamp__gte=from_timestamp,
                timestamp__lte=to_timestamp).union(
                    Post.objects.filter(
                        community__user=request.user,
                        timestamp__gte=from_timestamp,
                        timestamp__lte=to_timestamp)).union(
                            Post.objects.filter(
                                user__following=Profile.objects.filter(
                                    user=request.user).first(),
                                timestamp__gte=from_timestamp,
                                timestamp__lte=to_timestamp)).union(
                                    Post.objects.filter(
                                        community__state=True,
                                        timestamp__gte=from_timestamp,
                                        timestamp__lte=to_timestamp).distinct(
                                        )).distinct().order_by('-point')
            # query = Post.objects.filter(timestamp__gte=from_timestamp,
            #                             timestamp__lte=to_timestamp,
            #                             user=request.user)

            return get_paginated_queryset_response(query, request, page_size,
                                                   model)
        if (from_timestamp is None or to_timestamp is None
                or from_timestamp != ''
                or to_timestamp != '') and (from_timestamp is not None
                                            or to_timestamp is not None):
            return Response({Message.DETAIL: Message.SC_BAD_RQ}, status=400)
        query = Post.objects.filter(
            timestamp__gte=timestamp_in_the_past_by_day(30),
            timestamp__lte=timezone.now(),
            user=request.user)
        return get_paginated_queryset_response(query, request, page_size,
                                               model)
    return Response({Message.DETAIL: Message.SC_NO_AUTH}, status=401)
Exemplo n.º 2
0
def trending(request, days):
    page_size = request.data.get("page_size")
    if days:
        past = timestamp_in_the_past_by_day(days)
        post = Post.objects.filter(
            community__state=True,
            timestamp__gte=past,
            timestamp__lte=datetime.datetime.now()).annotate(
                user_count=Count("up_vote")).order_by('-user_count')
        return get_paginated_queryset_response(post, request, page_size,
                                               ModelName.POST)
    post = Post.objects.filter(community__state=True).annotate(
        user_count=Count("up_vote")).order_by('-user_count')
    return get_paginated_queryset_response(post, request, page_size,
                                           ModelName.POST)
Exemplo n.º 3
0
def find_post_by_username_up_vote(request, username):
    page_size = request.data.get("page_size")
    if request.user.is_authenticated:
        no_block = Post.objects.filter(up_vote__username=username,
                                       community__user=request.user)
        if no_block:
            return get_paginated_queryset_response(no_block, request,
                                                   page_size, ModelName.POST)
        return Response({Message.SC_NOT_FOUND}, status=400)
    post = Post.objects.filter(up_vote__username=username,
                               community__state=True)
    if post:
        return get_paginated_queryset_response(post, request, page_size,
                                               ModelName.POST)
    return Response({Message.SC_NOT_FOUND}, status=400)
Exemplo n.º 4
0
def find_post_by_user(request):
    page_size = request.data.get("page_size")
    if request.user.is_authenticated:
        query = Post.objects.filter(user=request.user)
        return get_paginated_queryset_response(query, request, page_size,
                                               ModelName.POST)
    return Response({Message.SC_NO_AUTH}, status=401)
Exemplo n.º 5
0
def find_post_by_user(request, username):
    page_size = request.data.get("page_size")
    post = Post.objects.filter(user__username=username, community__state=True)
    if post:
        return get_paginated_queryset_response(post, request, page_size,
                                               ModelName.POST)
    return Response({Message.SC_NOT_FOUND}, status=400)
Exemplo n.º 6
0
def filter_by_up_vote(request):
    page_size = request.data.get("page_size")
    query = Post.objects.annotate(
        user_count=Count("up_vote")).order_by("-user_count").filter(
            community__state=True)
    return get_paginated_queryset_response(query, request, page_size,
                                           ModelName.POST)
Exemplo n.º 7
0
def hot(request):
    page_size = request.data.get("page_size")
    post = Post.objects.filter(
        community__state=True,
        timestamp__gte=timestamp_in_the_past_by_day(1),
        timestamp__lte=datetime.datetime.now()).order_by('-point')
    return get_paginated_queryset_response(post, request, page_size,
                                           ModelName.POST)
Exemplo n.º 8
0
def get_post_list(request):
    sort = request.data.get("sort")
    page_size = request.data.get("page_size")
    # this algorithm
    if sort == 'best':
        if request.user.is_authenticated:
            track = Track.objects.filter(user=request.user).first()
            if not track:
                track = Track.objects.create(user=request.user)
                track.save()
            community_track = CommunityTrack.objects.filter(
                track=track).order_by('-timestamp')[0:4]
            list_community_track = []
            for c in community_track:
                list_community_track.append(c.community.community_type)
            community = Community.objects.filter(
                community_type__in=list_community_track)
            query = Post.objects.filter(
                community__community_type__in=list_community_track).order_by(
                    '-point')
            return get_paginated_queryset_response(query, request, page_size,
                                                   ModelName.POST)
    top_community = Community.objects.filter(state=True).annotate(
        user_count=Count('user')).order_by('-user_count')
    if not sort or sort == 'hot':
        sort = '-point'
    if sort == 'timestamp':
        sort = '-timestamp'
    if request.user.is_authenticated:
        # community need to be fix right here!
        top_community = Community.objects.filter(user=request.user).union(
            Community.objects.filter(community__state=True)).union(
                Community.objects.filter(creator=request.user)).distinct()
        query = Post.objects.filter(user=request.user).union(
            Post.objects.filter(community__user=request.user)).union(
                Post.objects.filter(user__following=Profile.objects.filter(
                    user=request.user).first())).union(
                        Post.objects.filter(community__state=True).distinct()
                    ).distinct().order_by(sort)
        return get_paginated_queryset_response(query, request, page_size,
                                               ModelName.POST)
    query = Post.objects.filter(community__state=True,
                                community__in=top_community)
    return get_paginated_queryset_response(query, request, page_size,
                                           ModelName.POST)
Exemplo n.º 9
0
def find_post_by_up_vote(request):
    page_size = request.data.get("page_size")
    if request.user.is_authenticated:
        post = Post.objects.filter(up_vote=request.user)
        if post:
            return get_paginated_queryset_response(post, request, page_size,
                                                   ModelName.POST)
        return Response({Message.SC_NOT_FOUND}, status=400)
    return Response({Message.SC_LOGIN_REDIRECT}, status=401)
Exemplo n.º 10
0
def find_post_by_up_vote(request):
    page_size = request.data.get("page_size")
    if request.user.is_authenticated:
        query = Post.objects.filter(user=request.user).annotate(
            user_count=Count("up_vote")).order_by("-user_count").filter(
                community__user=request.user)
        return get_paginated_queryset_response(query, request, page_size,
                                               ModelName.POST)
    return Response({Message.SC_NO_AUTH}, status=401)
Exemplo n.º 11
0
def find_post_by_community(request, community_type):
    page_size = request.data.get("page_size")
    post = Post.objects.filter(community__community_type=community_type)
    community = Community.objects.filter(community_type=community_type).first()
    if not community:
        return Response({Message.SC_NOT_FOUND}, status=204)
    if request.user.is_authenticated:
        if community.state == True:
            return get_paginated_queryset_response(post, request, page_size,
                                                   ModelName.POST)
        post = post.filter(user=request.user)
        if post:
            return get_paginated_queryset_response(post, request, page_size,
                                                   ModelName.POST)
        return Response({Message.MUST_FOLLOW}, status=403)
    if community.state == False:
        return Response({Message.MUST_FOLLOW}, status=403)
    return get_paginated_queryset_response(post, request, page_size,
                                           ModelName.POST)
Exemplo n.º 12
0
def find_post_by_comment_with_username(request, username):
    page_size = request.data.get("page_size")
    # level 1
    comment_list = Comment.objects.filter(user__username=username,
                                          parent__isnull=True)
    # level 2 + 3
    comment_list_level_3 = Comment.objects.filter(parent__isnull=False).filter(
        parent__parent__isnull=False).filter(
            parent__parent__parent__isnull=True, user__username=username)
    comment_list_level_2 = Comment.objects.filter(parent__isnull=False).filter(
        parent__parent__isnull=True, user__username=username)
    query = Post.objects.filter(comment__in=comment_list)
    query_2 = Post.objects.filter(
        comment__in=parent_comment(comment_list_level_2, 2))
    query_3 = Post.objects.filter(
        comment__in=parent_comment(comment_list_level_3, 3))
    query_result = (query | query_2 | query_3).distinct()
    return get_paginated_queryset_response(query_result, request, page_size,
                                           ModelName.POST)
Exemplo n.º 13
0
def find_post_by_comment(request):
    page_size = request.data.get("page_size")
    if request.user.is_authenticated:
        # level 1
        comment_list = Comment.objects.filter(user=request.user,
                                              parent__isnull=True)
        # level 2 + 3
        comment_list_level_3 = Comment.objects.filter(
            parent__isnull=False).filter(parent__parent__isnull=False).filter(
                parent__parent__parent__isnull=True, user=request.user)
        comment_list_level_2 = Comment.objects.filter(
            parent__isnull=False).filter(parent__parent__isnull=True,
                                         user=request.user)
        query = Post.objects.filter(comment__in=comment_list)
        query_2 = Post.objects.filter(
            comment__in=parent_comment(comment_list_level_2, 2))
        query_3 = Post.objects.filter(
            comment__in=parent_comment(comment_list_level_3, 3))
        query_result = (query | query_2 | query_3).distinct()
        return get_paginated_queryset_response(query_result, request,
                                               page_size, ModelName.POST)
    return Response({Message.SC_NO_AUTH}, status=401)
Exemplo n.º 14
0
def recent(request):
    page_size = request.data.get("page_size")
    post = Post.objects.filter(community__state=True).order_by('-timestamp')
    return get_paginated_queryset_response(post, request, page_size,
                                           ModelName.POST)
Exemplo n.º 15
0
def get_type_list(request):
    page_size = request.data.get("page_size")
    query = PostType.objects.all()
    return get_paginated_queryset_response(query, request, page_size,
                                           ModelName.POST_TYPE)