News: odesilani na vlastni seznam adres

This commit is contained in:
Martin Rejman 2019-05-24 01:34:24 +02:00
parent b0945a10d9
commit d2a3d92157
5 changed files with 86 additions and 2 deletions

View File

@ -231,3 +231,5 @@ class Euro2019InterestForm(ModelForm):
raise ValidationError(_('Prosím, potvrďte souhlas se zpracováním osobních údajů.'), code='invalid')
return data
class CustomRecipientsForm(forms.Form):
recipients = forms.CharField(widget=forms.Textarea)

View File

@ -147,6 +147,7 @@ def list_show(request, lid):
if (request.user == obj.managed_by or 'sso_news_manage' in site_perms):
actions.add('nwsl_edit')
actions.add('nwsl_send_custom')
if (request.user in obj.sent_by.all() or request.user == obj.managed_by
or 'sso_news_manage' in site_perms):
actions.add('nwsl_details')
@ -314,10 +315,43 @@ def msg_edit(request, lid, mid=None):
return render(request, template, context)
@ensure_csrf_cookie
@login_required(login_url="/prihlaseni")
def msg_send_custom(request, id):
custom_recip=set()
if request.method == "POST":
form = forms.CustomRecipientsForm(request.POST)
if form.is_valid():
all_ok = True
for addr in form.cleaned_data['recipients'].replace("\n",",").split(","):
addr = addr.strip()
try:
validate_email(addr)
custom_recip.update([addr,]) # add list, string is also a list !!!
except ValidationError:
messages.error(request, "Email '%s' není ve správném tvaru." % (addr))
all_ok=False
if all_ok:
return msg_send(request, id, True, custom_recip)
else:
messages.error(request, "Je třeba opravit vadné adresy.")
else:
form = forms.CustomRecipientsForm()
template = 'news/msg_send_custom.html'
context = {
'form' : form,
}
return render(request, template, context)
@ensure_csrf_cookie
@login_required(login_url="/prihlaseni")
def msg_send(request, id, realSend=False):
def msg_send(request, id, realSend=False, custom_recip=set()):
"""
Odeslani newsletteru.
"""
@ -354,6 +388,10 @@ def msg_send(request, id, realSend=False):
update_sent_ts = True
use_public_to_email = True
recip_users = obj.news.get_recip_users()
elif recipients_wanted == "custom":
update_send_ts = False
use_public_to_email = True
recip_users = [ request.user ]
else:
recip_users = [ request.user ]
else:
@ -383,6 +421,11 @@ def msg_send(request, id, realSend=False):
except ValidationError:
messages.error(request, "Email '%s' uživatele %s není ve správném tvaru." % (eml, rcp.username))
# add custom recipients
recipients_email += list(custom_recip)
# do it unique
recipients_email = list(set(recipients_email))
template = 'news/msg_to_email.html'
context = {
'msg' : obj,
@ -400,7 +443,7 @@ def msg_send(request, id, realSend=False):
import quopri
for recip_part in chunks(recipients_email, 500):
# process by part, MailGun API hsa a limit of 1000 messages
# process by part, MailGun API has a limit of 1000 messages
mailgun_backend = get_connection('anymail.backends.mailgun.EmailBackend')

View File

@ -53,6 +53,9 @@
<td><a href="{% url 'nalodeni:news_msg_preview' msg.id %}" target="_blank">Náhled</a></td>
<td><a href="{% url 'nalodeni:news_msg_send' msg.id %}">Testovací email</a></td>
{% endif%}
{% if 'nwsl_send_custom' in actions %}
<td><a href="{% url 'nalodeni:news_msg_send_custom' msg.id %}">Odeslat</a></td>
{% endif%}
</tr>
{% endfor %}
</table>

View File

@ -0,0 +1,35 @@
{% extends 'pirati_cz.html' %}
{%block body%}
<div class="row">
<div class="medium-12 large-12 columns">
<section class="o-section o-section--spaceBot">
<div class="o-section-inner">
<div class="o-section-block">
<h2>Odeslání zprávy na vlastní seznam emailů</h2>
<br/>
<p>Adresy příjemců zadávejte po jedné na řádek.</p>
<form action="#?recipients=custom" method="POST">
{%csrf_token%}
{{form}}
<br/>
<input type="submit" class="button button-primary" value="Odeslat"/>
</form>
</div>
</div>
</section>
</div>
</div>
<div class="row">
<div class="medium-12 large-12 columns">
<section class="o-section o-section--spaceBot">
<div class="o-section-inner">
&nbsp;
</div>
</section>
</div>
</div>
{%endblock%}

View File

@ -58,6 +58,7 @@ urlpatterns = [
url(r'^news/msg/(?P<id>[0-9]+)/preview/$', news.msg_preview, name="news_msg_preview"),
url(r'^news/msg/(?P<id>[0-9]+)/send/$', news.msg_send, name="news_msg_send", kwargs={'realSend':True}),
url(r'^news/msg/(?P<id>[0-9]+)/send_custom/$', news.msg_send_custom, name="news_msg_send_custom"),
]