X-Git-Url: https://git.mdrn.pl/prawokultury.git/blobdiff_plain/a55d79e4c27c893841712145e38d4a4a61e04b20..ac2bf5fce0bddb3d687562c70dc7f4b90758b95d:/contact/models.py?ds=sidebyside diff --git a/contact/models.py b/contact/models.py index 21d8405..37096ae 100644 --- a/contact/models.py +++ b/contact/models.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +import random +import string from django.core.files.storage import FileSystemStorage from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -8,10 +10,11 @@ from . import app_settings class Contact(models.Model): created_at = models.DateTimeField(_('submission date'), auto_now_add=True) - ip = models.IPAddressField(_('IP address')) + ip = models.IPAddressField(_('IP address'), default='127.0.0.1') contact = models.CharField(_('contact'), max_length=128) form_tag = models.CharField(_('form'), max_length=32, db_index=True) body = JSONField(_('body')) + key = models.CharField(_('key'), max_length=64, db_index=True, blank=True) class Meta: ordering = ('-created_at',) @@ -21,6 +24,18 @@ class Contact(models.Model): def __unicode__(self): return unicode(self.created_at) + def save(self, *args, **kwargs): + if not self.key: + self.key = self.generate_key() + return super(Contact, self).save(*args, **kwargs) + + @classmethod + def generate_key(cls): + key = '' + while not key or key in [record['key'] for record in cls.objects.values('key')]: + key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30)) + return key + class Attachment(models.Model): contact = models.ForeignKey(Contact)