From 7c3539c1c2dec9d79902f23891639fa87e2c6c5d Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Wed, 14 Mar 2018 09:55:35 +0100 Subject: [PATCH] adding get email list management command --- rowers/management/commands/getemail_list.py | 56 +++++++++++++++++++++ rowers/views.py | 10 +++- 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 rowers/management/commands/getemail_list.py diff --git a/rowers/management/commands/getemail_list.py b/rowers/management/commands/getemail_list.py new file mode 100644 index 00000000..f12679a0 --- /dev/null +++ b/rowers/management/commands/getemail_list.py @@ -0,0 +1,56 @@ +#!/srv/venv/bin/python +import sys +import os +# If you find a solution that does not need the two paths, please comment! +sys.path.append('$path_to_root_of_project$') +sys.path.append('$path_to_root_of_project$/$project_name$') + +os.environ['DJANGO_SETTINGS_MODULE'] = '$project_name$.settings' + +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings +from django.core.mail import send_mail, BadHeaderError,EmailMessage + +import datetime +from rowers.models import * +from rowsandall_app.settings import BASE_DIR + +import pandas as pd + +def getemails(): + rs = Rower.objects.all() + firstnames = [r.user.first_name for r in rs] + lastnames = [r.user.last_name for r in rs] + emails = [r.user.email for r in rs] + is_actives = [r.user.is_active for r in rs] + df = pd.DataFrame({ + 'first_name':firstnames, + 'last_name':lastnames, + 'email':emails, + 'is_active':is_actives + }) + + return df + + + +class Command(BaseCommand): + def handle(self, *args, **options): + email_list = getemails() + email_list.to_csv('email_list.csv') + + fullemail = 'roosendaalsander@gmail.com' + subject = "Rowsandall users list" + message = "Dear Sander,\n\n" + message += "Best Regards, the Rowsandall Team" + message += "Users list attached \n\n" + + email = EmailMessage(subject, message, + 'Rowsandall ', + [fullemail]) + + email.attach_file('email_list.csv') + + os.remove('email_list.csv') + + res = email.send() diff --git a/rowers/views.py b/rowers/views.py index b1445c96..322cca42 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -4448,10 +4448,16 @@ def oterankings_view(request,theuser=0, startdate = enddate-datetime.timedelta(days=int(deltadays)) if startdatestring != "": - startdate = iso8601.parse_date(startdatestring) + try: + startdate = iso8601.parse_date(startdatestring) + except ParseError: + pass if enddatestring != "": - enddate = iso8601.parse_date(enddatestring) + try: + enddate = iso8601.parse_date(enddatestring) + except ParseError: + pass if enddate < startdate: s = enddate