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

import logging

from django.shortcuts import get_object_or_404

from rest_framework.decorators import api_view
from rest_framework.response import Response

from ..serializers import CategorySerializer
from app.models import TheUser, Category

logger = logging.getLogger('changes')


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
def all_categories(request):
    """
    Generates the categories list.
    """
    the_user = get_object_or_404(TheUser, auth_token=request.data.get('user_token'))
    categories = Category.objects.all().order_by('category_name')

    return Response({'status': 200,
                     'detail': 'successful',
                     'data': [CategorySerializer(category, context={'request': request}).data
                              for category in categories]})


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
def selected_category(request, category_id):
    """
    Returns books from selected category.
    """
    pass<commit_msg>Implemented books output for selected category API.<commit_after># -*- coding: utf-8 -*-

import logging

from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404

from rest_framework.decorators import api_view
from rest_framework.response import Response

from ..serializers import CategorySerializer, BookSerializer
from app.models import TheUser, Category, Book

OUTPUT_BOOKS_PER_PAGE = 20

logger = logging.getLogger('changes')


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
def all_categories(request):
    """
    Generates the categories list.
    """
    the_user = get_object_or_404(TheUser, auth_token=request.data.get('user_token'))
    categories = Category.objects.all().order_by('category_name')

    return Response({'status': 200,
                     'detail': 'successful',
                     'data': [CategorySerializer(category, context={'request': request}).data
                              for category in categories]})


# ----------------------------------------------------------------------------------------------------------------------
@api_view(['POST'])
def selected_category(request, category_id):
    """
    Returns books from selected category.
    """
    the_user = get_object_or_404(TheUser, auth_token=request.data.get('user_token'))

    category = Category.objects.get(id=category_id)
    books = Book.objects.filter(id_category=category).order_by('book_name')
    filtered_books = Book.exclude_private_books(request.user, books)

    paginator = Paginator(filtered_books, OUTPUT_BOOKS_PER_PAGE)
    page = paginator.page(request.data.get('page'))
    next_page = page.has_next()
    page_books = page.object_list

    return Response({'status': 200,
                     'detail': 'successful',
                     'data': {'books': [BookSerializer(book).data for book in page_books],
                              'next_page': page.next_page_number() if next_page else next_page}})
{'fraction_non_alphanumeric': 0.24255449800429843, 'fraction_numerical': 0.008596868283696654, 'mean_word_length': 1.980786825251601, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7360017', 'n_tokens_mistral': 807, 'n_tokens_neox': 772, 'n_words': 175}