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))

@@ -74,16 +74,24 @@
 ceptions
+, status
 %0Afrom .m
@@ -242,16 +242,158 @@
  %7D,%0A%7D%0A%0A%0A
+class LimitExceededError(exceptions.APIException):%0A    status_code = status.HTTP_402_PAYMENT_REQUIRED%0A    default_detail = 'limit exceeded'%0A%0A%0A
 def chec
@@ -754,61 +754,45 @@
 ise 
-exceptions.PermissionDenied('can not create more than
+LimitExceededError('exceeded limit of
  %25d 
@@ -806,25 +806,16 @@
 %25s' %25 %5C%0A
-         
         

return http 402 on exceeded limits