# Path: app/helpers/utils.py
# def dict_remove(d, *keys):
# def capitalize_first(str):
# def make_unique_md5():
# def get_all_functions(module):
# def sqlands(left, lst):
# def get_ip():
# def store(tablename, _test=False, **values):
# 
# Path: app/helpers/email_templates.py
# def to_applicant(applicant):
# def to_applicant_simple(applicant):
# def to_referee(applicant):
# def notify_applicant(applicant):
# def notify_referee(applicant):
# def resend_password(user):


import web
from config import db
from app.helpers import utils
from app.helpers import email_templates
from cStringIO import StringIO
from datetime import date

def submit_application(resume, d, simple=False):
    # clean up some fields
    clean_up_data(d)
    # handle the file upload
    d.resume_fn = save_resume(resume, d.first_name, d.last_name)
    # in simple mode, no reference is required
    if simple:
        # save the data in the db
        add_applicant(d)
        # email user and referee
        email_templates.to_applicant_simple(d)
    else:
        # make secret md5
        d.secret_md5 = utils.make_unique_md5()
        # save the data in the db
        add_applicant(d)
        # email user and referee
        email_templates.to_applicant(d)
        email_templates.to_referee(d)

def clean_up_data(d):
    utils.dict_remove(d, 'resume', 'submit', 'referee_email_again', 'email_again')
    d.gender = d.gender.lower() 
    d.pascal_member = d.pascal_member == 'Yes';
    d.travel_support = d.get('travel_support', None) and True or None;
    d.affiliated = d.get('affiliated', False) != False;

def is_email_available(email):
    return not db.select(

# Path: app/helpers/utils.py
# def dict_remove(d, *keys):
# def capitalize_first(str):
# def make_unique_md5():
# def get_all_functions(module):
# def sqlands(left, lst):
# def get_ip():
# def store(tablename, _test=False, **values):
# 
# Path: app/helpers/email_templates.py
# def to_applicant(applicant):
# def to_applicant_simple(applicant):
# def to_referee(applicant):
# def notify_applicant(applicant):
# def notify_referee(applicant):
# def resend_password(user):


# Path: app/models/submissions.py
import web
from config import db
from app.helpers import utils
from app.helpers import email_templates
from cStringIO import StringIO
from datetime import date


def submit_application(resume, d, simple=False):
    # clean up some fields
    clean_up_data(d)
    # handle the file upload
    d.resume_fn = save_resume(resume, d.first_name, d.last_name)
    # in simple mode, no reference is required
    if simple:
        # save the data in the db
        add_applicant(d)
        # email user and referee
        email_templates.to_applicant_simple(d)
    else:
        # make secret md5
        d.secret_md5 = utils.make_unique_md5()
        # save the data in the db
        add_applicant(d)
        # email user and referee
        email_templates.to_applicant(d)
        email_templates.to_referee(d)

def clean_up_data(d):
    utils.dict_remove(d, 'resume', 'submit', 'referee_email_again', 'email_again')
    d.gender = d.gender.lower() 
    d.pascal_member = d.pascal_member == 'Yes';
    d.travel_support = d.get('travel_support', None) and True or None;
    d.affiliated = d.get('affiliated', False) != False;

def is_email_available(email):
    return not db.select(

        'applicants', 