Esempio n. 1
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        inviteCode = str(request.form['invitecode'])
        kuCode = request.form['kucode']
        steamNickNname = request.form['steamnickname']
        db = get_db_dict()
        db.execute('SELECT id FROM user WHERE username = %s', (username, ))
        username_result = db.fetchone()
        db.execute('SELECT code, used FROM invite_code WHERE code = %s',
                   (inviteCode, ))
        inviteCode_result = db.fetchone()

        error = None

        if not username:
            error = 'Username is required'
        elif not password:
            error = 'Password is required'
        elif not inviteCode:
            error = 'inviteCode is required'
        elif username_result is not None:
            error = 'User {} is already registered.'.format(username)
        elif inviteCode_result['code'] != inviteCode or inviteCode_result[
                'used']:
            error = 'invalid code.'
        if error is None:
            lv_ = inviteCode.split('+')
            lv = lv_[-1]
            commit_sql(
                'INSERT INTO user (username, password, level, kucode, steamnickname) VALUES (%s,%s,%s,%s,%s)',
                (
                    username,
                    generate_password_hash(password),
                    lv,
                    kuCode,
                    steamNickNname,
                ))
            commit_sql(
                'UPDATE invite_code SET used_by = %s, used = %s WHERE code = %s',
                (username, 1, inviteCode))
            error = 'Success!'
        flash(error)

    return redirect(url_for('index'))
def generate_random_key_d():
    for i in range(2):
        key_ = generate_random() + '+3'
        commit_sql('INSERT INTO invite_code (code)' ' VALUES (%s)', (key_, ))