Beispiel #1
0
 def post(self, request, id):
     postForm = self.form_class(request.POST, request.FILES)
     if postForm.is_valid():
         if request.POST.get("method") == "1":
             postTemp = postForm.save(commit=False)
             beaver = getBeaverInstance(request)
             post = Post.objects.filter(pk=id, post_creator=beaver).first()
             if post is None:
                 message = "Cannot be find the specified post"
                 messages.error(request, message, fail_silently=True)
                 return HttpResponseRedirect(reverse("personal"))
             post.caption = postTemp.caption
             if postTemp.picture:
                 post.picture = postTemp.picture
             post.save()
             message = "Post Updated Successfully"
             messages.success(request, message, fail_silently=True)
             return HttpResponseRedirect(reverse("personal"))
         else:
             beaver = getBeaverInstance(request)
             post = Post.objects.filter(pk=id, post_creator=beaver).first()
             if post is None:
                 message = "Cannot find the specified post"
                 messages.error(request, message, fail_silently=True)
                 return HttpResponseRedirect(reverse("personal"))
             post.delete()
             message = "Post Deleted Successfully"
             messages.success(request, message, fail_silently=True)
             return HttpResponseRedirect(reverse("personal"))
Beispiel #2
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     beaverInstance = getBeaverInstance(self.request)
     response = ChatInfo.getAllURLParams(beaverInstance)
     friends = []
     for users in response:
         # The dictionary should contain the username, urlparam, profile picture
         dic = {}
         dic["urlparam"] = ChatInfo.convertUUIDToString(users.urlparam)
         friendInstance = users.member1
         if users.member1 == beaverInstance:
             friendInstance = users.member2
         dic["friend"] = friendInstance.user.username
         dic["profile"] = friendInstance.profile_photo
         friends.append(dic)
     context["friends"] = friends
     urlparam = kwargs.get("urlparam")
     try:
         uuidParam = ChatInfo.convertStringToUUID(urlparam)
         response = ChatInfo.objects.get(urlparam=uuidParam)
         talkingTo = response.member1
         if beaverInstance == talkingTo:
             talkingTo = response.member2
         context["chatTo"] = talkingTo
         context["chatmssgs"] = response.getAllMessages()
         context["room_name"] = kwargs.get("urlparam")
     except Exception:
         pass
     return context
Beispiel #3
0
 def get(self, request):
     beaver = getBeaverInstance(request)
     posts = beaver.posts.all().order_by("?")
     count = posts.count()
     friends = beaver.friends.all()
     friends_posts = Post.objects.filter(post_creator__in=friends)
     posts = posts | friends_posts
     liked = beaver.total_likes.values_list("post__id", flat=True)
     pending_requests = FriendRequest.objects.filter(receiver=beaver)
     post_liked = Post.objects.filter(pk__in=liked)
     pending_friend_requests = pending_requests[:3]
     all_requests = FriendRequest.objects.filter(
         Q(sender=beaver) | Q(receiver=beaver))
     already_sent_pending = [request.user]
     for _request in all_requests:
         if beaver == _request.sender:
             already_sent_pending.append(_request.receiver.user)
         else:
             already_sent_pending.append(_request.sender.user)
     for friend in beaver.friends.all():
         already_sent_pending.append(friend.user)
     random_friends = Beaver.objects.filter(~Q(
         user__in=already_sent_pending))[:3]
     kwargs = {
         "profile": beaver,
         "posts": posts,
         "post_liked": post_liked,
         "random": random_friends,
         "pending": pending_friend_requests,
         "count": count,
     }
     return render(request, self.template_name, kwargs)
Beispiel #4
0
def like(request):
    if request.is_ajax():
        if not request.user:
            return JsonResponse(None)
        id = request.GET.get("id")
        beaver = getBeaverInstance(request)
        post = Post.objects.filter(
            Q(pk=id, post_creator__in=beaver.friends.all())
            | Q(pk=id, post_creator=beaver)).first()
        if post is None:
            return JsonResponse(None)
        like = Like.objects.filter(post=post, liker=beaver).first()
        if like is None:
            like = Like(post=post, liker=beaver)
            like.save()
            data = {
                "status": True,
                "id": post.pk,
                "count": post.post_likes.all().count(),
            }
            log.error(data)
            return JsonResponse(data)
        else:
            like.delete()
            data = {
                "status": False,
                "id": post.pk,
                "count": post.post_likes.all().count(),
            }
            return JsonResponse(data)
