# Path: django_core/db/models/managers.py
# class TokenManager(BaseManager):
#     """Manager Mixin for tokens."""
# 
#     def get_by_token(self, token, **kwargs):
#         """Get by token."""
#         return self.get_or_none(token=token, **kwargs)
# 
#     def get_by_token_or_404(self, token, **kwargs):
#         group = self.get_by_token(token, **kwargs)
# 
#         if group:
#             return group
# 
#         raise Http404
# 
#     def get_next_token(self, length=15, **kwargs):
#         """Gets the next available token.
# 
#         :param length: length of the token
#         :param kwargs: additional filter criteria to check for when looking for
#             a unique token.
# 
#         """
#         return self.get_available_tokens(count=1,
#                                          token_length=length,
#                                          **kwargs)[0]
# 
#     def get_available_tokens(self, count=10, token_length=15, **kwargs):
#         """Gets a list of available tokens.
# 
#         :param count: the number of tokens to return.
#         :param token_length: the length of the tokens.  The higher the number
#             the easier it will be to return a list.  If token_length == 1
#             there's a strong probability that the enough tokens will exist in
#             the db.
# 
#         """
#         # This is the number of extra tokens to try and retrieve so calls to
#         # the db can be limited
#         token_buffer = int(math.ceil(count * .05))
# 
#         if token_buffer < 5:
#             token_buffer = 5
# 
#         available = set([])
# 
#         while True:
#             tokens = [random_alphanum(length=token_length)
#                       for t in range(count + token_buffer)]
#             db_tokens = self.filter(token__in=tokens).values_list('token',
#                                                                   flat=True)
#             available.update(set(tokens).difference(db_tokens))
# 
#             if len(available) >= count:
#                 return list(available)[:count]
# 
# Path: django_core/utils/list_utils.py
# def make_obj_list(obj_or_objs):
#     """This method will take an object or list of objects and ensure a list is
#     returned.
# 
#     Example:
# 
#     >>> make_obj_list('hello')
#     ['hello']
#     >>> make_obj_list(['hello', 'world'])
#     ['hello', 'world']
#     >>> make_obj_list(None)
#     []
# 
#     """
#     if not obj_or_objs:
#         return []
# 
#     if not isinstance(obj_or_objs, (list, tuple)):
#         return [obj_or_objs]
# 
#     return obj_or_objs


from django.db import models
from django_core.db.models import TokenManager
from django_core.utils.list_utils import make_obj_list
from __future__ import unicode_literals



class AbstractTokenModel(models.Model):
    """Abstract class for token logic."""
    token = models.CharField(max_length=100, db_index=True, unique=True)
    token_length = 15

# Path: django_core/db/models/managers.py
# class TokenManager(BaseManager):
#     """Manager Mixin for tokens."""
# 
#     def get_by_token(self, token, **kwargs):
#         """Get by token."""
#         return self.get_or_none(token=token, **kwargs)
# 
#     def get_by_token_or_404(self, token, **kwargs):
#         group = self.get_by_token(token, **kwargs)
# 
#         if group:
#             return group
# 
#         raise Http404
# 
#     def get_next_token(self, length=15, **kwargs):
#         """Gets the next available token.
# 
#         :param length: length of the token
#         :param kwargs: additional filter criteria to check for when looking for
#             a unique token.
# 
#         """
#         return self.get_available_tokens(count=1,
#                                          token_length=length,
#                                          **kwargs)[0]
# 
#     def get_available_tokens(self, count=10, token_length=15, **kwargs):
#         """Gets a list of available tokens.
# 
#         :param count: the number of tokens to return.
#         :param token_length: the length of the tokens.  The higher the number
#             the easier it will be to return a list.  If token_length == 1
#             there's a strong probability that the enough tokens will exist in
#             the db.
# 
#         """
#         # This is the number of extra tokens to try and retrieve so calls to
#         # the db can be limited
#         token_buffer = int(math.ceil(count * .05))
# 
#         if token_buffer < 5:
#             token_buffer = 5
# 
#         available = set([])
# 
#         while True:
#             tokens = [random_alphanum(length=token_length)
#                       for t in range(count + token_buffer)]
#             db_tokens = self.filter(token__in=tokens).values_list('token',
#                                                                   flat=True)
#             available.update(set(tokens).difference(db_tokens))
# 
#             if len(available) >= count:
#                 return list(available)[:count]
# 
# Path: django_core/utils/list_utils.py
# def make_obj_list(obj_or_objs):
#     """This method will take an object or list of objects and ensure a list is
#     returned.
# 
#     Example:
# 
#     >>> make_obj_list('hello')
#     ['hello']
#     >>> make_obj_list(['hello', 'world'])
#     ['hello', 'world']
#     >>> make_obj_list(None)
#     []
# 
#     """
#     if not obj_or_objs:
#         return []
# 
#     if not isinstance(obj_or_objs, (list, tuple)):
#         return [obj_or_objs]
# 
#     return obj_or_objs


# Path: django_core/db/models/mixins/tokens.py
from django.db import models
from django_core.db.models import TokenManager
from django_core.utils.list_utils import make_obj_list

from __future__ import unicode_literals



class AbstractTokenModel(models.Model):
    """Abstract class for token logic."""
    token = models.CharField(max_length=100, db_index=True, unique=True)
    token_length = 15

    objects = TokenManager()