import os
import re

import sys
#cmd_folder = '/home/jluttine'
cmd_folder = '/home/jluttine/tuhlaajapojat'
if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sportsteam.settings")
#os.environ['DJANGO_SETTINGS_MODULE'] = 'sportsteam.settings'

from teamstats.models import *

def get_addresses(list):

    print 'Trying to match a list: ' + list

    # Some aliases :)
    if list.lower() == 'vuokravaioma':
        list = 'jaakko.luttinen'

    # Try if for everyone
    if list.lower() == 'tuhlaajapojat':
        players = Player.objects.all()
        emails = [player.email for player in players if player.email]
        emails = filter(None, emails)
        return emails, '[Tuhlaajapojat] '

    # Check team aliases
    if list.lower() == 'tuhlaajapojat-adidas':
        list = 'tuhlaajapojat-adidas2014'
    if list.lower() == 'tuhlaajapojat-fmhd':
        list = 'tuhlaajapojat-fmhd2012'
    if list.lower() == 'tuhlaajapojat-fmhm':
        list = 'tuhlaajapojat-fmhm2014'
    if list.lower() == 'tuhlaajapojat-esport':
        list = 'tuhlaajapojat-esport2014'
    if list.lower() == 'tuhlaajapojat-haku2':
        list = 'tuhlaajapojat-haku22013'
    if list.lower() == 'tuhlaajapojat-hakud':
        list = 'tuhlaajapojat-hakud2014'
    if list.lower() == 'tuhlaajapojat-hakid':
        list = 'tuhlaajapojat-hakid2014'
    if list.lower() == 'tuhlaajapojat-hakim':
        list = 'tuhlaajapojat-hakim2013'
    if list.lower() == 'tuhlaajapojat-srksm':
        list = 'tuhlaajapojat-srksm2014'

    # Try matching to season IDs
    id = re.search('tuhlaajapojat-(?P<id>.+)', list.lower())
    if id:
        season = Season.objects.get(id__iexact=id.group('id'))
        if season:
            players = SeasonPlayer.objects.filter(season__id=season.id,passive=False)
            emails = [player.player.email for player in players]
            emails = filter(None, emails)
            return emails, '[Tuhlaajapojat-' + unicode(season.league) + '] '

    # Try matching to players
    try:
        player = Player.objects.get(id__iexact=list)
        if player.email:
            return [player.email], ''
    except:
        email = None

    print 'Did not find a match'

    # No match
    return None, ''


import os
import re

import sys
#cmd_folder = '/home/jluttine'
cmd_folder = '/home/jluttine/tuhlaajapojat'
if cmd_folder not in sys.path:
    sys.path.insert(0, cmd_folder)

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sportsteam.settings")
#os.environ['DJANGO_SETTINGS_MODULE'] = 'sportsteam.settings'

from teamstats.models import *

def get_addresses(list):

    print 'Trying to match a list: ' + list

    # Some aliases :)
    if list.lower() == 'vuokravaioma':
        list = 'jaakko.luttinen'

    # Try if for everyone
    if list.lower() == 'tuhlaajapojat':
        players = Player.objects.all()
        emails = [player.email for player in players if player.email]
        emails = filter(None, emails)
        return emails, '[Tuhlaajapojat] '

    # Check team aliases
    if list.lower() == 'adidas':
        list = 'adidas2014'
    if list.lower() == 'fmhd':
        list = 'fmhd2012'
    if list.lower() == 'fmhm':
        list = 'fmhm2014'
    if list.lower() == 'esport':
        list = 'esport2014'
    if list.lower() == 'haku2':
        list = 'haku22013'
    if list.lower() == 'hakud':
        list = 'hakud2014'
    if list.lower() == 'hakid':
        list = 'hakid2014'
    if list.lower() == 'hakim':
        list = 'hakim2013'
    if list.lower() == 'srksm':
        list = 'srksm2014'

    # Try matching to season IDs
    try:
        season = Season.objects.get(id__iexact=list)
    except Season.DoesNotExist:
        pass
    else:
        players = SeasonPlayer.objects.filter(season__id=season.id,passive=False)
        emails = [player.player.email for player in players]
        emails = filter(None, emails)
        return emails, '[Tuhlaajapojat-' + unicode(season.league) + '] '

    # Try matching to player IDs
    try:
        player = Player.objects.get(id__iexact=list)
    except Player.DoesNotExist:
        pass
    else:
        return [player.email], ''

    print 'Did not find recipients for %s' % list 

    # No match
    return None, ''


Update email lists