Ejemplo n.º 1
0
def register():
    if request.method == 'POST':
    #如果用户提交了表单,那么 request.method 将会是 'POST'
        username = request.form['username']
        password = request.form['password']
        password_re = request.form['password_re']
        db = get_db()
        error = None

        if not username:
            error = '请输入用户名'
        elif not (password or password_re):
            error = '请输入密码'
        elif password != password_re:
            error = '两次输入密码不一致'
        elif db.execute(
            'SELECT userID FROM user WHERE username = ?',(username,)
        ).fetchone() is not None:
            error = '该用户名已被使用'

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

        flash(error)

    return render_template('auth/register.html')
Ejemplo n.º 2
0
def edit_user():
    userID = session.get('userID')
    user = get_user(userID)

    if request.method == 'POST':
        userID = session.get('userID')
        nickname = request_form(user, 'nickname')
        if request.files.get('headImg') is not None:
            headImg = request.files['headImg']
            uuid_str = uuid.uuid4().hex
            img_name = uuid_str + '.jpg'
            basepath = os.path.dirname(__file__)
            headurl = os.path.join('/static/img/uploads',
                                   secure_filename(img_name))
            headImg.save(
                os.path.join(basepath, 'static/img/uploads',
                             secure_filename(img_name)))
        else:
            headurl = user['headurl']

        sign = request_form(user, 'sign')
        db = get_db()
        db.execute(
            'UPDATE user SET nickname = ? , sign = ?, headurl= ?'
            ' WHERE userID = ?', (nickname, sign, headurl, userID))
        db.commit()
        return redirect(url_for('user.edit_user'))

    elif request.method == 'GET':
        return render_template('mainPage/user_edit.html', user=user)
Ejemplo n.º 3
0
def add_answer(questionID):
    question = get_question(questionID)
    answers = get_answers(questionID)
    userID = session.get('userID')
    user = get_user(userID)

    if request.method == 'GET':
        return render_template('mainPage/question.html',
                               question=question,
                               answers=answers,
                               user=user)

    elif request.method == 'POST':
        if request.form.get('answer') is not None:
            answer = request.form['answer']
            error = None

            if answer == '' or (answers is None):
                error = '请输入回答'
            if error is not None:
                flash(error)
            else:
                db = get_db()
                db.execute(
                    'INSERT INTO answer(userID, questionID, answer) VALUES (?, ?, ?)',
                    (userID, questionID, answer))
                db.commit()

                return redirect(
                    url_for('question.add_answer', questionID=questionID))

        elif request.form.get('ask_title') is not None:
            return redirect(url_for('mainPage.asking'))
Ejemplo n.º 4
0
def get_all_answers():
    answers = get_db().execute(
        'SELECT a.questionID,answer, a.created, upvote,q.title,u.nickname,u.sign,u.headurl'
        ' FROM answer a'
        ' JOIN question q ON a.questionID = q.questionID'
        ' JOIN user u ON u.userID = a.userID'
        ' ORDER BY a.created DESC ', ).fetchall()
    return answers
Ejemplo n.º 5
0
def load_logged_in_user():
    userID = session.get('userID')

    if userID is None:
        g.user = None
    else:
        g.user = get_db().execute(
            'SELECT * FROM user WHERE userID = ?',(userID,)
        ).fetchone()
Ejemplo n.º 6
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 = "账号或密码错误"
        elif not check_password_hash(user['password'],password):
            error = '账号或密码错误'

        if error is None:
            session.clear()
            session['userID'] = user['userID']
            return redirect(url_for('mainPage.asking'))

        flash(error)

    return render_template('auth/login.html')
Ejemplo n.º 7
0
def asking():
    answers = get_all_answers()
    userID = session.get('userID')
    user = get_user(userID)

    if request.method == 'POST':
        ask_title = request.form['ask_title']
        ask_detail = request.form['ask_detail']
        db = get_db()
        error = None

        if not ask_title:
            error = '请输入问题'
        elif len(ask_title) >= 33:
            error = '问题不能超过33个字'
        elif db.execute('SELECT title FROM question WHERE title = ?',
                        (ask_title, )).fetchone() is not None:
            error = '已经存在该问题'

        if error is not None:
            flash(error)
        else:
            db.execute('INSERT INTO question (title, detail)'
                       ' VALUES (?, ?)', (ask_title, ask_detail))
            ids = db.execute(
                'select questionID from question order by questionID desc;'
            ).fetchall()
            questionID = ids[0][0]
            db.commit()

            return redirect(
                url_for('question.add_answer', questionID=questionID))

    elif request.method == 'GET':
        return render_template('mainPage/frontPage.html',
                               user=user,
                               answers=answers)
Ejemplo n.º 8
0
def get_user(userID):
    user = get_db().execute(
        'SELECT userID, nickname, sign, headurl '
        ' FROM user'
        ' WHERE userID = ?', (userID, )).fetchone()
    return user
Ejemplo n.º 9
0
def get_question(questionID):
    question = get_db().execute(
        'SELECT questionID, title, detail, created,commentCount '
        ' FROM question'
        ' WHERE questionID = ?', (questionID, )).fetchone()
    return question
Ejemplo n.º 10
0
def get_questions():
    questions = get_db().execute(
        'SELECT questionID,title'
        ' FROM question'
        ' ORDER BY created DESC ', ).fetchall()
    return questions