#!/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 Rower
from rowsandall_app.settings import BASE_DIR

def getexpired():
    rs = Rower.objects.all()
    lijst = []
    for r in rs:
        if r.planexpires < datetime.datetime.now().date():
            if r.rowerplan == 'pro' or r.rowerplan == 'coach':
                lijst.append(r)
        if r.teamplanexpires < datetime.datetime.now().date():
            if r.rowerplan == 'pro' or r.rowerplan == 'coach':
                lijst.append(r)
    return lijst


class Command(BaseCommand):
    def handle(self, *args, **options):
        expiredrowers = getexpired()
        
        if len(expiredrowers) > 0:
            fullemail = 'roosendaalsander@gmail.com'
            subject = "Expired rowers report"
            message = "Dear Sander,\n\n"
            message += "Best Regards, the Rowsandall Team"
            message += "Expired rowers report\n\n"
        
            for r in expiredrowers:
                message += r.user.first_name+" "+r.user.last_name+" "+r.user.username
                message += "\n"
    
            email = EmailMessage(subject, message,
			         'Rowsandall <info@rowsandall.com>',
			         [fullemail])
			 

    
            res = email.send()
