Skip to content

Commit

Permalink
add send email from admin functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanfoulis committed Aug 11, 2011
1 parent 072df9c commit d7178f7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,3 @@
include LICENSE include LICENSE
include README.rst include README.rst
recursive-include database_email_backend/templates *
79 changes: 79 additions & 0 deletions database_email_backend/admin.py
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,21 @@
#-*- coding: utf-8 -*- #-*- coding: utf-8 -*-
from django.http import HttpResponseRedirect
from django.contrib import admin from django.contrib import admin
from django import forms
from django.http import HttpResponse from django.http import HttpResponse
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.mail import message
from django.db.models import Count from django.db.models import Count
from django.utils.functional import update_wrapper from django.utils.functional import update_wrapper
from database_email_backend.models import Email, Attachment 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): class AttachmentInlineAdmin(admin.TabularInline):
Expand Down Expand Up @@ -77,3 +88,71 @@ def serve_attachment(self, request, email_id, attachment_id, filename, extra_con


admin.site.register(Email, EmailAdmin) 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)
Original file line number Original file line Diff line number Diff line change
@@ -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 %}

0 comments on commit d7178f7

Please sign in to comment.