<commit_before># -*- coding: utf-8 -*-

import json
import logging

from django.db import transaction
from django.shortcuts import get_object_or_404

from rest_framework import status
from rest_framework.decorators import api_view, parser_classes
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response

from ..serializers.model_serializers import BookSerializer
from ..serializers.request_serializers import (UploadBookRequest,
                                               GenerateAuthorsRequest,
                                               GenerateBooksRequest,
                                               GenerateLanguagesRequest)
from ..utils import invalid_data_response
from app.models import Author, AddedBook, Book, TheUser, Language
from app.tasks import compress_pdf_task

logger = logging.getLogger('changes')


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
@parser_classes((MultiPartParser,))
def upload_book(request):
    """
    Handles request and saves uploaded book.
    """
    request_serializer = UploadBookRequest(data=request.data)

    if request_serializer.is_valid():
        with transaction.atomic():
            user = get_object_or_404(TheUser, auth_token=request.data.get('user_token'))
            rel_objects = Book.get_related_objects_create_api(user, request.data)

            book = Book.objects.create(book_name=request.data.get('book_name'),
                                       id_author=rel_objects['author'],
                                       id_category=rel_objects['category'],
                                       description=request.data.get('about'),
                                       language=rel_objects['lang'],
                                       photo=request.data['photo'],
                                       book_file=request.data['book_file'],
                                       who_added=user,
                                       private_book=json.loads(request.data.get('private_book')))

            AddedBook.objects.create(id_user=user, id_book=book)

            logger.info("User '{}' uploaded book with id: '{}' and name: '{}' on category: '{}'."
                        .format(user, book.id, book.book_name, rel_objects['category']))

            compress_pdf_task.delay(book.book_file.path)

            return Response({'detail': 'successful',
                             'data': {'id': book.id}},
                            status=status.HTTP_200_OK)
    else:
        return invalid_data_response(request_serializer)


# ----------------------------------------------------------------------------------------------------------------------<commit_msg>After upload book returns the Book object at API call<commit_after># -*- coding: utf-8 -*-

import json
import logging

from django.db import transaction
from django.shortcuts import get_object_or_404

from rest_framework import status
from rest_framework.decorators import api_view, parser_classes
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response

from ..serializers.model_serializers import BookSerializer
from ..serializers.request_serializers import (UploadBookRequest,
                                               GenerateAuthorsRequest,
                                               GenerateBooksRequest,
                                               GenerateLanguagesRequest)
from ..utils import invalid_data_response
from app.models import Author, AddedBook, Book, TheUser, Language
from app.tasks import compress_pdf_task

logger = logging.getLogger('changes')


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
@parser_classes((MultiPartParser,))
def upload_book(request):
    """
    Handles request and saves uploaded book.
    """
    request_serializer = UploadBookRequest(data=request.data)

    if request_serializer.is_valid():
        with transaction.atomic():
            user = get_object_or_404(TheUser, auth_token=request.data.get('user_token'))
            rel_objects = Book.get_related_objects_create_api(user, request.data)

            book = Book.objects.create(book_name=request.data.get('book_name'),
                                       id_author=rel_objects['author'],
                                       id_category=rel_objects['category'],
                                       description=request.data.get('about'),
                                       language=rel_objects['lang'],
                                       photo=request.data['photo'],
                                       book_file=request.data['book_file'],
                                       who_added=user,
                                       private_book=json.loads(request.data.get('private_book')))

            AddedBook.objects.create(id_user=user, id_book=book)

            logger.info("User '{}' uploaded book with id: '{}' and name: '{}' on category: '{}'."
                        .format(user, book.id, book.book_name, rel_objects['category']))

            compress_pdf_task.delay(book.book_file.path)

            return Response({'detail': 'successful',
                             'data': {'book': BookSerializer(book).data}},
                            status=status.HTTP_200_OK)
    else:
        return invalid_data_response(request_serializer)


# ----------------------------------------------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.16796598157335224, 'fraction_numerical': 0.003543586109142452, 'mean_word_length': 1.5635785649409628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4713907', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1224, 'n_words': 247}