import binascii
import os

from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _


class HMACKey(models.Model):
    """
    The default HMACKey model that can auto generate a
    key/secret for HMAC Auth via a signal
    """
    def generate_key():
        """
        Returns a 40 character hex string based on binary random data
        """
        return binascii.hexlify(os.urandom(20)).decode()

    key = models.CharField(
        _("Key"), primary_key=True, max_length=40, default=generate_key)
    secret = models.CharField(
        _("Secret"), max_length=40, default=generate_key)
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, related_name='hmac_key',
        on_delete=models.CASCADE, verbose_name=_("User")
    )
    nonce = models.BigIntegerField(default=1)
    created = models.DateTimeField(_("Created"), auto_now_add=True)

    class Meta:
        # Only create a DB table for this Model if this app is registered
        abstract = 'rest_framework_hmac.hmac_key' \
            not in settings.INSTALLED_APPS
        verbose_name = _("HMACKey")
        verbose_name_plural = _("HMACKey")

    def __str__(self):
        return self.key

[((28, 12, 28, 45), 'django.db.models.BigIntegerField', 'models.BigIntegerField', (), '', False, 'from django.db import models\n'), ((21, 8, 21, 16), 'django.utils.translation.ugettext_lazy', '_', ({(21, 10, 21, 15): '"""Key"""'}, {}), "('Key')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((23, 8, 23, 19), 'django.utils.translation.ugettext_lazy', '_', ({(23, 10, 23, 18): '"""Secret"""'}, {}), "('Secret')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((29, 35, 29, 47), 'django.utils.translation.ugettext_lazy', '_', ({(29, 37, 29, 46): '"""Created"""'}, {}), "('Created')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((35, 23, 35, 35), 'django.utils.translation.ugettext_lazy', '_', ({(35, 25, 35, 34): '"""HMACKey"""'}, {}), "('HMACKey')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((36, 30, 36, 42), 'django.utils.translation.ugettext_lazy', '_', ({(36, 32, 36, 41): '"""HMACKey"""'}, {}), "('HMACKey')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((26, 47, 26, 56), 'django.utils.translation.ugettext_lazy', '_', ({(26, 49, 26, 55): '"""User"""'}, {}), "('User')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((18, 32, 18, 46), 'os.urandom', 'os.urandom', ({(18, 43, 18, 45): '(20)'}, {}), '(20)', False, 'import os\n')]