Beispiel #1
0
def getPosts(request):
    person_id = getPersonID(request)
    # If person_id type is Response that means we have errored
    if type(person_id) is Response:
        return person_id

    try:
        data = Friend.objects.filter(Q(user_a=person_id) | Q(user_b=person_id))
        friends = [entry.user_a if entry.user_a is not person_id else entry.user_b for entry in data]
        friends.append(person_id)
        data = Posts.objects.filter(person_id__in=friends).order_by('pk').values()
        posts_final = []
        for post in data:
            post_by = PersonSerializer(Person.objects.get(pk=post['person_id'])).data
            posts_final.append({**PostsSerializer(Posts.objects.get(pk=post['id'])).data,"person":post_by})
        if posts_final:
            return Response(data=posts_final,status=status.HTTP_200_OK)
        else:
            return Response(errorResponse("No posts found!"),status=status.HTTP_404_NOT_FOUND)
    except Friend.DoesNotExist:
        data = Posts.objects.filter(person_id=person_id)
        postsSerializer = PostsSerializer(data, many=True)
        if postsSerializer.data:
            return Response(data=postsSerializer.data,status=status.HTTP_200_OK)
        else:
            return Response(errorResponse("No posts found!"),status=status.HTTP_404_NOT_FOUND)
Beispiel #2
0
def getFriendSuggestions(request):
    person_id = getPersonID(request)
    # If person_id type is Response that means we have errored
    if type(person_id) is Response:
        return person_id
    
    # We have some friends
    friends = Friend.objects.filter(Q(user_a=person_id) | Q(user_b=person_id))

    # Let's add our current friends id's in list to  exclude from suggestions
    # and then excluding myself too
    friends_ids = [a.user_b if a.user_a is person_id else a.user_a for a in friends]
    friends_ids.append(person_id)

    # Apend people we have already sent friend request into friends_ids to avoid those suggestions
    try:
        freqs = FriendRequest.objects.filter(Q(from_user=person_id) | Q(to_user=person_id))
        for fr in freqs:
            if fr.to_user == person_id:
                friends_ids.append(fr.from_user)
            else:
                friends_ids.append(fr.to_user)
    except FriendRequest.DoesNotExist:
        pass

    # limiting the suggestions to 25 to prevent sending whole database
    persons = list(Person.objects.filter(~Q(id__in=friends_ids))[:25])
    if len(persons) > 0:
        # getting 10 random ids from people we are not friends with
        # and then serializing them and returning them
        random_persons_ids = [a.id for a in random.sample(persons,min(len(persons), 10))]
        random_persons = Person.objects.filter(id__in=random_persons_ids)
        persons_dict = [PersonSerializer(person).data for person in random_persons]
        return Response(data=dict(friend_suggestions=persons_dict), status=status.HTTP_200_OK)
    return Response(errorResponse("No friend suggestions."),status=status.HTTP_204_NO_CONTENT)
Beispiel #3
0
def getPost(post_key):
    try:
        data = Posts.objects.get(pk=post_key)
        post_by = PersonSerializer(Person.objects.get(pk=data.person_id)).data
        return Response(data={**PostsSerializer(Posts.objects.get(pk=data.id)).data,"person":post_by},status=status.HTTP_200_OK)
    except Posts.DoesNotExist:
        return Response(errorResponse("Post not found!"),status=status.HTTP_404_NOT_FOUND)
 def get_likes(self, obj):
     if obj.likes and obj.likes['persons']:
         people = []
         for person in obj.likes['persons']:
             people.append(
                 PersonSerializer(Person.objects.get(pk=person)).data)
         return dict(persons=people, person_ids=obj.likes['persons'])
