Add bulletin preview functionality

Also - do not send emails to those who are not subscribed
This commit is contained in:
Yingtong Li 2019-02-13 14:47:02 +11:00
parent e0982534b3
commit e19e191a77
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 111 additions and 19 deletions

View File

@ -0,0 +1,86 @@
{% extends 'sspromotions/base.html' %}
{#
Society Self-Service
Copyright © 2018-2019 Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
#}
{% block title %}Bulletin preview{% endblock %}
{% block maincontent %}
<h1>Bulletin preview</h1>
<form class="ui form" method="POST">
<div class="ui required inline grid field">
<label class="three wide column">Date</label>
<div class="eleven wide column">
<div class="ui calendar" id="cal_date">
<div class="ui input left icon grid">
<i class="calendar icon" style="z-index: 999;"></i>
<input class="sixteen wide column" type="text" name="date" value="{{ date }}">
</div>
</div>
</div>
</div>
<div class="ui inline grid field">
<label class="three wide column">Groups</label>
<div class="eleven wide column">
{% for group in groups %}
<div class="field" style="display: inline; margin-right: 1em;">
<div class="ui checkbox">
<input type="checkbox" name="group_{{ group.id }}" id="group_{{ group.id }}" checked>
<label for="group_{{ group.id }}">{{ group.name }}</label>
</div>
</div>
{% endfor %}
</div>
</div>
<div class="ui divider"></div>
<div class="ui error message"></div>
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<input class="ui primary button" type="submit" name='submit' value="Preview">
</form>
{% endblock %}
{% block head %}
{{ super() }}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui-calendar@0.0.8/dist/calendar.min.css" integrity="sha256-KCHiPtYk/vfF5/6lDXpz5r5FuIYchVdai0fepwGft80=" crossorigin="anonymous">
{% endblock %}
{% block script %}
{{ super() }}
<script src="https://cdn.jsdelivr.net/npm/semantic-ui-calendar@0.0.8/dist/calendar.min.js" integrity="sha256-Pnz4CK94A8tUiYWCfg/Ko25YZrHqOKeMS4JDXVTcVA0=" crossorigin="anonymous"></script>
<script>
function leftpad(n) {
if (n < 10)
return '0' + n;
return '' + n;
}
$('#cal_date').calendar({
type: 'date',
formatter: {
date: function(date, settings) {
return date.getFullYear() + '-' + leftpad(date.getMonth() + 1) + '-' + leftpad(date.getDate());
}
},
isDisabled: function(date, mode) {
return date.getDay() !== 0; // Sunday only
}
});
</script>
{% endblock script %}

View File

@ -26,6 +26,7 @@ from django.urls import reverse
from django.utils import timezone
import ssmembership.models
import sspromotions.models
import sspromotions.utils
import logging
@ -96,13 +97,14 @@ class Command(BaseCommand):
events = list(sspromotions.utils.get_calendar_events(calbegin, calend))
for member in members:
template_args = sspromotions.utils.bulletin_args(member, events, bulbegin, bulend)
content_html = premailer.Premailer(template_html.render(template_args), cssutils_logging_level=logging.ERROR, strip_important=False).transform()
content_txt = template_txt.render(template_args)
if options['render']:
self.stdout.write('Content-Type: multipart/alternative; boundary=boundary\n\n--boundary\nContent-Type: text/html; charset=utf-8\n\n' + content_html + '\n--boundary\nContent-Type: text/plain; charset=utf-8\n\n' + content_txt + '\n--boundary')
else:
self.stdout.write('Emailing {} at {}'.format(member.id, member.email))
send_aws_email(client, member.email, title, content_html, content_txt)
if sspromotions.models.BulletinSubscription.is_member_subscribed(member):
template_args = sspromotions.utils.bulletin_args(member, sspromotions.models.Group.get_member_groups(member), events, bulbegin, bulend)
content_html = premailer.Premailer(template_html.render(template_args), cssutils_logging_level=logging.ERROR, strip_important=False).transform()
content_txt = template_txt.render(template_args)
if options['render']:
self.stdout.write('Content-Type: multipart/alternative; boundary=boundary\n\n--boundary\nContent-Type: text/html; charset=utf-8\n\n' + content_html + '\n--boundary\nContent-Type: text/plain; charset=utf-8\n\n' + content_txt + '\n--boundary')
else:
self.stdout.write('Emailing {} at {}'.format(member.id, member.email))
send_aws_email(client, member.email, title, content_html, content_txt)

View File

@ -48,18 +48,13 @@ def bulletin_dates(dt):
return calbegin, calend, bulbegin, bulend
def bulletin_args(member, events, bulbegin, bulend):
if member is None:
groups = models.Group.objects.all()
else:
groups = models.Group.get_member_groups(member)
def bulletin_args(member, groups, events, bulbegin, bulend):
groups_data = []
for group in groups:
items = []
for item in group.bulletinitem_set.filter(date__gte=bulbegin).filter(date__lt=bulend).all():
# Check also_limit
if member is None or len(item.also_limit) == 0 or any(models.Group.objects.get(id=x).contains_member(member) for x in item.also_limit if models.Group.objects.filter(id=x).count() > 0):
if member is None or len(item.also_limit) == 0 or any(any(g.id == x for g in groups) for x in item.also_limit):
items.append(item)
if len(items) > 0:

View File

@ -22,6 +22,7 @@ from django.shortcuts import render, redirect
from django.urls import reverse
from django.utils import timezone
import ssmembership.models
from . import models
from . import utils
@ -59,8 +60,16 @@ def bulletin_list(request):
@login_required
def bulletin_preview(request):
calbegin, calend, bulbegin, bulend = utils.bulletin_dates(timezone.now())
return render(request, 'sspromotions/email/bulletin.html', utils.bulletin_args(None, utils.get_calendar_events(calbegin, calend), bulbegin, bulend))
if request.method == 'POST':
group_ids = [int(k[6:]) for k, v in request.POST.items() if k.startswith('group_') and v]
groups = [group for group in models.Group.objects.all() if group.id in group_ids]
calbegin, calend, bulbegin, bulend = utils.bulletin_dates(datetime.datetime.strptime(request.POST['date'], '%Y-%m-%d'))
return render(request, 'sspromotions/email/bulletin.html', utils.bulletin_args(None, groups, utils.get_calendar_events(calbegin, calend), bulbegin, bulend))
else:
date = timezone.now().date()
date += datetime.timedelta(days=(6 - date.weekday() + 7) % 7) # Next Sunday (6 = Sunday)
return render(request, 'sspromotions/bulletin_preview.html', {'date': date, 'groups': models.Group.objects.all()})
@login_required
def bulletin_new(request):