From 4800db3c2cbf8c6fd4580936fc9ae9b85c31e668 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Mon, 2 Oct 2017 00:10:52 +0200 Subject: [PATCH] Add auto generation for mailing lists --- club/templates/club/mailing.jinja | 6 ++++++ club/urls.py | 2 ++ club/views.py | 34 +++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/club/templates/club/mailing.jinja b/club/templates/club/mailing.jinja index 5ee67422..ad1f1432 100644 --- a/club/templates/club/mailing.jinja +++ b/club/templates/club/mailing.jinja @@ -16,6 +16,12 @@ - {% trans %}Delete{% endtrans %} {%- endif -%} +
+ +
+
+ +

diff --git a/club/urls.py b/club/urls.py index 2d0c47d1..a4022b16 100644 --- a/club/urls.py +++ b/club/urls.py @@ -45,6 +45,8 @@ urlpatterns = [ url(r'^(?P[0-9]+)/mailing$', ClubMailingView.as_view(action="display"), name='mailing'), url(r'^(?P[0-9]+)/mailing/new/mailing$', ClubMailingView.as_view(action="add_mailing"), name='mailing_create'), url(r'^(?P[0-9]+)/mailing/new/subscription$', ClubMailingView.as_view(action="add_member"), name='mailing_subscription_create'), + url(r'^(?P[0-9]+)/mailing/generate$', MailingAutoGenerationView.as_view(), name='mailing_generate'), + url(r'^(?P[0-9]+)/mailing/clean$', MailingAutoCleanView.as_view(), name='mailing_clean'), url(r'^(?P[0-9]+)/mailing/delete$', MailingDeleteView.as_view(), name='mailing_delete'), url(r'^(?P[0-9]+)/mailing/delete/subscription$', MailingSubscriptionDeleteView.as_view(), name='mailing_subscription_delete'), url(r'^membership/(?P[0-9]+)/set_old$', MembershipSetOldView.as_view(), name='membership_set_old'), diff --git a/club/views.py b/club/views.py index e8aaf929..edaaf40c 100644 --- a/club/views.py +++ b/club/views.py @@ -24,7 +24,7 @@ # from django import forms -from django.views.generic import ListView, DetailView, TemplateView +from django.views.generic import ListView, DetailView, TemplateView, View from django.views.generic.edit import DeleteView from django.views.generic.detail import SingleObjectMixin from django.views.generic.edit import UpdateView, CreateView @@ -35,7 +35,7 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext as _t from ajax_select.fields import AutoCompleteSelectField from django.core.exceptions import PermissionDenied -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, PageEditViewBase from core.views.forms import SelectDate, SelectDateTime @@ -562,3 +562,33 @@ class MailingSubscriptionDeleteView(CanEditMixin, DeleteView): def get_success_url(self, **kwargs): return reverse_lazy('club:mailing', kwargs={'club_id': self.club_id}) + +class MailingAutoGenerationView(View): + + def dispatch(self, request, *args, **kwargs): + self.mailing = get_object_or_404(Mailing, pk=kwargs['mailing_id']) + if not request.user.can_edit(self.mailing): + raise PermissionDenied + return super(MailingAutoGenerationView, self).dispatch(request, *args, **kwargs) + + def get(self, request, *args, **kwargs): + club = self.mailing.club + already_subscribed = [sub.user for sub in self.mailing.subscriptions.all()] + members = club.members.filter(role__gte=settings.SITH_CLUB_ROLES_ID['Board member']) + members = members.exclude(user__in=already_subscribed) + for member in members.all(): + MailingSubscription(user=member.user, mailing=self.mailing).save() + return redirect('club:mailing', club_id=club.id) + +class MailingAutoCleanView(View): + + def dispatch(self, request, *args, **kwargs): + self.mailing = get_object_or_404(Mailing, pk=kwargs['mailing_id']) + if not request.user.can_edit(self.mailing): + raise PermissionDenied + return super(MailingAutoCleanView, self).dispatch(request, *args, **kwargs) + + def get(self, request, *args, **kwargs): + for sub in self.mailing.subscriptions.all(): + sub.delete() + return redirect('club:mailing', club_id=self.mailing.club.id)