def submit_reply_comment(request): user = request.user reply_to = request.data.get('reply_to_id') content = request.data.get('content') if not (user and reply_to and content): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) comment = Comment.objects.filter(id=reply_to).first() if (comment is None): return JsonResponse(get_error_serialized(100, "Comment not found!").data, status=HTTPStatus.NOT_FOUND) data = {'author': user.id, 'content': content} new_comment = CommentSerializer(data=data) if (new_comment.is_valid()): new_comment.save() else: return JsonResponse(new_comment.errors, status=HTTPStatus.BAD_REQUEST) data = {'reply_to': reply_to, 'reply': new_comment.instance.id} reply_comment = CommentReplySerializer(data=data) if (reply_comment.is_valid()): reply_comment.save() else: return JsonResponse(reply_comment.errors, status=HTTPStatus.BAD_REQUEST) return JsonResponse( { 'message': 'Comment submitted.', 'comment': DisplayCommentSerializer(new_comment.instance).data }, status=HTTPStatus.CREATED)
def get_hashtag_posts(request): hashtag_text = request.query_params.get('text', None) sort = request.query_params.get('sort', 'latest') viewer = None if not (request.user.is_anonymous): viewer = request.user.username if (hashtag_text is None): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) hashtag = Hashtag.objects.filter(text=hashtag_text).first() if (hashtag is None): return JsonResponse(get_error_serialized(100, "Hashtag not found!").data, status=HTTPStatus.NOT_FOUND) records = PostHashtag.objects.filter(hashtag=hashtag.id) posts = [record.post for record in records] if (sort == 'latest'): posts = sorted(posts, key=lambda x: x.date_created)[::-1] elif (sort == 'popular'): posts = sorted( posts, key=lambda x: len(PostLike.objects.filter(post=x.id)))[::-1] return JsonResponse( { 'hashtag': HashtagSerializer(hashtag).data, 'posts': PostSerializer(posts, many=True, context={ 'viewer': viewer }).data }, status=HTTPStatus.OK)
def get_public_profile(request): viewer = request.user username = request.query_params.get('username', None) if (username is None): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) user = User.objects.filter(username=username).first() if (user is None): return JsonResponse(get_error_serialized(100, "User not found!").data, status=HTTPStatus.NOT_FOUND) public_profile = PublicProfileSerializer(user, context={"viewer_id": viewer.id}) data = public_profile.data data['follower'] = "5" data['follower'] = "10" data['nickname'] = user.first_name + ' ' + user.last_name user_posts = Post.objects.filter(author=user.id) if (user_posts is not None): data['posts_count'] = str(len(list(user_posts))) else: data['posts_count'] = "0" post_likes = list(PostLike.objects.filter(user=user.id)) comment_likes = list(CommentLike.objects.filter(user=user.id)) data['likes_count'] = len(post_likes) + len(comment_likes) comment_serializer = UserCommentSerializer(user) if (comment_serializer is not None): data['comments_count'] = str( len(comment_serializer.get_post_replies(user)) + len(comment_serializer.get_comment_replies(user))) else: data['comments_count'] = "0" return JsonResponse(data=data, status=HTTPStatus.OK)
def submit_post_comment(request): user = request.user post_id = request.data.get('post') content = request.data.get('content') if not (content and post_id and user): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) post = Post.objects.filter(id=post_id).first() if (post is None): return JsonResponse(get_error_serialized(100, "Post not found!").data, status=HTTPStatus.NOT_FOUND) data = {'author': user.id, 'content': content} comment = CommentSerializer(data=data) if (comment.is_valid()): comment.save() else: return JsonResponse(comment.errors, status=HTTPStatus.BAD_REQUEST) data = {'post': post_id, 'reply': comment.instance.id} reply_post = PostReplySerializer(data=data) if (reply_post.is_valid()): reply_post.save() else: return JsonResponse(reply_post.errors, status=HTTPStatus.BAD_REQUEST) return JsonResponse( { 'message': 'Comment submitted.', 'comment': DisplayCommentSerializer(comment.instance).data }, status=HTTPStatus.CREATED)
def get_reply_comments(request): reply_to = request.query_params.get('reply_to', None) depth = request.query_params.get('depth', None) startIdx = request.query_params.get('start_index', None) length = request.query_params.get('max_len', None) reply_len = request.query_params.get('max_reply_len', None) viewer = request.query_params.get('viewer') if not (depth and reply_to and startIdx and length): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) try: startIdx = int(startIdx) length = int(length) except: return JsonResponse(get_error_serialized( 103, "Integer conversion error!").data, status=HTTPStatus.BAD_REQUEST) if (viewer is None and not request.user.is_anonymous): viewer = request.user.username comment = Comment.objects.filter(id=reply_to).first() if (comment is None): return JsonResponse(get_error_serialized(100, "Comment not found!").data, status=HTTPStatus.NOT_FOUND) return JsonResponse(RepliedCommentSerializer(comment, context={ 'depth': depth, 'start_index': startIdx, 'max_len': reply_len, 'viewer': viewer, 'start_index': startIdx }).data, status=HTTPStatus.OK)
def get_post_likes(request): post_id = request.query_params.get('id', None) if (post_id is None): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) post = Post.objects.filter(id=post_id).first() if (post is None): return JsonResponse(get_error_serialized(100, "Post not found!").data, status=HTTPStatus.NOT_FOUND) return JsonResponse(ViewPostLikesSerializer(post).data, status=HTTPStatus.OK)
def get_comment_likes(request): comment_id = request.query_params.get('id', None) if (comment_id is None): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) comment = Comment.objects.filter(id=comment_id).first() if (comment is None): return JsonResponse(get_error_serialized(100, "Coment not found!").data, status=HTTPStatus.NOT_FOUND) return JsonResponse(ViewCommentLikesSerializer(comment).data, status=HTTPStatus.OK)
def get_post_comments(request): post_id = request.query_params.get('post_id', None) depth = request.query_params.get('depth', '0') startIdx = request.query_params.get('start_index', None) length = request.query_params.get('max_len', None) reply_len = request.query_params.get('max_reply_len', None) viewer = request.query_params.get('viewer') if not (depth and post_id and startIdx and length): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) try: startIdx = int(startIdx) length = int(length) depth = int(depth) except: return JsonResponse(get_error_serialized( 103, "Integer conversion error!").data, status=HTTPStatus.BAD_REQUEST) if (viewer is None and not request.user.is_anonymous): viewer = request.user.username post = Post.objects.filter(id=post_id).first() if (post is None): return JsonResponse(get_error_serialized(100, "Post not found!").data, status=HTTPStatus.NOT_FOUND) post_comments = PostReply.objects.filter(post=post_id) result = [] post_comments = list(post_comments) total_comments = len(post_comments) if (startIdx >= len(post_comments)): post_comments = [] else: post_comments = post_comments[startIdx:startIdx + length] for comment in list(post_comments): comment = comment.reply if (comment is None): continue result.append( RepliedCommentSerializer(comment, context={ 'depth': str(depth - 1), 'max_len': reply_len, 'viewer': viewer, 'start_index': startIdx }).data) return JsonResponse( { 'post_id': post_id, 'total_comments': total_comments, 'retrived_comments_count': len(result), 'comments': result }, status=HTTPStatus.OK)
def submit_post_likes(request): post_id = request.data.get('post_id') user = request.user if not (post_id and user): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) post = Post.objects.filter(id=post_id).first() if (post is None): return JsonResponse(get_error_serialized(100, "Post not found!").data, status=HTTPStatus.NOT_FOUND) like_ = PostLike.objects.filter(post=post_id, user=user.id).first() if (like_ is not None): return JsonResponse(ErrorSerializer(get_error(110)).data, status=HTTPStatus.BAD_REQUEST) data = {'post': post_id, 'user': user.id} like = PostLikeSerializer(data=data) if (like.is_valid()): like.save() else: return JsonResponse(like.errors, status=HTTPStatus.BAD_REQUEST) return JsonResponse({'message': 'Like submitted.'}, status=HTTPStatus.CREATED)
def delete_comment(request): user = request.user comment_id = request.data.get('id', None) if comment_id is None: return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) comment = Comment.objects.filter(id=comment_id, author=user.id).first() if (comment is None): return JsonResponse(get_error_serialized(100, "Comment not found!").data, status=HTTPStatus.NOT_FOUND) recursively_delete_comment(comment) return JsonResponse({'message': "Comment deleted successfully."}, status=HTTPStatus.OK)
def get_user_likes(request): username = request.query_params.get('username', None) viewer = request.query_params.get('viewer', None) if not (username): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) user = User.objects.filter(username=username).first() if (user is None): return JsonResponse(get_error_serialized(100, "User not found!").data, status=HTTPStatus.NOT_FOUND) if not (request.user.is_anonymous) and viewer is None: viewer = request.user.username return JsonResponse(UserLikesSerializer(user, context={ 'viewer': viewer }).data, status=HTTPStatus.OK)
def delete_post_like(request): user = request.user post_id = request.data.get('id', None) if not (post_id and user): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) post = Post.objects.filter(id=post_id).first() if (post is None): return JsonResponse(get_error_serialized(100, "Post not found!").data, status=HTTPStatus.NOT_FOUND) like = PostLike.objects.filter(user=user.id, post=post.id).first() if (like is None): return JsonResponse(get_error_serialized(100, "Like not found!").data, status=HTTPStatus.NOT_FOUND) like.delete() return JsonResponse({"message": "Like deleted successfully."}, status=HTTPStatus.OK)
def get_user_comments(request): username = request.query_params.get('username') viewer = request.query_params.get('viewer') if not (username): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) if (viewer is None and not request.user.is_anonymous): viewer = request.user.username user = User.objects.filter(username=username).first() if (user is None): return JsonResponse(get_error_serialized(100, "User not found!").data, status=HTTPStatus.NOT_FOUND) return JsonResponse(data=UserCommentSerializer(user, context={ 'viewer': viewer }).data, status=HTTPStatus.OK)
def get_similar_to(request): string = request.query_params.get('text', None) if (string is None): return JsonResponse(ErrorSerializer(get_error(103)).data, status=HTTPStatus.BAD_REQUEST) hashtags = Hashtag.objects.all() if string == "": return JsonResponse( {'hashtags': HashtagSerializer(hashtags, many=True).data}, status=HTTPStatus.OK) edit_distances = {} for hashtag in hashtags: edit_distances[hashtag] = edit_distance(string, hashtag.text, len(string), len(hashtag.text)) hashtags = sorted(list(hashtags), key=lambda h: edit_distances[h]) result = [h for h in hashtags if edit_distances[h] < len(h.text)] return JsonResponse( {'hashtags': HashtagSerializer(result, many=True).data}, status=HTTPStatus.OK)