示例#1
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        email = request.form['email']
        db = get_db()
        error = None

        if not username:
            error = "Username required"
        elif not password:
            error = "Password required"
        elif not email:
            email = "Email required"
        elif db.execute("SELECT id FROM user WHERE username = ? OR email = ?",
                        (username, email)).fetchone() is not None:
            error = "Username {} is already registered".format(username)

        if error is None:
            db.execute(
                "INSERT INTO user (username, password, email) VALUES (?, ?, ?)",
                (username, generate_password_hash(password), email))
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
示例#2
0
def index():
    db = get_db()
    posts = db.execute(
        'SELECT q_id, author_id, created, title, body, username'
        ' FROM question JOIN user ON question.author_id = user.id'
        ' ORDER BY created DESC').fetchall()
    return render_template('posts/index.html', posts=posts)
示例#3
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM user WHERE id = ?',
                                  (user_id, )).fetchone()
示例#4
0
def ask():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = "Title Required"
        if not body:
            error = "Explain Your question "
        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'INSERT INTO question (title, body, author_id)'
                'VALUES (?, ?, ?)', (title, body, g.user['id']))
            db.commit()
            return redirect(url_for('index'))

    return render_template('posts/ask.html')
示例#5
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()

        if user is None:
            error = "Incorrect Username"
        elif not check_password_hash(user['password'], password):
            error = "Incorrect Password"

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')