Ejemplo n.º 1
0
def profile(request, user_name):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)

    if request.method == 'GET':
        posts = Post.objects.filter(user=user).count()
        friends = Friend.objects.filter(user=user).count()

        context = {
            "title": "profile",
            "user_name": user_name,
            "user_photo": user.photo.url,
            "posts": posts,
            "friends": friends,
        }
        return render(request, "profile.html", context=context)
    elif request.method == 'POST':
        photo = request.FILES
        user_form = UserForm(request.POST, photo)
        if user_form.is_valid():
            user.photo = user_form.cleaned_data['photo']
            user.save()
        data = {"photo": user.photo.url}
        return JsonResponse(data)
Ejemplo n.º 2
0
def post(request, user_name):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)

    if request.method == "POST":
        post_text = request.POST.get("post_text")
        post_text = remove_danger_symbols(post_text)
        post_photo = request.FILES
        if not post_text and not post_photo:
            data = {
                "error": "Sorry, cannot share an empty post",
            }
            return JsonResponse(data)

        post_form = PostForm(request.POST, post_photo)
        if post_form.is_valid():
            new_post = Post.objects.create(user=user, post_text=post_text, post_photo=post_form.cleaned_data['post_photo'])
            data = {
                "post_id": new_post.id,
                "post_text": new_post.post_text,
                "post_photo_url": new_post.post_photo.url,
            }
        else:
            new_post = Post.objects.create(user=user, post_text=post_text)
            data = {
                "post_id": new_post.id,
                "post_text": new_post.post_text,
                "post_photo_url": "",
            }

        return JsonResponse(data)
Ejemplo n.º 3
0
def friend(request, user_name):
    if request.method == 'POST':
        user = get_user(user_name, request)
        if not user:
            return not_signed_in_error(request)

        friend_name = request.POST.get("username")

        error = ""
        if friend_name == user_name:
            # When the friend's name is the current user's name
            error = "Sorry, you cannot add yourself as a friend."
        elif Friend.objects.filter(friend_name=friend_name,
                                   user_id=user.id).count():
            # When the friend already exists
            error = "Sorry, you have added this friend."
        try:
            friend = User.objects.get(username=friend_name)
        except:
            # When the friend does not exist
            error = "Sorry, please enter a valid username"

        if error:
            data = {"error": error}
            return JsonResponse(data)
        else:
            # Add friends
            Friend.objects.create(friend_name=friend_name, user=user)
            Friend.objects.create(friend_name=user_name, user=friend)

            # Create a room for the user and each friend
            Room.objects.create(user_one=user_name, user_two=friend_name)

            data = {"friend_name": friend_name}
            return JsonResponse(data)
Ejemplo n.º 4
0
def messages(request, user_name, friend_name):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)

    friend = get_object_or_404(Friend, friend_name=friend_name, user=user)

    if request.method == 'GET':
        friend.last_message_color = "#5c5c5c"
        friend.save()

        response = {"messages": [], "room": None}

        # Get messages between the user and a friend
        messages = Message.objects.filter(
            Q(sender_name=user_name, receiver_name=friend_name)
            | Q(sender_name=friend_name, receiver_name=user_name)).order_by(
                "id")
        for message in messages:
            response["messages"].append({
                "sender_name": message.sender_name,
                "receiver_name": message.receiver_name,
                "content": message.content
            })

        # Get the room between the user and a friend
        room = Room.objects.get(
            Q(user_one=user_name, user_two=friend_name)
            | Q(user_one=friend_name, user_two=user_name))
        response["room"] = room.id

        return JsonResponse(response, safe=False)
    elif request.method == 'POST':
        content = remove_danger_symbols(request.POST.get("content"))

        # Store the last message between the user and the friend
        friend.last_message = content
        friend.last_message_color = "#008000"
        friend.save()

        temp_user = User.objects.get(username=friend_name)
        temp_friend = get_object_or_404(Friend,
                                        friend_name=user_name,
                                        user=temp_user)
        temp_friend.last_message = content
        temp_friend.last_message_color = "#008000"
        temp_friend.save()

        message = Message.objects.create(friend=friend,
                                         sender_name=user_name,
                                         receiver_name=friend_name,
                                         content=content)
        data = {
            "sender_name": message.sender_name,
            "receiver_name": message.receiver_name,
            "content": message.content
        }
        return JsonResponse(data)
Ejemplo n.º 5
0
def chat(request, user_name):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)
    friends = Friend.objects.filter(user=user).order_by('-id')
    context = {
        "title": "chat",
        "user_name": user_name,
        "friends": friends,
        "user_photo": user.photo.url,
    }
    return render(request, "chat.html", context=context)
Ejemplo n.º 6
0
def like(request, user_name, post_id):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)

    if request.method == "POST":
        try:
            Like.objects.get(liker=user_name, post_id=post_id)
            return HttpResponse("Exist")
        except:
            new_like = Like.objects.create(liker=user_name, post_id=post_id)
            data = {
                "post_id": post_id,
                "like_id": new_like.id,
                "liker": new_like.liker,
            }
            return JsonResponse(data)
Ejemplo n.º 7
0
def share(request, user_name):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)

    post_list = Post.objects.filter(user=user).order_by("-post_date")
    like_list = Like.objects.all()
    comment_list = Comment.objects.all()
    if request.method == "GET":
        context = {
            "title": "share",
            "user_name": user_name,
            "user_photo": user.photo.url,
            "post_list": post_list,
            "like_list": like_list,
            "comment_list": comment_list,
        }
        return render(request, "share.html", context=context)
Ejemplo n.º 8
0
def comment(request, user_name, post_id):
    user = get_user(user_name, request)
    if not user:
        return not_signed_in_error(request)

    text = request.POST.get("text")
    text = remove_danger_symbols(text)
    if text:
        Comment.objects.create(commenter=user_name, comment_text=text, post_id=post_id)
        data = {
            "post_id": post_id,
            "commenter": user_name,
            "comment_text": text,
        }
        return JsonResponse(data)
    else:
        error = {
            "error": "error"
        }
        return JsonResponse(error)