Source

inbounding / inbounding / dispatch / models.py

import uuid 

from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.db.models.signals import pre_save
from django.utils import simplejson as json

from userena.models import UserenaBaseProfile


class Template(models.Model):
    ''' 
    Templates are preformatted Action Boxes that allow users to
    get up and running quickly
    '''

    name = models.CharField(max_length=128)
    
    def __unicode__(self):
        return self.name

def mc_template(data):
    obj = json.loads(data)
    print obj['mailchimpkey']
    print obj['mailchimplistid']

class Action(models.Model):
    ACTION_TYPES = (
	('1', 'Embed Action'),
	('2', 'Floated Action'),
    )
    label = models.CharField(max_length=128)
    url = models.CharField(max_length=128)
    code = models.TextField()
    type = models.CharField(max_length=8, choices=ACTION_TYPES,)
    template = models.IntegerField(null=True)
    # Template values will store template specific information in json.
    tpl_values = models.TextField(null=True)
    published = models.BooleanField(default=0)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True,null=True)
    lastmod = models.DateTimeField(auto_now=True)
        
    def __unicode__(self):
        return self.label + " | " + self.user.username + " : " + self.url

"""
Save signal for sets a pubdate when the users selects publish but 
does not specify a date.
"""
def create_html_from_template(sender, **kwargs):
    obj = kwargs['instance']
    if obj.template:
        # created mailchimp form from json data.
        print "created via a template"

pre_save.connect(create_html_from_template, sender=Action)



class Refer(models.Model):
    name = models.CharField(max_length=128)
    action = models.ForeignKey(Action)
    
    def __unicode__(self):
        return self.name


class CustomerProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=('user'),
                                related_name='my_profile')
    site = models.ForeignKey(Site, default=1)
    uuid = models.CharField(max_length=36, blank=True, null=True)
    
"""
Save signal for creating a UUID for every user.
"""
def create_uuid(sender, **kwargs):
    obj = kwargs['instance']
    if not obj.uuid:
        obj.uuid = str(uuid.uuid4())

pre_save.connect(create_uuid, sender=CustomerProfile)