# Path: app/models/users.py
# def create_account(email, password, nickname):
# def get_user_by_email(email):
# def is_email_available(email):
# def is_valid_password(password):
# def is_correct_password(email, password):
# def update(id, **kw):
# def get_user_by_id(id):
# 
# Path: app/models/applicants.py
# def get_where(context='', query=None, user_id=None):
# def query(context='', query=None, offset=0, limit=50, order='creation_ts desc', user_id=None):
# def get_by_id(id):
# def admit(ids, user_id):
# def reject(ids, user_id):
# def undecide(ids, user_id):
# def grant(user_id, id, amount):
# def add_comment(id, comment):
# def rate(ids, score):
# def get_counts():
# def get_by_secret_md5(secret_md5):
# def get_dummy_record():
# def get_stats():


import web
from config import db
from app.models import users
from app.models import applicants
def login(email):
    s = get_session()
    for k, v in users.get_user_by_email(email).items():
        s[k] = v
    s.is_logged = True
    
def logout():
    get_session().kill()
    
def reset():
    user = users.get_user_by_id(get_user_id())
    login(user.email)
    
def get_user_id():
    return get_session().id    

def get_last_visited_url():
    redirect_url = web.cookies(redirect_url='/').redirect_url
    web.setcookie('redirect_url', '', expires='')
    return redirect_url

def set_last_visited_url():
    url = web.ctx.get('path')
    if url:
        web.setcookie('redirect_url', url)

def login_required(meth):
    def new(*args):
        if not is_logged():
            return web.redirect('/account')

# Path: app/models/users.py
# def create_account(email, password, nickname):
# def get_user_by_email(email):
# def is_email_available(email):
# def is_valid_password(password):
# def is_correct_password(email, password):
# def update(id, **kw):
# def get_user_by_id(id):
# 
# Path: app/models/applicants.py
# def get_where(context='', query=None, user_id=None):
# def query(context='', query=None, offset=0, limit=50, order='creation_ts desc', user_id=None):
# def get_by_id(id):
# def admit(ids, user_id):
# def reject(ids, user_id):
# def undecide(ids, user_id):
# def grant(user_id, id, amount):
# def add_comment(id, comment):
# def rate(ids, score):
# def get_counts():
# def get_by_secret_md5(secret_md5):
# def get_dummy_record():
# def get_stats():


# Path: app/helpers/session.py
import web
from config import db
from app.models import users
from app.models import applicants

def login(email):
    s = get_session()
    for k, v in users.get_user_by_email(email).items():
        s[k] = v
    s.is_logged = True
    
def logout():
    get_session().kill()
    
def reset():
    user = users.get_user_by_id(get_user_id())
    login(user.email)
    
def get_user_id():
    return get_session().id    

def get_last_visited_url():
    redirect_url = web.cookies(redirect_url='/').redirect_url
    web.setcookie('redirect_url', '', expires='')
    return redirect_url

def set_last_visited_url():
    url = web.ctx.get('path')
    if url:
        web.setcookie('redirect_url', url)

def login_required(meth):
    def new(*args):
        if not is_logged():
            return web.redirect('/account')

        return meth(*args)