# 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
# - because sessions are attached to an app, every user has sessions whether they atually need it or not.
# - login required decorator should save intended user action before asking to login.



def add_sessions_to_app(app):
    if web.config.get('_session') is None:
        store = web.session.DBStore(db, 'sessions')
        session = web.session.Session(app, store, 
            initializer={'is_logged' : False})
        web.config._session = session
    else:
        session = web.config._session

def get_session():
    return web.config._session

def is_logged():
    return get_session().is_logged

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():

# 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

# - because sessions are attached to an app, every user has sessions whether they atually need it or not.
# - login required decorator should save intended user action before asking to login.



def add_sessions_to_app(app):
    if web.config.get('_session') is None:
        store = web.session.DBStore(db, 'sessions')
        session = web.session.Session(app, store, 
            initializer={'is_logged' : False})
        web.config._session = session
    else:
        session = web.config._session

def get_session():
    return web.config._session

def is_logged():
    return get_session().is_logged

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())