forked from CGM_Public/pretix_original
Fixed #46 -- Added a plugin to send out emails
This commit is contained in:
@@ -9,6 +9,7 @@ from django.apps import apps
|
||||
class PluginType(Enum):
|
||||
RESTRICTION = 1
|
||||
PAYMENT = 2
|
||||
ADMINFEATURE = 3
|
||||
|
||||
|
||||
def get_all_plugins() -> "List[class]":
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
import logging
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template.loader import get_template
|
||||
from django.utils import translation
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from pretix.base.i18n import LazyI18nString
|
||||
from pretix.base.models import User, Event
|
||||
from pretix.helpers.urls import build_absolute_uri
|
||||
|
||||
logger = logging.getLogger('pretix.base.mail')
|
||||
|
||||
|
||||
def mail(user: User, subject: str, template: str, context: dict, event: Event=None):
|
||||
def mail(user: User, subject: str, template: str, context: dict=None, event: Event=None):
|
||||
"""
|
||||
Sends out an email to a user.
|
||||
|
||||
:param user: The user this should be sent to.
|
||||
:param subject: The e-mail subject. Should be localized.
|
||||
:param template: The filename of a template to be used. It will
|
||||
be rendered with the recipient's locale.
|
||||
be rendered with the recipient's locale. Alternatively, you
|
||||
can pass a LazyI18nString and leave ``context`` empty
|
||||
:param context: The context for rendering the template.
|
||||
:param event: The event, used for determining the sender of the e-mail
|
||||
|
||||
@@ -31,8 +36,11 @@ def mail(user: User, subject: str, template: str, context: dict, event: Event=No
|
||||
_lng = translation.get_language()
|
||||
translation.activate(user.locale or settings.LANGUAGE_CODE)
|
||||
|
||||
tpl = get_template(template)
|
||||
body = tpl.render(context)
|
||||
if isinstance(template, LazyI18nString):
|
||||
body = str(template)
|
||||
else:
|
||||
tpl = get_template(template)
|
||||
body = tpl.render(context)
|
||||
|
||||
sender = event.settings.get('mail_from') if event else settings.MAIL_FROM
|
||||
|
||||
@@ -41,6 +49,23 @@ def mail(user: User, subject: str, template: str, context: dict, event: Event=No
|
||||
if prefix:
|
||||
subject = "[%s] %s" % (prefix, subject)
|
||||
|
||||
body += "\r\n\r\n----\r\n"
|
||||
body += _(
|
||||
"You are receiving this e-mail because you placed an order for %s." % event.name
|
||||
)
|
||||
body += "\r\n"
|
||||
body += _(
|
||||
"You can view all of your orders at the following URL:"
|
||||
)
|
||||
body += "\r\n"
|
||||
body += build_absolute_uri(
|
||||
'presale:event.orders', kwargs={
|
||||
'event': event.slug,
|
||||
'organizer': event.organizer.slug
|
||||
}
|
||||
)
|
||||
body += "\r\n"
|
||||
|
||||
email = EmailMessage(
|
||||
subject, body, sender,
|
||||
to=[user.email]
|
||||
|
||||
22
src/pretix/plugins/sendmail/__init__.py
Normal file
22
src/pretix/plugins/sendmail/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from django.apps import AppConfig
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from pretix.base.plugins import PluginType
|
||||
|
||||
|
||||
class SendMailApp(AppConfig):
|
||||
name = 'pretix.plugins.sendmail'
|
||||
verbose_name = _("Send out emails")
|
||||
|
||||
class PretixPluginMeta:
|
||||
type = PluginType.ADMINFEATURE
|
||||
name = _("Send out emails")
|
||||
author = _("the pretix team")
|
||||
version = '1.0.0'
|
||||
description = _("This plugin allows you to send out emails " +
|
||||
"to all your customers.")
|
||||
|
||||
def ready(self):
|
||||
from . import signals # NOQA
|
||||
|
||||
|
||||
default_app_config = 'pretix.plugins.sendmail.SendMailApp'
|
||||
25
src/pretix/plugins/sendmail/forms.py
Normal file
25
src/pretix/plugins/sendmail/forms.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from pretix.base.i18n import I18nFormField, I18nTextarea, I18nTextInput
|
||||
from pretix.base.models import Order
|
||||
|
||||
|
||||
class MailForm(forms.Form):
|
||||
sendto = forms.MultipleChoiceField(
|
||||
label=_("Send to"), widget=forms.CheckboxSelectMultiple,
|
||||
choices=Order.STATUS_CHOICE
|
||||
)
|
||||
subject = forms.CharField(label=_("Subject"))
|
||||
message = forms.CharField(label=_("Message"))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
event = kwargs.pop('event')
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['subject'] = I18nFormField(
|
||||
widget=I18nTextInput, required=True,
|
||||
langcodes=event.settings.get('locales')
|
||||
)
|
||||
self.fields['message'] = I18nFormField(
|
||||
widget=I18nTextarea, required=True,
|
||||
langcodes=event.settings.get('locales')
|
||||
)
|
||||
23
src/pretix/plugins/sendmail/signals.py
Normal file
23
src/pretix/plugins/sendmail/signals.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.core.urlresolvers import reverse, resolve
|
||||
from django.dispatch import receiver
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from pretix.control.signals import nav_event
|
||||
|
||||
|
||||
@receiver(nav_event)
|
||||
def control_nav_import(sender, request=None, **kwargs):
|
||||
url = resolve(request.path_info)
|
||||
if not request.eventperm.can_change_orders:
|
||||
return []
|
||||
return [
|
||||
{
|
||||
'label': _('Send out emails'),
|
||||
'url': reverse('plugins:sendmail:send', kwargs={
|
||||
'event': request.event.slug,
|
||||
'organizer': request.event.organizer.slug,
|
||||
}),
|
||||
'active': (url.namespace == 'plugins:sendmail' and url.url_name == 'send'),
|
||||
'icon': 'envelope',
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
{% extends "pretixcontrol/event/base.html" %}
|
||||
{% load i18n %}
|
||||
{% load bootstrap3 %}
|
||||
{% block title %}{% trans "Send out emails" %}{% endblock %}
|
||||
{% block content %}
|
||||
<h1>{% trans "Send out emails" %}</h1>
|
||||
{% block inner %}
|
||||
<form class="form-horizontal" method="post" action="">
|
||||
{% csrf_token %}
|
||||
{% bootstrap_field form.sendto layout='horizontal' %}
|
||||
{% bootstrap_field form.subject layout='horizontal' %}
|
||||
{% bootstrap_field form.message layout='horizontal' %}
|
||||
<div class="form-group submit-group">
|
||||
<button type="submit" class="btn btn-primary btn-save">
|
||||
{% trans "Send" %}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
|
||||
9
src/pretix/plugins/sendmail/urls.py
Normal file
9
src/pretix/plugins/sendmail/urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from . import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^control/event/(?P<organizer>[^/]+)/(?P<event>[^/]+)/sendmail/', views.SenderView.as_view(),
|
||||
name='send'),
|
||||
]
|
||||
43
src/pretix/plugins/sendmail/views.py
Normal file
43
src/pretix/plugins/sendmail/views.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import logging
|
||||
from django.contrib import messages
|
||||
from django.shortcuts import redirect
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views.generic import FormView
|
||||
|
||||
from pretix.base.models import Order
|
||||
from pretix.base.services.mail import mail
|
||||
from pretix.control.permissions import EventPermissionRequiredMixin
|
||||
|
||||
from . import forms
|
||||
|
||||
|
||||
logger = logging.getLogger('pretix.plugins.sendmail')
|
||||
|
||||
|
||||
class SenderView(EventPermissionRequiredMixin, FormView):
|
||||
template_name = 'pretixplugins/sendmail/send_form.html'
|
||||
permission = 'can_change_orders'
|
||||
form_class = forms.MailForm
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super().get_form_kwargs()
|
||||
kwargs['event'] = self.request.event
|
||||
return kwargs
|
||||
|
||||
def form_valid(self, form):
|
||||
orders = Order.objects.current.filter(
|
||||
event=self.request.event, status__in=form.cleaned_data['sendto']
|
||||
).select_related("user")
|
||||
users = set([o.user for o in orders])
|
||||
|
||||
for u in users:
|
||||
mail(u, form.cleaned_data['subject'], form.cleaned_data['message'],
|
||||
None, self.request.event)
|
||||
|
||||
messages.success(self.request, _('Your message will be sent to the selected users.'))
|
||||
|
||||
return redirect(
|
||||
'plugins:sendmail:send',
|
||||
event=self.request.event.slug,
|
||||
organizer=self.request.event.organizer.slug
|
||||
)
|
||||
@@ -131,6 +131,7 @@ INSTALLED_APPS = (
|
||||
'pretix.plugins.stripe',
|
||||
'pretix.plugins.paypal',
|
||||
'pretix.plugins.ticketoutputpdf',
|
||||
'pretix.plugins.sendmail',
|
||||
)
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
|
||||
Reference in New Issue
Block a user