Private
Public Access
1
0

adding get email list management command

This commit is contained in:
Sander Roosendaal
2018-03-14 09:55:35 +01:00
parent cdbf2459ed
commit 7c3539c1c2
2 changed files with 64 additions and 2 deletions

View File

@@ -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 <info@rowsandall.com>',
[fullemail])
email.attach_file('email_list.csv')
os.remove('email_list.csv')
res = email.send()