from django.db import models
from django.contrib.auth.models import User
import datetime

def default_expiry_time():
  return datetime.datetime.now() + datetime.timedelta(days=1)

def random_string():
  return User.objects.make_random_password(16)

class Credentials(models.Model):
  "Keeps track of issued MAC credentials"
  user = models.ForeignKey(User)
  expiry = models.DateTimeField("Expires On", default=default_expiry_time)
  identifier = models.CharField("MAC Key Identifier", max_length=16, default=random_string)
  key = models.CharField("MAC Key", max_length=16, default=random_string)

  def __unicode__(self):
    return u"{0}:{1}".format(self.identifier, self.key)
  
  @property
  def expired(self):
    """Returns whether or not the credentials have expired"""
    if self.expiry < datetime.datetime.now():
      return True
    return False
  
class Nonce(models.Model):
  """Keeps track of any NONCE combinations that we have used"""
  nonce = models.CharField("NONCE", max_length=16, null=True, blank=True)
  timestamp = models.DateTimeField("Timestamp", auto_now_add=True)
  credentials = models.ForeignKey(Credentials)
from django.db import models
from django.contrib.auth.models import User
import datetime

def default_expiry_time():
  return datetime.datetime.now() + datetime.timedelta(days=1)

def random_string():
  return User.objects.make_random_password(16)

class Credentials(models.Model):
  "Keeps track of issued MAC credentials"
  user = models.ForeignKey(User)
  expiry = models.DateTimeField("Expires On", default=default_expiry_time)
  identifier = models.CharField("MAC Key Identifier", max_length=16, default=random_string)
  key = models.CharField("MAC Key", max_length=16, default=random_string)

  def __unicode__(self):
    return u"{0}:{1}".format(self.identifier, self.key)
  
  @property
  def expired(self):
    """Returns whether or not the credentials have expired"""
    if self.expiry < datetime.datetime.now():
      return True
    return False
  
class Nonce(models.Model):
  """Keeps track of any NONCE combinations that we have used"""
  nonce = models.CharField("NONCE", max_length=16, null=True, blank=True)
  timestamp = models.DateTimeField("Timestamp", auto_now_add=True)
  credentials = models.ForeignKey(Credentials)

  def __unicode__(self):
    timestamp = self.timestamp - datetime.datetime(1970,1,1)
    timestamp = timestamp.days * 24 * 3600 + timestamp.seconds
    return u"[{0}/{1}/{2}]".format(self.nonce, timestamp, self.credentials.identifier)
Add a unicode method for the Nonces
Add a unicode method for the Nonces
