# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


from django.utils.translation import get_language, to_locale
from wagtail.wagtailcore.models import Page
from .models import Token
import factory
    """

    class Meta:
        abstract = True
        strategy = factory.BUILD_STRATEGY

    title = factory.Faker('sentence', locale=locale)
    slug = factory.LazyAttributeSequence(
        lambda o, n: '{type}_{n}'.format(
            type=o._LazyStub__model_class._meta.model.__name__.lower(),
            n=n,
        )
    )

    @classmethod
    def _setup_next_sequence(cls):
        """
        Returns the last ID for persistant sequence (used by slug and so on).

        IMPORTANT: we should always have pages, at least root and timeline.
        """
        return (
            Page.objects
            .values('pk')
            .order_by('-pk')[:1]
        )[0]['pk'] + 1


class TokenFactory(factory.DjangoModelFactory):
    class Meta:

# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


# Path: privagal/core/factories.py
from django.utils.translation import get_language, to_locale
from wagtail.wagtailcore.models import Page
from .models import Token
import factory

    """

    class Meta:
        abstract = True
        strategy = factory.BUILD_STRATEGY

    title = factory.Faker('sentence', locale=locale)
    slug = factory.LazyAttributeSequence(
        lambda o, n: '{type}_{n}'.format(
            type=o._LazyStub__model_class._meta.model.__name__.lower(),
            n=n,
        )
    )

    @classmethod
    def _setup_next_sequence(cls):
        """
        Returns the last ID for persistant sequence (used by slug and so on).

        IMPORTANT: we should always have pages, at least root and timeline.
        """
        return (
            Page.objects
            .values('pk')
            .order_by('-pk')[:1]
        )[0]['pk'] + 1


class TokenFactory(factory.DjangoModelFactory):
    class Meta:

        model = Token