Beispiel #5
0
 def get(self, request, friend_username):
     user_beaver = getBeaverInstance(request)
     status = 1
     friend_user = User.objects.filter(username=friend_username).first()
     if friend_user is None:
         message = f"Cannot find anyone with username {friend_username}"
         messages.error(request, message, fail_silently=True)
         return HttpResponseRedirect(reverse("posts:feed"))
     beaver = getBeaverInstanceFromUser(friend_user)
     if beaver is None:
         message = f"Cannot find anyone with username {friend_username}"
         messages.error(request, message, fail_silently=True)
         return HttpResponseRedirect(reverse("posts:feed"))
     if beaver == user_beaver:
         return HttpResponseRedirect(reverse("personal"))
     if FriendRequest.objects.filter(Q(sender=user_beaver,
                                       receiver=beaver)):
         status = 2
     if FriendRequest.objects.filter(Q(sender=beaver,
                                       receiver=user_beaver)):
         status = 3
     pending_requests = FriendRequest.objects.filter(receiver=user_beaver)
     pending_friend_requests = pending_requests[:3]
     all_requests = FriendRequest.objects.filter(
         Q(sender=user_beaver) | Q(receiver=user_beaver))
     already_sent_pending = [request.user]
     for _request in all_requests:
         if user_beaver == _request.sender:
             already_sent_pending.append(_request.receiver.user)
         else:
             already_sent_pending.append(_request.sender.user)
     for friend in user_beaver.friends.all():
         already_sent_pending.append(friend.user)
     random_friends = Beaver.objects.filter(~Q(
         user__in=already_sent_pending))[:3]
     if user_beaver.friends.filter(user=friend_user).first() is None:
         posts = beaver.posts.all().order_by("-posted_on")
         kwargs = {
             "profile": beaver,
             "posts": posts,
             "random": random_friends,
             "pending": pending_friend_requests,
             "status": status,
             "friend": False,
         }
         return render(request, self.template_name, kwargs)
     posts = beaver.posts.all().order_by("-posted_on")
     liked = user_beaver.total_likes.values_list("post__id", flat=True)
     post_liked = Post.objects.filter(pk__in=liked)
     kwargs = {
         "profile": beaver,
         "posts": posts,
         "post_liked": post_liked,
         "random": random_friends,
         "pending": pending_friend_requests,
         "status": 4,
         "friend": True,
     }
     return render(request, self.template_name, kwargs)
Beispiel #6
0
 def checkComment(self, request, post):
     if self.is_valid():
         comment = self.save(commit=False)
         comment.comment_creator = getBeaverInstance(request)
         comment.post = post
         comment.save()
         return True
     return False
Beispiel #7
0
 def checkPost(self, request):
     if self.is_valid():
         post = self.save(commit=False)
         beaver = getBeaverInstance(request)
         post.post_creator = beaver
         post.save()
         return True
     return False
Beispiel #8
0
 def get(self, request, id):
     beaver = getBeaverInstance(request)
     post = Post.objects.filter(pk=id, post_creator=beaver).first()
     if post is None:
         message = "Cannot be find the specified post"
         messages.error(request, message, fail_silently=True)
         return HttpResponseRedirect(reverse("personal"))
     kwargs = {
         "post": post,
     }
     return render(request, self.template_name, kwargs)
Beispiel #9
0
def send_request(request):
    if request.is_ajax():
        if not request.user:
            return JsonResponse(None)
        username_friend = request.GET.get("username_friend")
        friend_user = User.objects.filter(username=username_friend).first()
        user_beaver = getBeaverInstance(request)
        friend_beaver = getBeaverInstanceFromUser(friend_user)
        if friend_beaver is None:
            return JsonResponse({"status": 400})
        FriendRequest.sendRequest(user_beaver, friend_beaver)
        return JsonResponse({"status": 201})
Beispiel #10
0
def delete_request(request):
    if not request.user:
        return JsonResponse(None)
    username_friend = request.GET.get("username_friend")
    friend_user = User.objects.filter(username=username_friend).first()
    user_beaver = getBeaverInstance(request)
    friend_beaver = getBeaverInstanceFromUser(friend_user)
    request_instance = FriendRequest.objects.filter(
        sender=user_beaver, receiver=friend_beaver).first()
    if request_instance is None:
        return JsonResponse({"status": 400})
    request_instance.delete()
    return JsonResponse({"status": 201})
Beispiel #11
0
 def get(self, request, id):
     beaver = getBeaverInstance(request)
     # Can only comment on friend or their own posts
     post = Post.objects.filter(
         Q(pk=id, post_creator__in=beaver.friends.all())
         | Q(pk=id, post_creator=beaver)).first()
     if post is None:
         message = "Cannot find the specified post"
         messages.error(request, message, fail_silently=True)
         return HttpResponseRedirect(reverse("personal"))
     kwargs = {
         "post": post,
     }
     return render(request, self.template_name, kwargs)
Beispiel #12
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     beaverInstance = getBeaverInstance(self.request)
     response = ChatInfo.getAllURLParams(beaverInstance)
     friends = []
     for users in response:
         # The dictionary should contain the username, urlparam, profile picture
         dic = {}
         dic["urlparam"] = ChatInfo.convertUUIDToString(users.urlparam)
         friendInstance = users.member1
         if users.member1 == beaverInstance:
             friendInstance = users.member2
         dic["friend"] = friendInstance.user.username
         dic["profile"] = friendInstance.profile_photo
         friends.append(dic)
     context["friends"] = friends
     return context
Beispiel #13
0
 def post(self, request, id):
     beaver = getBeaverInstance(request)
     # Can only comment on friend or their own posts
     post = Post.objects.filter(
         Q(pk=id, post_creator__in=beaver.friends.all())
         | Q(pk=id, post_creator=beaver)).first()
     if post is None:
         message = "Cannot find the specified post"
         messages.error(request, message, fail_silently=True)
         return HttpResponseRedirect(reverse("personal"))
     commentForm = CommentForm(request.POST)
     if commentForm.checkComment(request, post):
         message = "Comment created successfully"
         messages.success(request, message, fail_silently=True)
         return HttpResponseRedirect(
             reverse("posts:comment", kwargs={"id": post.pk}))
     message = "Comment cannot be created"
     messages.error(request, message, fail_silently=True)
     return HttpResponseRedirect(
         reverse("posts:comment", kwargs={"id": post.pk}))