2014-11-21 15:39:41 +00:00
|
|
|
from django.contrib import admin, messages
|
|
|
|
from django.core.mail import send_mass_mail
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.utils.translation import ungettext, ugettext_lazy as _
|
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
from .. import settings
|
2014-11-21 15:39:41 +00:00
|
|
|
|
2014-11-24 14:39:41 +00:00
|
|
|
from .utils import change_url
|
2014-11-21 15:39:41 +00:00
|
|
|
from .forms import SendEmailForm
|
|
|
|
|
|
|
|
|
|
|
|
class SendEmail(object):
|
|
|
|
""" Form wizard for billing orders admin action """
|
|
|
|
short_description = _("Send email")
|
|
|
|
form = SendEmailForm
|
|
|
|
template = 'admin/orchestra/generic_confirmation.html'
|
2014-11-24 14:39:41 +00:00
|
|
|
default_from = settings.ORCHESTRA_DEFAULT_SUPPORT_FROM_EMAIL
|
2014-11-21 15:39:41 +00:00
|
|
|
__name__ = 'semd_email'
|
|
|
|
|
|
|
|
def __call__(self, modeladmin, request, queryset):
|
|
|
|
""" make this monster behave like a function """
|
|
|
|
self.modeladmin = modeladmin
|
|
|
|
self.queryset = queryset
|
2014-11-24 14:39:41 +00:00
|
|
|
self.opts = modeladmin.model._meta
|
|
|
|
app_label = self.opts.app_label
|
2014-11-21 15:39:41 +00:00
|
|
|
self.context = {
|
|
|
|
'action_name': _("Send email"),
|
|
|
|
'action_value': self.__name__,
|
2014-11-24 14:39:41 +00:00
|
|
|
'opts': self.opts,
|
2014-11-21 15:39:41 +00:00
|
|
|
'app_label': app_label,
|
|
|
|
'queryset': queryset,
|
|
|
|
'action_checkbox_name': admin.helpers.ACTION_CHECKBOX_NAME,
|
|
|
|
}
|
|
|
|
return self.write_email(request)
|
|
|
|
|
|
|
|
def write_email(self, request):
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
raise PermissionDenied
|
2014-11-24 14:39:41 +00:00
|
|
|
initial={
|
|
|
|
'email_from': self.default_from,
|
|
|
|
'to': ' '.join(self.queryset.values_list('email', flat=True))
|
|
|
|
}
|
|
|
|
form = self.form(initial=initial)
|
2014-11-21 15:39:41 +00:00
|
|
|
if request.POST.get('post'):
|
2014-11-24 14:39:41 +00:00
|
|
|
form = self.form(request.POST, initial=initial)
|
2014-11-21 15:39:41 +00:00
|
|
|
if form.is_valid():
|
|
|
|
options = {
|
|
|
|
'email_from': form.cleaned_data['email_from'],
|
2014-11-24 14:39:41 +00:00
|
|
|
'extra_to': form.cleaned_data['extra_to'],
|
2014-11-21 15:39:41 +00:00
|
|
|
'subject': form.cleaned_data['subject'],
|
|
|
|
'message': form.cleaned_data['message'],
|
|
|
|
|
|
|
|
}
|
|
|
|
return self.confirm_email(request, **options)
|
|
|
|
opts = self.modeladmin.model._meta
|
|
|
|
app_label = opts.app_label
|
|
|
|
self.context.update({
|
2014-11-24 14:39:41 +00:00
|
|
|
'title': _("Send e-mail to %s") % self.opts.verbose_name_plural,
|
2014-11-21 15:39:41 +00:00
|
|
|
'content_title': "",
|
|
|
|
'form': form,
|
|
|
|
'submit_value': _("Continue"),
|
|
|
|
})
|
|
|
|
# Display confirmation page
|
|
|
|
return render(request, self.template, self.context)
|
|
|
|
|
|
|
|
def confirm_email(self, request, **options):
|
|
|
|
num = len(self.queryset)
|
|
|
|
email_from = options['email_from']
|
2014-11-24 14:39:41 +00:00
|
|
|
extra_to = options['extra_to']
|
2014-11-21 15:39:41 +00:00
|
|
|
subject = options['subject']
|
|
|
|
message = options['message']
|
|
|
|
# The user has already confirmed
|
|
|
|
if request.POST.get('post') == 'email_confirmation':
|
2014-11-24 14:39:41 +00:00
|
|
|
emails = []
|
2014-11-21 15:39:41 +00:00
|
|
|
for contact in self.queryset.all():
|
2014-11-24 14:39:41 +00:00
|
|
|
emails.append((subject, message, email_from, [contact.email]))
|
|
|
|
if extra_to:
|
|
|
|
emails.append((subject, message, email_from, extra_to))
|
|
|
|
send_mass_mail(emails)
|
2014-11-21 15:39:41 +00:00
|
|
|
msg = ungettext(
|
|
|
|
_("Message has been sent to %s.") % str(contact),
|
2014-11-24 14:39:41 +00:00
|
|
|
_("Message has been sent to %i %s.") % (num, self.opts.verbose_name_plural),
|
2014-11-21 15:39:41 +00:00
|
|
|
num
|
|
|
|
)
|
|
|
|
self.modeladmin.message_user(request, msg)
|
|
|
|
return None
|
|
|
|
|
|
|
|
form = self.form(initial={
|
2014-11-24 14:39:41 +00:00
|
|
|
'email_from': email_from,
|
|
|
|
'extra_to': ', '.join(extra_to),
|
2014-11-21 15:39:41 +00:00
|
|
|
'subject': subject,
|
|
|
|
'message': message
|
|
|
|
})
|
|
|
|
self.context.update({
|
|
|
|
'title': _("Are you sure?"),
|
|
|
|
'content_message': _(
|
2014-11-24 14:39:41 +00:00
|
|
|
"Are you sure you want to send the following message to the following %s?"
|
|
|
|
) % self.opts.verbose_name_plural,
|
2014-11-21 15:39:41 +00:00
|
|
|
'display_objects': ["%s (%s)" % (contact, contact.email) for contact in self.queryset],
|
|
|
|
'form': form,
|
|
|
|
'subject': subject,
|
|
|
|
'message': message,
|
|
|
|
'post_value': 'email_confirmation',
|
|
|
|
})
|
|
|
|
# Display the confirmation page
|
|
|
|
return render(request, self.template, self.context)
|