<commit_before>from django.contrib.auth.models import User
from rest_framework import exceptions
from .models import Notebook, Note, Task


LIMITS = {
    User: {
        Notebook: 8,
        Task: 125,
    },
    Notebook: {
        Note: 250,
    },
}


def check_limits(parent, child_type):
    parent_limits = LIMITS.get(parent.__class__)
    if not parent_limits:
        return

    limit = parent_limits.get(child_type)
    if limit is None:
        return

    child_set_name = child_type._meta.verbose_name + '_set'
    child_set = getattr(parent, child_set_name)
    if child_set.count() >= limit:
        raise exceptions.PermissionDenied('can not create more than %d %s per %s' % \
                                          (limit, child_type._meta.verbose_name_plural, parent._meta.verbose_name))<commit_msg>return http 402 on exceeded limits<commit_after>from django.contrib.auth.models import User
from rest_framework import exceptions, status
from .models import Notebook, Note, Task


LIMITS = {
    User: {
        Notebook: 8,
        Task: 125,
    },
    Notebook: {
        Note: 250,
    },
}


class LimitExceededError(exceptions.APIException):
    status_code = status.HTTP_402_PAYMENT_REQUIRED
    default_detail = 'limit exceeded'


def check_limits(parent, child_type):
    parent_limits = LIMITS.get(parent.__class__)
    if not parent_limits:
        return

    limit = parent_limits.get(child_type)
    if limit is None:
        return

    child_set_name = child_type._meta.verbose_name + '_set'
    child_set = getattr(parent, child_set_name)
    if child_set.count() >= limit:
        raise LimitExceededError('exceeded limit of %d %s per %s' % \
                                 (limit, child_type._meta.verbose_name_plural, parent._meta.verbose_name))
{'fraction_non_alphanumeric': 0.08892617449664429, 'fraction_numerical': 0.011185682326621925, 'mean_word_length': 2.7426778242677825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3250675', 'n_tokens_mistral': 569, 'n_tokens_neox': 534, 'n_words': 132}