def patch(self, request, post_uuid, post_comment_id): request_data = self._get_request_data(request, post_uuid, post_comment_id) serializer = UpdatePostCommentSerializer(data=request_data) serializer.is_valid(raise_exception=True) data = serializer.validated_data comment_text = data.get('text') post_uuid = data.get('post_uuid') post_comment_id = data.get('post_comment_id') user = request.user post_id = get_post_id_for_post_uuid(post_uuid) with transaction.atomic(): post_comment = user.update_comment_with_id_for_post_with_id( post_comment_id=post_comment_id, post_id=post_id, text=comment_text) post_comment_serializer = EditPostCommentSerializer( post_comment, context={"request": request}) return Response(post_comment_serializer.data, status=status.HTTP_200_OK)
def get(self, request, post_uuid): request_data = request.data.copy() request_data['post_uuid'] = post_uuid serializer = GetPostSerializer(data=request_data) serializer.is_valid(raise_exception=True) data = serializer.validated_data post_uuid = data.get('post_uuid') user = request.user post_id = get_post_id_for_post_uuid(post_uuid) try: preview_link_data = user.get_preview_link_data_for_post_with_id( post_id) except URLNotFound: return ApiMessageResponse( _('The linked url associated for preview was not found.'), status=status.HTTP_400_BAD_REQUEST) except URLUnreachable: return ApiMessageResponse( _('The linked url associated for preview was not reachable.'), status=status.HTTP_400_BAD_REQUEST) return Response(preview_link_data, status=status.HTTP_200_OK)
def post(self, request, post_uuid): request_data = request.data.copy() request_data['post_uuid'] = post_uuid serializer = TranslatePostSerializer(data=request_data) serializer.is_valid(raise_exception=True) user = request.user data = serializer.validated_data post_uuid = data.get('post_uuid') post_id = get_post_id_for_post_uuid(post_uuid) try: post, translated_text = user.translate_post_with_id( post_id=post_id) except UnsupportedLanguagePairException: return ApiMessageResponse( _('Translation between these languages is not supported.'), status=status.HTTP_400_BAD_REQUEST) except TranslationClientError: return ApiMessageResponse( _('Translation service returned an error'), status=status.HTTP_500_INTERNAL_SERVER_ERROR) except MaxTextLengthExceededError: return ApiMessageResponse( _('Max length of translation text exceeded.'), status=status.HTTP_400_BAD_REQUEST) return Response({'translated_text': translated_text}, status=status.HTTP_200_OK)
def delete(self, request, post_uuid): request_data = self._get_request_data(request, post_uuid) serializer = DeletePostSerializer(data=request_data) serializer.is_valid(raise_exception=True) data = serializer.validated_data post_uuid = data.get('post_uuid') user = request.user post_id = get_post_id_for_post_uuid(post_uuid) with transaction.atomic(): user.delete_post_with_id(post_id) return Response({ 'message': _('Post deleted') }, status=status.HTTP_200_OK)
def get(self, request, post_uuid): request_data = self._get_request_data(request, post_uuid) serializer = GetPostSerializer(data=request_data) serializer.is_valid(raise_exception=True) data = serializer.validated_data post_uuid = data.get('post_uuid') user = request.user post_id = get_post_id_for_post_uuid(post_uuid) post = user.get_post_with_id(post_id) post_comments_serializer = GetPostPostSerializer(post, context={"request": request}) return Response(post_comments_serializer.data, status=status.HTTP_200_OK)
def post(self, request, post_uuid): serializer = MutePostSerializer(data={ 'post_uuid': post_uuid }) serializer.is_valid(raise_exception=True) data = serializer.validated_data post_uuid = data.get('post_uuid') user = request.user post_id = get_post_id_for_post_uuid(post_uuid) with transaction.atomic(): muted_post = user.mute_post_with_id(post_id=post_id) post_comments_serializer = GetPostPostSerializer(muted_post, context={"request": request}) return Response(post_comments_serializer.data, status=status.HTTP_200_OK)
def post(self, request, post_uuid): request_data = request.data.copy() request_data['post_uuid'] = post_uuid serializer = OpenPostSerializer(data=request_data) serializer.is_valid(raise_exception=True) user = request.user data = serializer.validated_data post_uuid = data.get('post_uuid') post_id = get_post_id_for_post_uuid(post_uuid) with transaction.atomic(): post = user.open_post_with_id(post_id=post_id) post_serializer = OpenClosePostSerializer(post, context={'request': request}) return Response(post_serializer.data, status=status.HTTP_200_OK)
def patch(self, request, post_uuid): request_data = request.data.dict() request_data['post_uuid'] = post_uuid serializer = EditPostSerializer(data=request_data, context={"request": request}) serializer.is_valid(raise_exception=True) data = serializer.validated_data text = data.get('text', None) post_uuid = data.get('post_uuid') user = request.user post_id = get_post_id_for_post_uuid(post_uuid) with transaction.atomic(): post = user.update_post(post_id=post_id, text=text) post_serializer = AuthenticatedUserEditPostSerializer(post, context={"request": request}) return Response(post_serializer.data, status=status.HTTP_200_OK)