Beispiel #5
0
def postNewComment(request, post_id):
    person = getPersonID(request)
    if type(person) is Response:
        return person

    post = getPost(post_id)
    if type(post) is Response:
        return post
    time_stamp = datetime.now().timestamp()

    # Filtering
    if post.person_id != person:
        # post author themselves are not commenting on their post
        if isFriends(post.person_id, person) == False:
            # post author and person trying to comment are not friends
            # if they are friends then we let the commentator post a comment
            return Response(errorResponse(INVALID_REQUEST),
                            status=status.HTTP_400_BAD_REQUEST)

    commentSerializer = CommentSerializer(
        data={
            'post_id': post.id,
            'person_id': person,
            'comment_text': request.data['comment_text'],
            'comment_parent': request.data['comment_parent'],
            'created': time_stamp,
            'updated': time_stamp
        })
    if commentSerializer.is_valid():
        commentSerializer.save()
        try:
            # make a notification to send
            p_for = post.person_id if request.data[
                'comment_parent'] == "0" else Comment.objects.get(
                    pk=int(request.data['comment_parent'])).person_id
            if p_for != person:
                notification = Notification(
                    noti=1 if request.data['comment_parent'] == "0" else 5,
                    person_for=p_for,
                    person_from=person,
                    about=post.id,
                    created=datetime.now().timestamp())
                notification.save()
        except Comment.DoesNotExist:
            print("errored")
            pass
        return Response(data={
            **commentSerializer.data, 'person':
            PersonSerializer(Person.objects.get(id=person)).data
        },
                        status=status.HTTP_201_CREATED)
    return Response(commentSerializer.errors,
                    status=status.HTTP_400_BAD_REQUEST)
Beispiel #6
0
def getFriends(request):
    person_id = getPersonID(request)
    # If person_id type is Response that means we have errored
    if type(person_id) is Response:
        return person_id

    try:
        data = Friend.objects.filter(Q(user_a=person_id) | Q(user_b=person_id))
        friends = [entry.user_a if entry.user_a is not person_id else entry.user_b for entry in data]
        if friends:
            persons = Person.objects.filter(id__in=friends)
            persons_dict = [PersonSerializer(person).data for person in persons]
            return Response(data=persons_dict, status=status.HTTP_200_OK)
        return Response(data=errorResponse("No friends found!"),status=status.HTTP_404_NOT_FOUND)
    except Friend.DoesNotExist:
        return Response(data=errorResponse("No friends found!"),status=status.HTTP_404_NOT_FOUND)
Beispiel #7
0
def getNotifications(request):
    person_id = getPersonID(request)
    if type(person_id) is Response:
        return person_id

    # Get notifications which are at most a day old; or are not seen yet unrelated to time
    notifications = Notification.objects.filter(person_for=person_id).filter(
        Q(seen=0) | Q(created__range=(
            (datetime.datetime.now() - datetime.timedelta(days=1)).timestamp(),
            datetime.datetime.now().timestamp()))).values()
    res = []
    for notif in notifications:
        person_by = PersonSerializer(
            Person.objects.get(pk=notif['person_from'])).data
        res.append({**notif, **{"person": person_by}})
    return Response(data=res, status=status.HTTP_200_OK)
Beispiel #8
0
def getFriendRequests(request):
    person_id = getPersonID(request)
    # If person_id type is Response that means we have errored
    if type(person_id) is Response:
        return person_id
    
    data = FriendRequest.objects.filter(to_user=person_id)
    friendrequests = []
    if data:
        for fr in data:
            # Check if person exist or if account id deleted
            try:
                persons = Person.objects.get(id=fr.from_user)
                personSerializer = PersonSerializer(persons)
                friendrequests.append({**personSerializer.data, 
                    **{'from_user': fr.from_user, 'to_user': fr.to_user, 'request_id': fr.id,'since':fr.since}})
            except Person.DoesNotExist:
                FriendRequest.objects.get(pk=fr.id).delete()
        return Response(data=dict(requests=friendrequests), status=status.HTTP_200_OK)
    return Response(data=errorResponse("No friend requests!"),status=status.HTTP_200_OK)
Beispiel #9
0
def getPostComments(request, post):
    person = getPersonID(request)
    if type(person) is Response:
        return person

    author = getAuthor(post)
    if type(author) is Response:
        return author

    if isFriends(author.id, person) or author.id == person:
        # Person can only retrieve comments if they are friends with author or author themselves
        result = [{
            **comment, 'person':
            PersonSerializer(Person.objects.get(id=comment['person_id'])).data
        } for comment in Comment.objects.filter(
            post_id=post).order_by('pk').values()]
        return Response({"comments": result}, status=status.HTTP_200_OK)

    return Response(errorResponse(INVALID_REQUEST),
                    status=status.HTTP_400_BAD_REQUEST)