From 0c44eb82241f12bbd4c9fb68a6341338657747dc Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Mon, 26 Oct 2020 13:55:36 +0100 Subject: [PATCH] code duplication removed --- input/management/commands/sendmails.py | 58 +++++++++----------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/input/management/commands/sendmails.py b/input/management/commands/sendmails.py index 9f23e11..229e65d 100644 --- a/input/management/commands/sendmails.py +++ b/input/management/commands/sendmails.py @@ -53,7 +53,6 @@ class Command(BaseCommand): for project in old: context = {'project': project} context['URLPREFIX'] = URLPREFIX - print(context) try: send_mail('Projektende erreicht', mail_template.render(context), @@ -67,21 +66,28 @@ class Command(BaseCommand): self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.')) + def surveymails_to_object(self, supported, name, type='LIB'): + mytype=type + for item in supported: + if type == 'LIB': + mytype = item.type + self.survey_link(email=item.email, + type=mytype, + pid=9999, ## TODO + name=getattr(item,name), + realname=item.realname) + item.survey_mail_send = True + item.save() + self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} send')) + + def surveymails_to_lib(self): '''get all library objects which where granted two weeks ago''' supported = Library.objects.filter(granted=True)\ .filter(granted_date__lt = date.today() - timedelta(days=14))\ .exclude(survey_mail_send=True) - print(supported) - for item in supported: - self.survey_link(email=item.email, - type=item.type, - pid=9999, ## TODO - name=item.library, - realname=item.realname) - item.survey_mail_send = True - item.save() + self.surveymails_to_object(supported,name='name') def surveymails_to_hon(self): '''get all HonoraryCertificate objects which where granted two weeks ago''' @@ -89,30 +95,14 @@ class Command(BaseCommand): supported = HonoraryCertificate.objects.filter(granted=True)\ .filter(granted_date__lt = date.today() - timedelta(days=14))\ .exclude(survey_mail_send=True) - print(supported) - for item in supported: - self.survey_link(email=item.email, - type='HON', - pid=9999, ## TODO - name=item.request_url, - realname=item.realname) - item.survey_mail_send = True - item.save() + self.surveymails_to_object(supported, type='HON', name='request_url') def surveymails_to_project(self): '''send survey link 4 weeks after end of project reached''' supported = Project.objects.filter(granted=True)\ .filter(granted_date__lt = date.today() - timedelta(days=28))\ .exclude(survey_mail_send=True) - print(supported) - for item in supported: - self.survey_link(email=item.email, - type='PRO', - pid=9999, ## TODO - name=item.name, - realname=item.realname) - item.survey_mail_send = True - item.save() + self.surveymails_to_object(supported, type='PRO', name='realname') def surveymails_to_travel(self): '''send survey link 3 weeks after end of project reached''' @@ -120,17 +110,7 @@ class Command(BaseCommand): supported = Travel.objects.filter(project__granted=True)\ .filter(project__granted_date__lt = date.today() - timedelta(days=21))\ .exclude(survey_mail_send=True) - print(supported) - for item in supported: - self.survey_link(email=item.email, - type='TRAV', - pid=9999, ## TODO - name=item.request_url, - realname=item.realname) - item.survey_mail_send = True - item.save() - - pass + self.surveymails_to_object(supported, type='TRAV', name='request_url') def handle(self, *args, **options):