Beispiel #1
0
def add_question():
    if request.method == 'POST':
        id = data_handler.generate_new_id('question')
        submission = data_handler.current_time()
        view = 0
        vote = 0
        title = request.form['title'].capitalize()
        message = request.form['message'].capitalize()
        user_data = data_handler.get_user_data(session['username'])
        remove_list = 0
        user_id = user_data[remove_list]['id']
        if request.files:
            target = os.path.join(APP_ROUTE, 'static/')
            image = request.files['image']
            filename = ".".join([str(submission), "jpg"])
            image.save("/".join([target, filename]))

        data = {
            'id': id,
            'submission': submission,
            'view': view,
            'vote': vote,
            'title': title,
            'message': message,
            'filename': filename,
            'user_id': user_id
        }
        data_handler.write_question_data(data)

        return redirect(f'/question/{id}')
    return render_template('add-question.html',
                           button_text='Add new question',
                           title_text='Add a question',
                           action_text='/add-question')
Beispiel #2
0
def show_user_page():
    username = session["username"]
    user_id = data_handler.get_user_id(session["username"])
    user_questions = data_handler.get_user_questions(user_id)
    user_answers = data_handler.get_user_answers(user_id)
    user_data=data_handler.get_user_data(user_id)
    answer_comments = data_handler.read_comments()
    return render_template('user_page.html', user_data=user_data, username=username, user_questions=user_questions,answer_comments=answer_comments, user_answers=user_answers,fancy_word=None)
Beispiel #3
0
def add_comment_to_question(question_id):
    if request.method == 'POST':
        comment = request.form['comment']
        submission_time = data_handler.current_time()
        user_data = data_handler.get_user_data(session['username'])
        remove_list = 0
        user_id = user_data[remove_list]['id']
        data_handler.new_comment_for_question(question_id, comment,
                                              submission_time, user_id)
        return redirect(f'/question/{question_id}')
    return render_template('new_comment.html',
                           action=f'/question/{question_id}/new-comment',
                           title='Add comment')
Beispiel #4
0
def login():
    title_text = 'Login'

    if request.method == 'POST':
        email = request.form['email']
        input_password = request.form['password']
        correct_password = data_handler.get_user_data(email)
        remove_list = 0
        if correct_password \
                and data_handler.verify_password(input_password, correct_password[remove_list]['password']):
            session['username'] = email
            session['user_id'] = correct_password[remove_list]['id']
            return redirect(url_for('main_page'))
        else:
            error_message = "Wrong password or email!"
            return render_template('login.html',
                                   title_text=title_text,
                                   error_message=error_message)
    return render_template('login.html', title_text=title_text)
Beispiel #5
0
def login():
    if request.method == 'POST':
        user = {
            'username': request.form.get('username'),
            'password': request.form.get('password')
        }
        hashed_password = data_handler.get_password(user)
        verified = util.verify_password(user['username'],
                                        hashed_password[0]['password'])
        if verified:
            user_all_data = data_handler.get_user_data(user)
            session['user'] = user_all_data[0]['username']

            return redirect(url_for('route_list'))

        else:
            return redirect(url_for('route_list'))
    return render_template('login.html',
                           page_title='Login',
                           button_title='Login')
Beispiel #6
0
def new_answer(question_id):
    question_data = data_handler.get_single_question(question_id)

    if request.method == 'POST':
        id = data_handler.generate_new_id('answer')
        submission_time = data_handler.current_time()
        vote_number = 0
        question_id = question_id
        message = request.form['answer']
        user_data = data_handler.get_user_data(session['username'])
        remove_list = 0
        user_id = user_data[remove_list]['id']
        filename = None
        if request.files and request.files['image']:
            target = os.path.join(APP_ROUTE, 'static/')
            image = request.files['image']
            filename = ".".join([str(submission_time), "jpg"])
            image.save("/".join([target, filename]))

        data = {
            'id': id,
            'submission': submission_time,
            'vote': vote_number,
            'question_id': question_id,
            'message': message,
            'filename': filename,
            'user_id': user_id
        }
        data_handler.write_answer_data(data)

        return redirect(f'/question/{question_id}')
    return render_template('new-answer.html',
                           question_data=question_data,
                           question_id=question_id,
                           todo='Add',
                           action_text=f'/question/{question_id}/new-answer',
                           answer_data=None)
Beispiel #7
0
def user_page(username=None):
    if (username != user_functions.user_logged_in()) or (username is None):
        flash(VALIDATION_MESSAGES["no user logged in"])
        return redirect(url_for("main_page"))
    user_info = data_handler.get_user_data(username)
    return render_template(TEMPLATES_ROUTES["user page"], user_info=user_info, username=username)