Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add send email from admin functionality

  • Loading branch information...
commit d7178f7198628e4d987c68a56c570fe020d3bd27 1 parent 072df9c
@stefanfoulis authored
View
1  MANIFEST.in
@@ -1,2 +1,3 @@
include LICENSE
include README.rst
+recursive-include database_email_backend/templates *
View
79 database_email_backend/admin.py
@@ -1,10 +1,21 @@
#-*- coding: utf-8 -*-
+from django.http import HttpResponseRedirect
from django.contrib import admin
+from django import forms
from django.http import HttpResponse
from django.core.urlresolvers import reverse
+from django.core.mail import message
from django.db.models import Count
from django.utils.functional import update_wrapper
from database_email_backend.models import Email, Attachment
+from django.utils.translation import ugettext as _
+
+WIDE_INPUT_SIZE = '80'
+
+
+###################
+# view sent email #
+###################
class AttachmentInlineAdmin(admin.TabularInline):
@@ -77,3 +88,71 @@ def serve_attachment(self, request, email_id, attachment_id, filename, extra_con
admin.site.register(Email, EmailAdmin)
+
+##############
+# send email #
+##############
+
+
+class SendEmail(Email):
+ class Meta:
+ proxy = True
+
+
+class SendEmailForm(forms.ModelForm):
+ class Meta:
+ model = SendEmail
+ widgets = {
+ 'from_email': forms.TextInput(attrs={'size': '30'}),
+ 'to_emails': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
+ 'cc_emails': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
+ 'bcc_emails': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
+ 'subject': forms.TextInput(attrs={'size': WIDE_INPUT_SIZE}),
+ }
+
+
+
+class SendEmailAdmin(admin.ModelAdmin):
+ form = SendEmailForm
+ fieldsets = (
+ (None, {'fields':('from_email', 'to_emails')}),
+ (_('cc and bcc'), {
+ 'fields': ('cc_emails', 'bcc_emails'),
+ 'classes': ('collapse',)}),
+ (None, {'fields': ('subject', 'body')}),
+ )
+
+ def save_model(self, request, obj, form, change):
+ """
+ sends the email and does not save it
+ """
+ email = message.EmailMessage(
+ subject=obj.subject,
+ body=obj.body,
+ from_email=obj.from_email,
+ to=[t.strip() for t in obj.to_emails.split(',')],
+ bcc=[t.strip() for t in obj.bcc_emails.split(',')],
+ cc=[t.strip() for t in obj.cc_emails.split(',')]
+ )
+ email.send()
+
+ def response_add(self, request, obj, post_url_continue=None):
+ msg = _('The Email was sent successfully.')
+ self.message_user(request, msg)
+ if "_addanother" in request.POST:
+ return HttpResponseRedirect(request.path)
+ return HttpResponseRedirect('../../')
+
+ def has_delete_permission(self, request, obj=None):
+ return False
+
+ def has_change_permission(self, request, obj=None):
+ return False
+
+ def get_model_perms(self, request):
+ return {
+ 'add': self.has_add_permission(request),
+ 'change': False,
+ 'delete': False
+ }
+admin.site.register(SendEmail, SendEmailAdmin)
View
12 database_email_backend/templates/admin/database_email_backend/sendemail/change_form.html
@@ -0,0 +1,12 @@
+{% extends 'admin/change_form.html' %}
+{% load i18n %}
+
+{% block breadcrumbs %}{% if not is_popup %}
+<div class="breadcrumbs">
+ <a href="../../../">{% trans "Home" %}</a> &rsaquo;
+ <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
+ {% trans "Send SMS" %}
+</div>
+{% endif %}{% endblock %}
+
+{% block content_title %}<h1>{% trans 'Send SMS' %}</h1>{% endblock %}
Please sign in to comment.
Something went wrong with that request. Please try again.