# 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/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/helpers/session.py
# def add_sessions_to_app(app):
# def get_session():
# def is_logged():
# def login(email):
# def logout():
# def reset():
# def get_user_id():
# def get_last_visited_url():
# def set_last_visited_url():
# def login_required(meth):
#     def new(*args):
# def login_required_for_reviews(meth):
#     def new(*l):


import web
import config
from web import form
from app.models import applicants
from app.models import users
from app.helpers import session
from config import view
vemail = form.regexp(r'.+@.+', 'Please enter a valid email address')
email_form = form.Form(
    form.Textbox('email', 
        form.notnull, vemail,
        form.Validator('This email address is already taken.', 
        lambda x: users.is_email_available(x)),
        description='Your new email:'),
    form.Button('submit', type='submit', value='Change your email')
)

def render_settings(nickname_form=nickname_form(), email_form=email_form(), password_form=password_form(), on_success_message=''):
    counts = applicants.get_counts()
    user = session.get_session()
    
    return view.base(
        view.settings(user, nickname_form, email_form, password_form, on_success_message)
    )
	
class index:
    @session.login_required
    def GET(self):
        return render_settings()

class change_nickname:
    @session.login_required
    def POST(self):
        f = self.form()
        if not f.validates(web.input(_unicode=False)):
            return render_settings(nickname_form=f)
        else:

# 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/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/helpers/session.py
# def add_sessions_to_app(app):
# def get_session():
# def is_logged():
# def login(email):
# def logout():
# def reset():
# def get_user_id():
# def get_last_visited_url():
# def set_last_visited_url():
# def login_required(meth):
#     def new(*args):
# def login_required_for_reviews(meth):
#     def new(*l):


# Path: app/controllers/settings.py
import web
import config
from web import form
from app.models import applicants
from app.models import users
from app.helpers import session
from config import view

vemail = form.regexp(r'.+@.+', 'Please enter a valid email address')
email_form = form.Form(
    form.Textbox('email', 
        form.notnull, vemail,
        form.Validator('This email address is already taken.', 
        lambda x: users.is_email_available(x)),
        description='Your new email:'),
    form.Button('submit', type='submit', value='Change your email')
)

def render_settings(nickname_form=nickname_form(), email_form=email_form(), password_form=password_form(), on_success_message=''):
    counts = applicants.get_counts()
    user = session.get_session()
    
    return view.base(
        view.settings(user, nickname_form, email_form, password_form, on_success_message)
    )
	
class index:
    @session.login_required
    def GET(self):
        return render_settings()

class change_nickname:
    @session.login_required
    def POST(self):
        f = self.form()
        if not f.validates(web.input(_unicode=False)):
            return render_settings(nickname_form=f)
        else:

            users.update(session.get_user_id(), nickname=f.d.nickname)