def delete(self, request, format=None): '''To remove upvote or downvote. Takes comment_id and type. # type 1: remove upvote # type 2: remove downvote returns the final comment object.''' try: print(request.GET) comment = Comment.objects.get( id=int(request.GET.get('comment_id'))) query_type = int(request.GET.get('type')) print(comment, query_type) if (query_type == 1): remove_upvoter = request.user comment.upvoters.remove(remove_upvoter) comment.save() serializer = CommentSerializer(comment, many=False) return Response(serializer.data) elif (query_type == 2): remove_downvoter = request.user comment.downvoters.remove(remove_downvoter) comment.save() serializer = CommentSerializer(comment, many=False) return Response(serializer.data) return Response({"error": "Type value is not defined."}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: print(e) return Response({"error": "Comment query doesn't exists."}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): '''To post a comment on a resource Takes resource_id, comment_id(if replying a comment), content Returns resource with all comments''' try: resource = Resource.objects.get(id=request.data.get('resource_id')) comment = Comment() if request.data.get('comment_id'): try: comment.parent = Comment.objects.get(id=request.data.get('comment_id')) comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() except Exception as e: return Response({ "error": "Comment query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST) else: comment.parent = None comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() resource.comments.add(comment) resource.save() allComments = resource.comments.all() serialized_comments = CommentSerializer(allComments, many=True).data serialized_resource = ResourceSerializer(resource, many=False).data serialized_resource['comments'] = serialized_comments return Response(serialized_resource) except Exception as e: return Response({ "error": "Resource query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): '''To get comment in an announcement. Takes annoucement_id. returns the announcement object with a list of comments.''' try: announcement = Announcement.objects.get(id=request.GET.get('id')) if request.user.username == announcement.classroom.creator.username or request.user in announcement.classroom.moderators.all() or request.user in announcement.classroom.students.all(): announcement_serializer = AnnouncementSerializer(announcement, many=False).data allcomments = announcement.comment.all() print("Start") comments_serialized=[] for comment in allcomments: serializedcomment = CommentSerializer(comment, many=False).data if(request.user in comment.upvoters.all()): serializedcomment['has_Upvoted']=1 else: serializedcomment['has_Upvoted']=0 if(request.user in comment.downvoters.all()): serializedcomment['has_Downvoted']=1 else: serializedcomment['has_Downvoted']=0 comments_serialized.append(serializedcomment) announcement_serializer['comments'] = comments_serialized serializedclassroom = ClassroomSerializer(announcement.classroom, many=False) announcement_serializer['classroom'] = serializedclassroom.data return Response(announcement_serializer) else: return Response({ "error": "You aren't enrolled in this classroom." }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response({ "error": "Announcement query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, format=None): '''To edit comment, add upvote or add a downvote. Takes comment_id, type, comment_text(only if type=1) # type 1: comment edit # type 2: upvote # type 3: downvote returns the final comment object.''' try: comment = Comment.objects.get(id=request.data.get('comment_id')) query_type = request.data.get('type') if (query_type == 1): if (comment.commenter == request.user): comment.comment_text = request.data.get('comment_text') comment.save() serializer = CommentSerializer(comment, many=False) return Response(serializer.data) return Response({"error": "Can't edit this comment."}, status=status.HTTP_400_BAD_REQUEST) elif (query_type == 2): upvoter = request.user comment.upvoters.add(upvoter) print(comment) comment.save() serializer = CommentSerializer(comment, many=False) return Response(serializer.data) elif (query_type == 3): downvoter = request.user comment.downvoters.add(downvoter) print(comment) comment.save() serializer = CommentSerializer(comment, many=False) return Response(serializer.data) return Response({"error": "Type value is not defined."}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response({"error": "Comment query doesn't exists."}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): '''To get comments with upvoters, downvoters and its child. Takes the comment_id. return the comment object with list of upvoters, list of downvoters and list of its child''' try: comment = Comment.objects.get(id=request.GET.get('id')) print(comment) child_comments = comment.child_comment.all() upvoters = comment.upvoters.all() downvoters = comment.downvoters.all() serialized_comments = CommentSerializer(child_comments, many=True).data serializer = CommentSerializer(comment, many=False).data serialized_upvotes = UserSerializer(upvoters, many=True).data serialized_downvoters = UserSerializer(downvoters, many=True).data serializer['child'] = serialized_comments serializer['upvoters'] = serialized_upvotes serializer['downvoters'] = serialized_downvoters print(serializer) return Response(serializer) except Exception as e: return Response({"error": "Comment query doesn't exists."}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): '''To post a comment on an assignment Takes assignment_id, comment_id(if replying a comment), content Returns assignment with comments''' assignment_id = request.data.get('assignment_id') try: assignment = Assignment.objects.get(id=assignment_id) if request.user.username == assignment.classroom.creator.username or request.user in assignment.classroom.moderators.all() or request.user in assignment.classroom.students.all(): comment = Comment() if request.data.get('comment_id'): try: comment.parent = Comment.objects.get(id=request.data.get('comment_id')) comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() except Exception as e: return Response({ "error": "Comment query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST) else: comment.parent = None comment.commenter = request.user comment.comment_text = request.data.get('content') comment.save() assignment.comments.add(comment) assignment.save() allComments = assignment.comments.all() serialized_comments = CommentSerializer(allComments, many=True).data serialized_assignment = AssignmentSerializer(assignment, many=False).data serialized_assignment['comments'] = serialized_comments return Response(serialized_assignment) else: return Response({ "error": "You aren't enrolled in this classroom." }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response({ "error": "Assignment query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): '''To get all comments on an assignment Takes assignment_id Returns all comments with assignment''' assignment_id = request.GET.get('assignment_id') try: assignment = Assignment.objects.get(id=assignment_id) if request.user.username == assignment.classroom.creator.username or request.user in assignment.classroom.moderators.all() or request.user in assignment.classroom.students.all(): allComments = assignment.comments.all() serialized_comments = CommentSerializer(allComments, many=True).data serialized_assignment = AssignmentSerializer(assignment, many=False).data serialized_assignment['comments'] = serialized_comments return Response(serialized_assignment) else: return Response({ "error": "You aren't enrolled in this classroom." }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response({ "error": "Assignment query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): '''To get all comments on a resource Takes resource_id Returns resource with all comments''' try: resource = Resource.objects.get(id=request.GET.get('resource_id')) if request.user.username == resource.classroom.creator or request.user in resource.classroom.moderators.all() or request.user in resource.classroom.students.all(): allComments = resource.comments.all() serialized_comments = CommentSerializer(allComments, many=True).data serialized_resource = ResourceSerializer(resource, many=False).data serialized_resource['comments'] = serialized_comments return Response(serialized_resource) else: return Response({ "error": "You can't see this resource." }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: print(e) return Response({ "error": "Resource query doesn't exists." }, status=status.HTTP_400_BAD_REQUEST)