from __future__ import unicode_literals

import base64
from cStringIO import StringIO

import base32_crockford

import qrcode

from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:  # Django version < 1.5
    from django.contrib.auth.models import User


@python_2_unicode_compatible
class RedirectURL(models.Model):
    """
    A ``RedirectURL`` represents a redirection mapping between a short
    URL and the full destination URL. Several additional values are
    stored with related data and usage statistics.
    """
    campaign = models.CharField(_('campaign'), max_length=64, blank=True,
                                help_text=_('The individual campaign name, slogan, promo code, etc. for a product'))
    content = models.CharField(_('content'), max_length=64, blank=True,
                               help_text=_('Used to differentiate similar content, or links within the same ad'))
    created = models.DateTimeField(_('created'), auto_now_add=True, editable=False)
    creator = models.ForeignKey(User, verbose_name=_('creator'), editable=False)
    description = models.TextField(_('description'), blank=True)
    hits = models.IntegerField(_('hits'), default=0, editable=False)
    last_used = models.DateTimeField(_('last used'), editable=False, blank=True, null=True)
    medium = models.CharField(_('medium'), max_length=64, blank=True,
                              help_text=_('The advertising or marketing medium, e.g.: postcard, banner, email newsletter'))
    url = models.URLField(_('target url'), help_text=_('The target URL to which the short URL redirects'))

    class Meta:
        verbose_name = _('Redirect URL')
        verbose_name_plural = _('Redirect URLs')

    def __str__(self):
        return self.url

    @property
    def key(self):
        return base32_crockford.encode(self.pk)

    def get_absolute_url(self):
        return reverse('deflect-redirect', args=[self.key])

    def short_url(self):
        """
        Return the complete short URL for the current redirect.
        """
        url_base = 'http://%s' % Site.objects.get_current().domain
        return url_base + self.get_absolute_url()

    def qr_code(self):
        """
        Return an HTML img tag containing an inline base64 encoded
        representation of the short URL as a QR code.
        """
        png_stream = StringIO()
        img = qrcode.make(self.short_url())
        img.save(png_stream)
        png_base64 = base64.b64encode(png_stream.getvalue())
        png_stream.close()
        return '<img src="data:image/png;base64,%s" />' % png_base64
    qr_code.allow_tags = True

@@ -301,16 +301,54 @@
 patible%0A
+from django.utils.timezone import now%0A
 from dja
@@ -1270,27 +1270,8 @@
 d'),
- auto_now_add=True,
  edi
@@ -2055,35 +2055,63 @@
     
-@property%0A    def key(
+def save(self, *args, **kwargs):%0A        if not 
 self
-)
+.id
 :%0A  
@@ -2120,46 +2120,86 @@
     
-return base32_crockford.encode(self.pk
+    self.created = now()%0A        super(RedirectURL, self).save(*args, **kwargs
 )%0A%0A 
@@ -2290,16 +2290,98 @@
 .key%5D)%0A%0A
+    @property%0A    def key(self):%0A        return base32_crockford.encode(self.pk)%0A%0A
     def 

Use a custom save method instead of auto_now_add