Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented injecting merge field data into emails; changed merge_dat…

…a field on emails to be a DictionaryField from django-geo;
  • Loading branch information...
commit 632abe3b579131423043a280442e2c1e0594dbbf 1 parent a66b950
@sherzberg sherzberg authored
View
4 .settings/org.eclipse.core.resources.prefs
@@ -1,4 +0,0 @@
-#Fri Jul 08 14:33:49 EDT 2011
-eclipse.preferences.version=1
-encoding//emailer/migrations/0006_setis_oneoff.py=utf-8
-encoding//emailer/migrations/0009_emaillist_prepend_name.py=utf-8
View
2  emailer/admin.py
@@ -33,7 +33,7 @@ class EmailTemplateAdmin(admin.ModelAdmin):
form = EmailTemplateAdminForm
class EmailListAdmin(admin.ModelAdmin):
- list_display = ('name', 'type', 'date_created', 'preview_emails',)
+ list_display = ('name', 'type', 'date_created', 'preview_emails', 'merge_fields',)
list_filter = ('type', 'is_oneoff',)
def changelist_view(self, request, extra_context=None):
View
43 emailer/fields.py
@@ -0,0 +1,43 @@
+
+from django.db import models
+
+try:
+ import cPickle as pickle
+except ImportError:
+ import pickle
+
+
+
+
+class DictionaryField(models.Field):
+ #taken from django-geo
+ __metaclass__ = models.SubfieldBase
+
+ def to_python(self, value):
+ if isinstance(value, dict):
+ return value
+ else:
+ if not value:
+ return value
+ return pickle.loads(str(value))
+
+ def get_db_prep_save(self, value):
+ if value is not None and not isinstance(value, basestring):
+ if isinstance(value, dict):
+ value = pickle.dumps(value)
+ else:
+ raise TypeError('This field can only store dictionaries. Use PickledObjectField to store a wide(r) range of data types.')
+ return value
+
+ def get_internal_type(self):
+ return 'TextField'
+
+ def get_db_prep_lookup(self, lookup_type, value):
+ if lookup_type == 'exact':
+ value = self.get_db_prep_save(value)
+ return super(DictionaryField, self).get_db_prep_lookup(lookup_type, value)
+ elif lookup_type == 'in':
+ value = [self.get_db_prep_save(v) for v in value]
+ return super(DictionaryField, self).get_db_prep_lookup(lookup_type, value)
+ else:
+ raise TypeError('Lookup type %s is not supported.' % lookup_type)
View
2  emailer/management/commands/testemail.py
@@ -17,7 +17,7 @@ def __init__(self, email):
from_address = 'test@gmail.com'
subject = 'one off test'
- html = '<p>Hello</p>lksjdlfkdj'
+ html = '''<p>This message was sent to: {{ email }}</p>'''
send_raw_email(RawEmail(to_address), from_address, subject, html)
print 'test sent'
View
119 emailer/migrations/0010_convert_merge_data.py
@@ -0,0 +1,119 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import DataMigration
+from django.db import models
+
+class Migration(DataMigration):
+
+ def forwards(self, orm):
+ try:
+ import cPickle as pickle
+ except ImportError:
+ import pickle
+
+ from emailer.models import Email
+
+ for email in Email.objects.all():
+ email.merge_data = pickle.dumps(eval(email.merge_data))
+ email.save()
+
+ def backwards(self, orm):
+ try:
+ import cPickle as pickle
+ except ImportError:
+ import pickle
+
+ from emailer.models import Email
+
+ for email in Email.objects.all():
+ email.merge_data = str(email.merge_data)
+ email.save()
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'emailer.email': {
+ 'Meta': {'ordering': "['date_created']", 'object_name': 'Email'},
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'email_blast': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['emailer.EmailBlast']"}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'76c01d3d-8da8-425e-a342-fafae650bb50'", 'max_length': '36', 'primary_key': 'True'}),
+ 'merge_data': ('django.db.models.fields.TextField', [], {}),
+ 'opened': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'status': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'status_message': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'to_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'})
+ },
+ 'emailer.emailblast': {
+ 'Meta': {'ordering': "['date_created']", 'object_name': 'EmailBlast'},
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'from_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+ 'html': ('django.db.models.fields.TextField', [], {}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'b7874ec8-dbd8-4316-bd53-a2b745df0eb5'", 'max_length': '36', 'primary_key': 'True'}),
+ 'lists': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['emailer.EmailList']", 'symmetrical': 'False'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
+ 'send_after': ('django.db.models.fields.DateTimeField', [], {}),
+ 'subject': ('django.db.models.fields.CharField', [], {'max_length': '40'})
+ },
+ 'emailer.emaillist': {
+ 'Meta': {'ordering': "['name']", 'object_name': 'EmailList'},
+ 'data_query_sql': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'data_raw_emails': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'data_raw_json': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'data_site_users': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.User']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'48e942a3-5e40-45da-8656-66ba0c8b36ce'", 'max_length': '36', 'primary_key': 'True'}),
+ 'is_oneoff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '40'}),
+ 'type': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+ },
+ 'emailer.emailtemplate': {
+ 'Meta': {'object_name': 'EmailTemplate'},
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'html': ('django.db.models.fields.TextField', [], {}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'c23c3371-591e-46fd-bc1c-54c29c45b277'", 'max_length': '36', 'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '40'})
+ }
+ }
+
+ complete_apps = ['emailer']
View
106 emailer/migrations/0011_auto__chg_field_email_merge_data.py
@@ -0,0 +1,106 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Changing field 'Email.merge_data'
+ db.alter_column('emailer_email', 'merge_data', self.gf('emailer.fields.DictionaryField')())
+
+
+ def backwards(self, orm):
+
+ # Changing field 'Email.merge_data'
+ db.alter_column('emailer_email', 'merge_data', self.gf('django.db.models.fields.TextField')())
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'emailer.email': {
+ 'Meta': {'ordering': "['date_created']", 'object_name': 'Email'},
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'email_blast': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['emailer.EmailBlast']"}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'e6c1cbe2-27ae-41ba-a8f7-0225f2b0a3db'", 'max_length': '36', 'primary_key': 'True'}),
+ 'merge_data': ('emailer.fields.DictionaryField', [], {'blank': 'True'}),
+ 'opened': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'status': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'status_message': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'to_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'})
+ },
+ 'emailer.emailblast': {
+ 'Meta': {'ordering': "['date_created']", 'object_name': 'EmailBlast'},
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'from_address': ('django.db.models.fields.EmailField', [], {'max_length': '75'}),
+ 'html': ('django.db.models.fields.TextField', [], {}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'e1efb7b9-3f7d-44e9-8bc1-cdfa1ef59dd7'", 'max_length': '36', 'primary_key': 'True'}),
+ 'lists': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['emailer.EmailList']", 'symmetrical': 'False'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
+ 'send_after': ('django.db.models.fields.DateTimeField', [], {}),
+ 'subject': ('django.db.models.fields.CharField', [], {'max_length': '40'})
+ },
+ 'emailer.emaillist': {
+ 'Meta': {'ordering': "['name']", 'object_name': 'EmailList'},
+ 'data_query_sql': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'data_raw_emails': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'data_raw_json': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'data_site_users': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.User']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'86588956-6bea-4f83-83d0-67cfbc4aa786'", 'max_length': '36', 'primary_key': 'True'}),
+ 'is_oneoff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '40'}),
+ 'type': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+ },
+ 'emailer.emailtemplate': {
+ 'Meta': {'object_name': 'EmailTemplate'},
+ 'date_changed': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'date_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'html': ('django.db.models.fields.TextField', [], {}),
+ 'id': ('django.db.models.fields.CharField', [], {'default': "'ff153855-4cdb-4305-a80c-3b3fcc24d7c9'", 'max_length': '36', 'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '40'})
+ }
+ }
+
+ complete_apps = ['emailer']
View
23 emailer/models.py
@@ -6,6 +6,8 @@
from django.conf import settings
from emailer.html2text import html2text
+from emailer.fields import DictionaryField
+
from urlparse import urljoin
import uuid, json
@@ -105,6 +107,17 @@ def preview_emails(self):
preview_emails.short_description = 'Preview Emails'
preview_emails.allow_tags = True
+ def merge_fields(self):
+ try:
+ obj = self.get_objects()[0]
+ print obj
+ except:
+ obj = object()
+
+ return obj.__dict__.keys()
+ merge_fields.short_description = 'Merge Fields'
+ merge_fields.allow_tags = True
+
class Meta:
ordering = ['name']
@@ -167,6 +180,8 @@ def _add_tracking_info(html, tracking_id, tracking_png_url):
def _apply_merge_data(html, merge_data):
t = Template(html)
+ print html, type(html)
+ print merge_data, type(merge_data)
c = Context(merge_data)
return t.render(c)
@@ -174,7 +189,11 @@ class EmailManager(models.Manager):
def email_from_tracking(self, id):
return self.get(id=id)
-
+
+
+from south.modelsinspector import add_introspection_rules
+add_introspection_rules([], ["^emailer\.fields\.DictionaryField"])
+
class Email(DefaultModel):
STATUS_PREPARED = 0
STATUS_SENT = 1
@@ -188,7 +207,7 @@ class Email(DefaultModel):
email_blast = models.ForeignKey(EmailBlast, blank=False)
to_address = models.EmailField(blank=False)
- merge_data = models.TextField(editable=False)
+ merge_data = DictionaryField(editable=False, blank=True)
status = models.IntegerField(blank=False, choices=STATUS_CHOCES, default=STATUS_PREPARED, editable=False)
status_message = models.TextField(blank=True, editable=False)
View
5 emailer/utils/__init__.py
@@ -17,15 +17,16 @@ def send_raw_email(email_obj, from_address, subject, content_html, just_prepare=
email_list.is_oneoff = True
email_list.type = EmailList.LISTTYPE_RAW_EMAILS
email_list.save()
-
+
email_blast = EmailBlast()
email_blast.name = subject+ ' '+str(datetime.datetime.today().strftime("%Y-%b-%d-%m"))
- email_blast.lists = [email_list]
email_blast.send_after = datetime.datetime.now()
email_blast.from_address = from_address
email_blast.subject = subject
email_blast.html = content_html
email_blast.save()
+ email_blast.lists.add(email_list)
+ email_blast.save()
if just_prepare:
status = email_blast.prepare_for_send()
View
1  testsite/settings.py
@@ -94,6 +94,7 @@
'tinymce',
'emailer',
'south',
+ 'testapp',
)
#TINYMCE_SPELLCHECKER = False
View
10 testsite/testapp/models.py
@@ -1,3 +1,11 @@
from django.db import models
-
+from emailer import fields as custom_fields
# Create your models here.
+
+
+
+class DefaultModel(models.Model):
+ dict = custom_fields.DictionaryField('A dictionary of additional information', blank=False, null=False, editable=False)
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.