Permalink
Browse files

implemented lists based on auth.models.Group

  • Loading branch information...
1 parent ab2ad43 commit 5e28c4a01cb644c1afcad26943d3bc185456f7af @sherzberg sherzberg committed Aug 12, 2011
Showing with 148 additions and 23 deletions.
  1. +2 −2 emailer/admin.py
  2. +112 −0 emailer/migrations/0012_auto.py
  3. +34 −21 emailer/models.py
View
@@ -31,11 +31,11 @@ class EmailTemplateAdminForm(forms.ModelForm):
class EmailTemplateAdmin(admin.ModelAdmin):
list_display = ('name', 'date_created',)
form = EmailTemplateAdminForm
-
+
class EmailListAdmin(admin.ModelAdmin):
list_display = ('name', 'type', 'date_created', 'preview_emails', 'merge_fields',)
list_filter = ('type', 'is_oneoff',)
-
+
def changelist_view(self, request, extra_context=None):
'''
This override, by default hides, the one off lists that are
@@ -0,0 +1,112 @@
+# 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):
+
+ # Adding M2M table for field data_site_groups on 'EmailList'
+ db.create_table('emailer_emaillist_data_site_groups', (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('emaillist', models.ForeignKey(orm['emailer.emaillist'], null=False)),
+ ('group', models.ForeignKey(orm['auth.group'], null=False))
+ ))
+ db.create_unique('emailer_emaillist_data_site_groups', ['emaillist_id', 'group_id'])
+
+
+ def backwards(self, orm):
+
+ # Removing M2M table for field data_site_groups on 'EmailList'
+ db.delete_table('emailer_emaillist_data_site_groups')
+
+
+ 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': "'ce912f7f-a455-46fd-9cf8-99d562e6d44c'", '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': "'94d547f3-3b25-412f-8b6e-d865b02fc908'", '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_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', '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': "'6a44baa0-4427-4c7f-ae72-28108b04c1d8'", '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': "'e28528d2-f184-4406-90a2-2ddab44a76a5'", 'max_length': '36', 'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '40'})
+ }
+ }
+
+ complete_apps = ['emailer']
View
@@ -1,5 +1,5 @@
from django.db import models
-from django.contrib.auth.models import User
+from django.contrib.auth.models import User, Group
from django.db import connection
from django.core.mail import EmailMultiAlternatives
from django.template import Context, Template
@@ -43,13 +43,15 @@ class EmailList(DefaultModel):
LISTTYPE_QUERY_CUSTOM_SQL = 1
LISTTYPE_RAW_EMAILS = 2
LISTTYPE_RAW_JSON = 3
+ LISTTYPE_SITEGROUPS = 4
EMAIL_LIST_TYPE_CHOICES = (
(LISTTYPE_SITEUSERS_USERDEFINED,'Site Users - User Defined'),
(LISTTYPE_QUERY_CUSTOM_SQL, 'Custom SQL Query'),
(LISTTYPE_RAW_EMAILS, 'Raw Emails'),
(LISTTYPE_RAW_EMAILS, 'Raw JSON'),
+ (LISTTYPE_SITEGROUPS, 'Site Groups'),
)
class RawEmail():
@@ -66,37 +68,48 @@ def __init__(self, email):
data_site_users = models.ManyToManyField(User, blank=True)
data_query_sql = models.TextField(blank=True)
data_raw_json = models.TextField(blank=True)
+ data_site_groups = models.ManyToManyField(Group, blank=True)
is_oneoff = models.BooleanField(default=False)
def _is_valid_field(self, field):
return not field == 'id' and not field.startswith('_')
+ def _get_user_objs(self, users):
+ try:
+ #try to include profile fields on User object
+ for u in users:
+ for field,value in u.get_profile().__dict__.iteritems():
+ if self._is_valid_field(field):
+ setattr(u, field, value)
+ except:
+ #eat this if there is no profile defined in settings.py
+ pass
+ return users
+
def get_objects(self):
if self.type in (EmailList.LISTTYPE_SITEUSERS_USERDEFINED,):
- users = self.data_site_users.all()
+ users = self._get_user_objs(self.data_site_users.all())
+ return users
+
+ elif self.type == EmailList.LISTTYPE_SITEGROUPS:
+ users = []
+ groups = self.data_site_groups.all()
- try:
- #try to include profile fields on User object
- for u in users:
- for field,value in u.get_profile().__dict__.iteritems():
- if self._is_valid_field(field):
- setattr(u, field, value)
- except:
- #eat this if there is no profile defined in settings.py
- pass
-
+ for group in groups:
+ users += self._get_user_objs(group.user_set.all())
+
return users
- elif self.type in (EmailList.LISTTYPE_QUERY_CUSTOM_SQL,):
- cursor = connection.cursor()
- cursor.execute(self.data_query_sql)
- rows = cursor.fetchall()
- objs = []
- for row in rows:#not good
- objs.append(EmailList.RawEmail(row[0]))
- return objs
+# elif self.type in (EmailList.LISTTYPE_QUERY_CUSTOM_SQL,):
+# cursor = connection.cursor()
+# cursor.execute(self.data_query_sql)
+# rows = cursor.fetchall()
+# objs = []
+# for row in rows:#not good
+# objs.append(EmailList.RawEmail(row[0]))
+# return objs
elif self.type in (EmailList.LISTTYPE_RAW_EMAILS,):
return [EmailList.RawEmail(email.strip()) for email in self.data_raw_emails.split(',')]
@@ -122,7 +135,7 @@ def merge_fields(self):
try:
obj = self.get_objects()[0]
except:
- obj = EmailList.RawEmail()
+ obj = EmailList.RawEmail("")
return obj.__dict__.keys()
merge_fields.short_description = 'Merge Fields'

0 comments on commit 5e28c4a

Please sign in to comment.