def add_comment(type, qid): if type == 'question': comment_text = request.form["commenttext"] user_id = connection.get_user_by_name( session.get('username')).get('id') connection.add_comment('question_id', qid, comment_text, user_id) elif type == 'answer': comment_text = request.form["commenttext"] user_id = connection.get_user_by_name( session.get('username')).get('id') connection.add_comment('answer_id', qid, comment_text, user_id) qid = request.form['question_id'] return redirect(f"/question/{qid}")
def login(): if request.method == 'POST': print('POST request received!') username = request.form['username'] password_input = request.form['password'] if not connection.get_user_by_name(username): return redirect("/error/invalid_login") try: user_pw = connection.get_user_password(username).get('password') except AttributeError: return redirect(session['currenturl']) if data_manager.verify_password(password_input, user_pw): session['username'] = username return redirect(session['currenturl']) else: return redirect("/error/invalid_login")
def route_submit_question(): if 'username' in session: if request.method == 'POST': print('POST request received!') title = request.form['title'] message = request.form['question'] image = request.form['image'] user_id = connection.get_user_by_name( session.get('username')).get('id') question = connection.add_question(title, message, image, user_id) return redirect(url_for('route_question', qid=question['id'])) else: return render_template('form.html', colour=data_manager.get_style( session.get('username'))) else: return redirect('/error/login_error')
def route_question(qid): session['currenturl'] = request.url editable = data_manager.check_for_edit_or_save(qid) questions = connection.get_all_questions('id', "", 0) returned_question = data_manager.get_question_to_show(qid, questions) user = connection.get_user(returned_question.get('user_id')) user_id = False if 'username' in session: user_id = connection.get_user_by_name( session.get('username')).get('id') connection.update_view_number(qid) return render_template('question.html', question=returned_question, editable=editable, user_name=user['username'], user_id=user_id, sessionusername=session.get('username'), profile_id=user['id'], profile_picture=user['profile_picture'], colour=data_manager.get_style( session.get('username')))
def answer(qid): answer_text = request.form["answertext"] user_id = connection.get_user_by_name(session.get('username')).get('id') connection.add_answer(qid, answer_text, user_id) return redirect(f"/question/{qid}")