From e19e191a77a527c5de346c9522bd8707d33fd485 Mon Sep 17 00:00:00 2001 From: Yingtong Li Date: Wed, 13 Feb 2019 14:47:02 +1100 Subject: [PATCH] Add bulletin preview functionality Also - do not send emails to those who are not subscribed --- .../jinja2/sspromotions/bulletin_preview.html | 86 +++++++++++++++++++ .../management/commands/sendbulletin.py | 22 ++--- sspromotions/utils.py | 9 +- sspromotions/views.py | 13 ++- 4 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 sspromotions/jinja2/sspromotions/bulletin_preview.html diff --git a/sspromotions/jinja2/sspromotions/bulletin_preview.html b/sspromotions/jinja2/sspromotions/bulletin_preview.html new file mode 100644 index 0000000..28b3668 --- /dev/null +++ b/sspromotions/jinja2/sspromotions/bulletin_preview.html @@ -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 . +#} + +{% block title %}Bulletin preview{% endblock %} + +{% block maincontent %} +

Bulletin preview

+ +
+
+ +
+
+
+ + +
+
+
+
+
+ +
+ {% for group in groups %} +
+
+ + +
+
+ {% endfor %} +
+
+
+
+ + +
+{% endblock %} + +{% block head %} + {{ super() }} + +{% endblock %} + +{% block script %} + {{ super() }} + + + +{% endblock script %} diff --git a/sspromotions/management/commands/sendbulletin.py b/sspromotions/management/commands/sendbulletin.py index d7d7c38..dcf2bcf 100644 --- a/sspromotions/management/commands/sendbulletin.py +++ b/sspromotions/management/commands/sendbulletin.py @@ -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) diff --git a/sspromotions/utils.py b/sspromotions/utils.py index b0a7237..7958f39 100644 --- a/sspromotions/utils.py +++ b/sspromotions/utils.py @@ -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: diff --git a/sspromotions/views.py b/sspromotions/views.py index 7af339a..1f34a32 100644 --- a/sspromotions/views.py +++ b/sspromotions/views.py @@ -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):