Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add send email from admin functionality

  • Loading branch information...
commit d7178f7198628e4d987c68a56c570fe020d3bd27 1 parent 072df9c
Stefan Foulis authored August 11, 2011
1  MANIFEST.in
... ...
@@ -1,2 +1,3 @@
1 1
 include LICENSE
2 2
 include README.rst
  3
+recursive-include database_email_backend/templates *
79  database_email_backend/admin.py
... ...
@@ -1,10 +1,21 @@
1 1
 #-*- coding: utf-8 -*-
  2
+from django.http import HttpResponseRedirect
2 3
 from django.contrib import admin
  4
+from django import forms
3 5
 from django.http import HttpResponse
4 6
 from django.core.urlresolvers import reverse
  7
+from django.core.mail import message
5 8
 from django.db.models import Count
6 9
 from django.utils.functional import update_wrapper
7 10
 from database_email_backend.models import Email, Attachment
  11
+from django.utils.translation import ugettext as _
  12
+
  13
+WIDE_INPUT_SIZE = '80'
  14
+
  15
+
  16
+###################
  17
+# view sent email #
  18
+###################
8 19
 
9 20
 
10 21
 class AttachmentInlineAdmin(admin.TabularInline):
@@ -77,3 +88,71 @@ def serve_attachment(self, request, email_id, attachment_id, filename, extra_con
77 88
 
78 89
 admin.site.register(Email, EmailAdmin)
79 90
 
  91
+
  92
+##############
  93
+# send email #
  94
+##############
  95
+
  96
+
  97
+class SendEmail(Email):
  98
+    class Meta:
  99
+        proxy = True
  100
+
  101
+
  102
+class SendEmailForm(forms.ModelForm):
  103
+    class Meta:
  104
+        model = SendEmail
  105
+        widgets = {
  106
+            'from_email': forms.TextInput(attrs={'size': '30'}),
  107
+            'to_emails': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
  108
+            'cc_emails': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
  109
+            'bcc_emails': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
  110
+            'subject': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
  111
+        }
  112
+        
  113
+
  114
+
  115
+class SendEmailAdmin(admin.ModelAdmin):
  116
+    form = SendEmailForm
  117
+    fieldsets = (
  118
+        (None, {'fields':('from_email', 'to_emails')}),
  119
+        (_('cc and bcc'), {
  120
+            'fields': ('cc_emails', 'bcc_emails'),
  121
+            'classes': ('collapse',)}),
  122
+        (None, {'fields': ('subject', 'body')}),
  123
+    )
  124
+    
  125
+    def save_model(self, request, obj, form, change):
  126
+        """
  127
+        sends the email and does not save it
  128
+        """
  129
+        email = message.EmailMessage(
  130
+            subject=obj.subject,
  131
+            body=obj.body,
  132
+            from_email=obj.from_email,
  133
+            to=[t.strip() for t in obj.to_emails.split(',')],
  134
+            bcc=[t.strip() for t in obj.bcc_emails.split(',')],
  135
+            cc=[t.strip() for t in obj.cc_emails.split(',')]
  136
+        )
  137
+        email.send()
  138
+
  139
+    def response_add(self, request, obj, post_url_continue=None):
  140
+        msg = _('The Email was sent successfully.')
  141
+        self.message_user(request, msg)
  142
+        if "_addanother" in request.POST:
  143
+            return HttpResponseRedirect(request.path)
  144
+        return HttpResponseRedirect('../../')
  145
+
  146
+    def has_delete_permission(self, request, obj=None):
  147
+        return False
  148
+
  149
+    def has_change_permission(self, request, obj=None):
  150
+        return False
  151
+
  152
+    def get_model_perms(self, request):
  153
+        return {
  154
+            'add': self.has_add_permission(request),
  155
+            'change': False,
  156
+            'delete': False
  157
+        }
  158
+admin.site.register(SendEmail, SendEmailAdmin)
12  database_email_backend/templates/admin/database_email_backend/sendemail/change_form.html
... ...
@@ -0,0 +1,12 @@
  1
+{% extends 'admin/change_form.html' %}
  2
+{% load i18n %}
  3
+
  4
+{% block breadcrumbs %}{% if not is_popup %}
  5
+<div class="breadcrumbs">
  6
+     <a href="../../../">{% trans "Home" %}</a> &rsaquo;
  7
+     <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
  8
+     {% trans "Send SMS" %}
  9
+</div>
  10
+{% endif %}{% endblock %}
  11
+
  12
+{% block content_title %}<h1>{% trans 'Send SMS' %}</h1>{% endblock %}

0 notes on commit d7178f7

Please sign in to comment.
Something went wrong with that request. Please try again.