From 02d5a542bc9f59b5263f752cb890daad89dfc344 Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Tue, 27 Oct 2020 11:55:16 +0100 Subject: [PATCH] added MAIL, VIS and LIST to sendmails command --- input/management/commands/sendmails.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/input/management/commands/sendmails.py b/input/management/commands/sendmails.py index 85c830a..3264850 100644 --- a/input/management/commands/sendmails.py +++ b/input/management/commands/sendmails.py @@ -1,10 +1,11 @@ from datetime import date, timedelta +import sys from django.core.management.base import BaseCommand, CommandError from django.template.loader import get_template from django.core.mail import send_mail, BadHeaderError -from input.models import Project, Library, HonoraryCertificate, Travel +from input.models import Project, Library, HonoraryCertificate, Travel, Email, BusinessCard, List from input.settings import URLPREFIX, IF_EMAIL, SURVEYPREFIX class Command(BaseCommand): @@ -66,15 +67,18 @@ class Command(BaseCommand): self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.')) - def surveymails_to_object(self, supported, name, type='LIB'): + def surveymails_to_object(self, supported, name='', type='LIB'): mytype=type + myname = name for item in supported: if type == 'LIB': mytype = item.type + elif type not in ('MAIL','VIS','LIST'): + myname = getattr(item,name) self.survey_link(email=item.email, type=mytype, pid=f'{mytype}{item.pk}', - name=getattr(item,name), + name=myname, realname=item.realname) item.survey_mail_send = True item.save() @@ -112,6 +116,19 @@ class Command(BaseCommand): .exclude(survey_mail_send=True) self.surveymails_to_object(supported, type='TRAV', name='request_url') + def surveymails_to_mail_vis_lis(self): + '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted''' + lastdate = date.today() - timedelta(days=14) + + typefield = ('MAIL','VIS','LIST') + count = 0 + for c in ('Email', 'BusinessCard', 'List'): + supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\ + .filter(granted_date__lt = lastdate)\ + .exclude(survey_mail_send=True) + self.surveymails_to_object(supported, type=typefield[count]) + count += 1 + def handle(self, *args, **options): '''the main function which is called by the custom command''' @@ -121,5 +138,6 @@ class Command(BaseCommand): self.surveymails_to_hon() self.surveymails_to_project() self.surveymails_to_travel() + self.surveymails_to_mail_vis_lis() self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))