# 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 save_resume(resume, first_name, last_name):
    ext = resume.filename.split('.')[-1]
    fname = '%s.%s.%s.resume.%s' % (first_name, last_name, date.today(), ext)
    fname = fname.replace(' ', '-')
    open(resume_dir + fname, 'wb').write(resume.value)
    return fname

def add_applicant(d):
    db.insert('applicants', **d)

def submit_reference(applicant, d):
    # clean up some fields
    utils.dict_remove(d, 'submit')
    # add the provided reference
    add_reference(applicant.secret_md5, d)
    # notify user and referee by email
    email_templates.notify_applicant(applicant)
    email_templates.notify_referee(applicant)

def get_by_secret_md5(secret_md5):
    return web.listget(db.select(
        'applicants', 
        vars = dict(md5=secret_md5),
        where = 'secret_md5 = $md5'), 0, False)

# TODO: set update_ts as well
def add_reference(secret_md5, d):
    db.update(
        'applicants', 
        vars = dict(md5=secret_md5),

# 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 save_resume(resume, first_name, last_name):
    ext = resume.filename.split('.')[-1]
    fname = '%s.%s.%s.resume.%s' % (first_name, last_name, date.today(), ext)
    fname = fname.replace(' ', '-')
    open(resume_dir + fname, 'wb').write(resume.value)
    return fname

def add_applicant(d):
    db.insert('applicants', **d)

def submit_reference(applicant, d):
    # clean up some fields
    utils.dict_remove(d, 'submit')
    # add the provided reference
    add_reference(applicant.secret_md5, d)
    # notify user and referee by email
    email_templates.notify_applicant(applicant)
    email_templates.notify_referee(applicant)

def get_by_secret_md5(secret_md5):
    return web.listget(db.select(
        'applicants', 
        vars = dict(md5=secret_md5),
        where = 'secret_md5 = $md5'), 0, False)

# TODO: set update_ts as well
def add_reference(secret_md5, d):
    db.update(
        'applicants', 
        vars = dict(md5=secret_md5),

        where = 'secret_md5=$md5', 