Source

django-emailform / emailform / models.py

Full commit
from __future__ import absolute_import

import datetime
from operator import itemgetter

from django.contrib.sites.models import Site
from django.db import models
from django.utils.translation import ugettext_lazy as _

from .util import ChoiceEnum
from . import settings as local_settings

try:
    from positions.fields import PositionField
except ImportError:
    logging.warn('positions not installed. '
                 'Will just use integers for position fields.')
    PositionField = None


class LiveContactFormManager(models.Manager):

    def get_query_set(self):
        now = datetime.datetime.now()
        return super(LiveContactFormManager, self).get_query_set().filter(
            is_published=True,
            starts_at__lte=now)


class ContactForm(models.Model):

    class Meta:
        ordering = ('-starts_at',)

    title = models.CharField(max_length=80)
    slug = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    starts_at = models.DateTimeField(default=datetime.datetime.now)
    is_published = models.BooleanField(default=False)
    thanks_message = models.TextField(default="Thanks for entering!")
    email = models.CharField(
        blank=True,
        help_text=("Send a notification to this email whenever someone "
                   "submits an entry to this contact form",),
        max_length=300)
    site = models.ForeignKey(Site)

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ('contact_form_detail', (), {'slug': self.slug})

    objects = models.Manager()
    live = LiveContactFormManager()

OPTION_TYPE_CHOICES = ChoiceEnum(sorted([('char', 'Text Field'),
                                         ('email', 'Email Field'),
                                         ('photo', 'Photo Upload'),
                                         ('video', 'Video Link'),
                                         ('location', 'Location Field'),
                                         ('integer', 'Integer'),
                                         ('float', 'Float'),
                                         ('bool', 'Boolean'),
                                         ('text', 'Text Area'),
                                         ('select', 'Select One Choice'),
                                         ('radio', 'Radio List'),
                                         ('checkbox', 'Checkbox List'),
                                         ('zipcode', 'Zipcode'),
                                         ('state', 'State'),
                                         ('phonenumber', 'Phone Number')],
                                        key=itemgetter(1)))


class Question(models.Model):

    class Meta:
        ordering = ('order',)

    contact_form = models.ForeignKey(ContactForm, related_name="questions")
    question = models.TextField(help_text=_(
        "Appears on the contact form entry page."))
    label = models.CharField(max_length=32, help_text=_(
        "Appears on the results page."))
    help_text = models.TextField(blank=True)
    required = models.BooleanField(default=False)
    if PositionField:
        order = PositionField(collection=('contact_form',))
    else:
        order = models.IntegerField()
    option_type = models.CharField(max_length=12, choices=OPTION_TYPE_CHOICES)
    options = models.TextField(blank=True, default='')

    def __unicode(self):
        return self.question

    @property
    def parsed_options(self):
        if OPTION_TYPE_CHOICES.BOOLEAN == self.option_type:
            return [True, False]
        return filter(None, (s.strip() for s in self.options.splitlines()))


class Entry(models.Model):

    class Meta:
        verbose_name = "Entry"
        verbose_name_plural = "Entries"
        ordering = ('-submitted_at',)

    contact_form = models.ForeignKey(ContactForm)
    ip_address = models.IPAddressField()
    submitted_at = models.DateTimeField(default=datetime.datetime.now)
    content = models.TextField(blank=True)