def modify_comment(self, request, id, comment_id): logger.info('updating of comment') if request.user.role == "Admin": return Response({}, status=status.HTTP_400_BAD_REQUEST) serializer = CommentSerializer(instance=Comment.objects.get(id=comment_id, user=request.user, journal_id=id), data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK)
def write_comment(self, request, id): logger.info('post method of a comment') if request.user.role == "Admin": return Response({}, status=status.HTTP_400_BAD_REQUEST) serializer = CommentSerializer(data=request.data, context={"user": request.user, "journal": Book.objects.get(id=id)}) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def post(self, request, post_id, format=None): post = get_object_or_404(Post, id=post_id) request_data = request.data request_data['post'] = post.id serializer = CommentSerializer(data=request_data) # print(serializer.post) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, post_id, format=None): comments = Comment.objects.filter(post__id=post_id) comments_serializer = CommentSerializer(comments, many=True) return Response(data=comments_serializer.data, status=status.HTTP_200_OK)
def create_comment(request): comment = Comment.objects.create( content=request.data['content'], task=Task.objects.get(id=request.data['task']), creator=request.user) serializer = CommentSerializer(comment) return Response(serializer.data)
def get_user_home(request, username): try: user = CustomUser.objects.get(username=username) except: return Response({'status': 'error', 'message': 'No such user found'}) # Get github data try: profile_data = requests.get(user.extra_detail_url).text except: profile_data = "{\"error\":\"No user found\"}" # Get asked questions asked_questions = QuestionSerializer(user.asked_questions.all(), many=True).data # Get all anwers answered = AnswerSerializer(user.written_answers.all(), many=True).data # Get all comments written_comments = CommentSerializer(user.written_comments.all(), many=True).data # Get all upvoted questions, comments upvoted_answers = AnswerSerializer(user.upvoted_answers.all(), many=True).data upvoted_comments = CommentSerializer(user.upvoted_comments.all(), many=True).data requested_answers = [] if request.user and request.user == user: requested_answers = AnswerSerializer(user.answer_requests.all(), many=True).data subscribed_genres = GenreSerializer(user.subscribed_genres.all(), many=True).data followed_topics = TopicSerializer(user.followed_topics.all(), many=True).data return Response({ 'status': 'success', 'data': { 'profile_data': loads(profile_data), 'asked_questions': asked_questions, 'answered': answered, 'written_comments': written_comments, 'upvoted_answers': upvoted_answers, 'upvoted_comments': upvoted_comments, 'requested_answers': requested_answers, 'subscribed_genres': subscribed_genres, 'followed_topics': followed_topics } })
def get(self, request, post_id, comment_id, format=None): comment = Comment.objects.filter(post__id=post_id, id=comment_id) if comment: comment_serializer = CommentSerializer(comment[0]) return Response(data=comment_serializer.data, status=status.HTTP_200_OK) return Response(status=status.HTTP_404_NOT_FOUND)
def get(self, request, comment_id): try: comment = Comment.objects.get(id = comment_id) except core.models.comment.DoesNotExist: return Response({'status' : 'error', 'message' : 'Comment does not exist'}) return Response({ 'status' : 'success', 'data' : CommentSerializer(comment).data })
def put(self, request, comment_id): try: comment = Comment.objects.get(id = comment_id) except core.models.Comment.DoesNotExist: return Response({'status' : 'error', 'message' : 'Comment does not exist'}) updated_comment = request.data if updated_comment.get('comment', False) and request.user != comment.author : return Response({'status' : 'error', 'message' : 'Not authenticated to change this comment'}) updated_comment['current_user'] = request.user.username serializer = CommentSerializer(comment) serializer.update(comment, updated_comment, request.user) return Response({ 'status' : 'success', 'message' : 'Comment updated succesfully', 'answer_id' : serializer.data['id'] })
def comments(self, request, id): logger.info('get comments') book = Book.objects.get(id=id) try: comments = book.comments.all() except Comment.DoesNotExist: return Response({}) serializer = CommentSerializer(comments, many=True) return Response(serializer.data)
def get(self, request, pk, format=None): post = get_object_or_404(Post, pk=pk) comments = Comment.objects.filter(post=post) comments_serializer = CommentSerializer(comments, many=True) post_serializer = PostSerializer(post) data = { 'post': post_serializer.data, 'comments': comments_serializer.data } return Response(data=data, status=status.HTTP_200_OK)
def get(self, request, format=None): posts_data = [] for post in Post.objects.all(): comments = Comment.objects.filter(post=post) comments_serializer = CommentSerializer(comments, many=True) post_serializer = PostSerializer(post) posts_data.append({ 'post': post_serializer.data, 'comments': comments_serializer.data }) return Response(data=posts_data, status=status.HTTP_200_OK)
def retrieve_comment(self, request, id, comment_id): logger.info('retrieve of a comment') queryset = Comment.objects.all() task = get_object_or_404(queryset, id=comment_id, user=request.user, journal_id=id) serializer = CommentSerializer(task) return Response(serializer.data)
def comments(self, request, pk): comments = Comment.objects.all().filter(post=pk).order_by("-created_at") serializer = CommentSerializer(comments, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def comment(request): if request.method == 'POST': return CommentSerializer.save(user=request.user)
def get_comment_by_task(request): task_id = request.query_params.get('task') comments = Comment.objects.get(task_id=task_id) serializer = CommentSerializer(comments) return Response(serializer.data)
def get_comments(request): queryset = Comment.objects.all() serializer = CommentSerializer(queryset, many=True) return Response(serializer.data)
def list(self, request, *args, **kwargs): # return Response({'something': 'my custom JSON'}) comment = Comment(email='*****@*****.**', content='foo bar') serializer = CommentSerializer(comment) return Response(serializer.data)
def get(self, request, habit_record_id, format=None): comments = Comment.objects.filter(habit_record=HabitRecord.objects.get( id=habit_record_id)) serializer = CommentSerializer(comments, many=True) return Response(serializer.data)