def query():
    setting = sql.init_setting()
    if request.method == 'GET':
        if 'ADSUSERID' in request.cookies:
            uid = request.cookies.get('ADSUSERID')
            data = sql.init_user(uid)
            return render_template('query.html',
                                   is_signup=True,
                                   data=data,
                                   setting=setting)
        else:
            return redirect(url_for('info'))
    elif request.method == 'POST':
        uid = uuid.uuid4().hex.upper()
        result = request.form.to_dict()
        if not any(isBlank(value) for value in result.values()):
            sql.add_user(uid, result['name'], result['tel'], result['mail'],
                         result['province'], result['category'],
                         result['score'], result['school'])
            data = sql.init_user(uid)
            resp = make_response(redirect(url_for('query')))
            resp.set_cookie('ADSUSERID', uid,
                            max_age=60 * 60 * 24 * 365)  # Max age is 365 days.
            return resp
        else:
            return json.dumps({
                'message': 'fail',
                'error_code': '2001',
                'error_desc': 'String is blank'
            })
    return "success"
def info():
    setting = sql.init_setting()
    if request.method == 'GET':
        if 'ADSUSERID' in request.cookies:
            uid = request.cookies.get('ADSUSERID')
            data = sql.init_user(uid)
            return redirect(url_for('query'))
        return render_template('info.html', setting=setting)
def login():
    if request.method == 'POST':
        result = request.form.to_dict()
        if not any(isBlank(value) for value in result.values()):
            username = result['username']
            password = result['password']
            try:
                data = sql.admin(username)
                hash = data['password']
                if pbkdf2_sha256.verify(password, hash):
                    user = User(username)
                    flask_login.login_user(user)
                    return redirect(url_for('index'))
                else:
                    return redirect(url_for('login'))
            except Exception as e:
                return redirect(url_for('login'))
    else:
        setting = sql.init_setting()
        return render_template('login.html', setting=setting)
def setting():
    setting = sql.init_setting()
    if request.method == 'GET':
        return render_template('setting.html', setting=setting)
    elif request.method == 'POST':
        data = request.form.to_dict()
        if not any(isBlank(value) for value in data.values()):
            sql.new_setting(data['brand'], data['backend_brand'],
                            data['contact'], data['message'],
                            data['placeholder'])
            return json.dumps(
                {
                    'status': 'success',
                    'message': '设置成功',
                    'info': '请刷新页面查看效果'
                },
                ensure_ascii=False)
        return json.dumps(
            {
                'status': 'error',
                'message': '设置失败',
                'info': '请检查你的输入框是否为空'
            },
            ensure_ascii=False)
def mobile():
    setting = sql.init_setting()
    return render_template('mobile.html', setting=setting)
def stat():
    setting = sql.init_setting()
    dashboard_data = sql.get_dashboard_data()
    return render_template('stat.html',
                           setting=setting,
                           dashboard_data=dashboard_data)
def message():
    setting = sql.init_setting()
    return render_template('message.html', setting=setting)
def score():
    setting = sql.init_setting()
    return render_template('score.html', setting=setting)
def library():
    setting = sql.init_setting()
    return render_template('library.html', setting=setting)