def login():
    current_app.logger.debug('Inside login route of users blueprint')
    user_details = Users.get_current_user()
    user_error_message = None
    pass_error_message = None
    loginform = LoginForm()

    if loginform.validate_on_submit():
        username = loginform.username.data
        password = loginform.password.data
        hashed_password = generate_password_hash(password, method='sha256')
        user_result = Users.get_by_name(name=username)
        if user_result:
            returned_password = user_result.password
            if check_password_hash(returned_password, password):
                session['username'] = user_result.name
                return redirect(url_for('questions.index'))
            else:
                current_app.logger.error('passwords are not matching')
                pass_error_message = "Passwords don't match"
        else:
            current_app.logger.error('User does not exist')
            user_error_message = "User does not exist!"

    return render_template('login.html',
                           user=user_details,
                           user_error=user_error_message,
                           pass_error=pass_error_message,
                           form=loginform)
예제 #2
0
def get_experts():
    experts = Users.get_experts()
    expertlist = []
    for expert in experts:
        t = (str(expert['id']), expert['name'])
        expertlist.append(t)
    return expertlist
def users():
    current_app.logger.debug('Inside /users route of users views')
    user_details = Users.get_current_user()
    if user_details:
        if user_details.admin:
            users = Users.get_all_nonadmin_users()
            return render_template('users.html',
                                   user=user_details,
                                   users=users)
        else:
            current_app.logger.debug(
                'Redirecting to index route of questions blueprint')
            return redirect(url_for('questions.index'))
    else:
        current_app.logger.debug(
            'Redirecting to login route of users blueprint')
        return redirect(url_for('users.login'))
def register():
    current_app.logger.debug('Inside register route of users blueprint')
    user_details = Users.get_current_user()
    error_message = None
    registerform = RegisterForm()
    if registerform.validate_on_submit():
        username = registerform.username.data
        password = registerform.password.data
        existing_user = Users.get_by_name(name=username)
        if not existing_user:
            hashed_password = generate_password_hash(password, method='sha256')
            new_user = Users(name=username,
                             password=hashed_password,
                             expert=False,
                             admin=False)
            new_user.save_to_db()
            session['username'] = username
            if username == 'admin':
                admin_user = Users.get_by_name(name='admin')
                admin_user.admin = True
                admin_user.save_to_db()
            current_app.logger.debug(
                'Redirecting to index route of questions blueprint')
            return redirect(url_for('questions.index'))
        else:
            current_app.logger.error('User already exists')
            error_message = "User already exists!"

    return render_template('register.html',
                           user=user_details,
                           error=error_message,
                           form=registerform)
def promote(user_id):
    current_app.logger.debug('Inside promote route of users blueprint')
    user_details = Users.get_current_user()
    if user_details:
        if user_details.admin:
            user_details = Users.get_by_id(user_id)
            if user_details.expert:
                user_details.expert = False
            else:
                user_details.expert = True
            user_details.save_to_db()
            current_app.logger.debug(
                'Redirecting to users route of users blueprint')
            return redirect(url_for('users.users'))
        else:
            current_app.logger.debug(
                'Redirecting to index route of questions blueprint')
            return redirect(url_for('questions.index'))
    else:
        return redirect(url_for('login'))
def unanswered():
    current_app.logger.debug('Inside unanswered route of questions blueprint')
    user_details = Users.get_current_user()
    if user_details:
        if user_details.expert:
            unanswered_questions = Questions.get_unanswered_questions(user_details.id)
            return render_template('unanswered.html',user=user_details,questions=unanswered_questions)
        else:
            current_app.logger.debug('Redirecting to index route of questions blueprint')
            return redirect(url_for('questions.index'))
    else:
        current_app.logger.debug('Redirecting to login route of users blueprint')
        return redirect(url_for('users.login'))
def ask():
    current_app.logger.debug('Inside ask route of questions blueprint')
    user_details = Users.get_current_user()

    askform = AskForm()

    if request.method == "POST":
        if askform.validate_on_submit():
            question = askform.question.data
            expert_id = askform.expert.data
            ask_by_id = user_details.id
            new_question = Questions(question_text=question,asked_by_id=ask_by_id,expert_id=expert_id)
            new_question.save_to_db()
            current_app.logger.debug('Redirecting to index route of questions blueprint')
            return redirect(url_for('questions.index'))

    if user_details:
        return render_template('ask.html',user=user_details,form=askform)
    else:
        current_app.logger.debug('Redirecting to login route of users blueprint')
        return redirect(url_for('users.login'))
def answer(question_id):
    current_app.logger.debug('Inside answer route for question id : %s',str(question_id))
    user_details = Users.get_current_user()
    answerform = AnswerForm()

    if answerform.validate_on_submit():
        answer = answerform.answer.data
        question = Questions.get_question_details(question_id=question_id)
        question.answer_text = answer
        question.save_to_db()
        current_app.logger.debug('Redirecting to unanswered route of questions blueprint')
        return redirect(url_for('questions.unanswered'))

    if user_details:
        if user_details.expert:
            question = Questions.get_question_details(question_id=question_id)
            return render_template('answer.html',user=user_details,question=question,form=answerform)
        else:
            current_app.logger.debug('Redirecting to index route of questions blueprint')
            return redirect(url_for('questions.index'))
    else:
        current_app.logger.debug('Redirecting to login route of users blueprint')
        return redirect(url_for('users.login'))
def question(question_id):
    current_app.logger.debug('Inside question route of questions blueprint for question id %s',str(question_id))
    user_details = Users.get_current_user()
    details = Questions.view_question_details(question_id)
    return render_template('question.html',user=user_details,details=details)
def index():
    current_app.logger.debug('Inside index route of questions blueprint')
    user_details = Users.get_current_user()
    results = Questions.get_all_answered_questions_details()
    return render_template('home.html',user=user_details,results=results)