示例#1
0
def route_add_edit_tag(question_id):
    tags = data_manager.get_basic_tags()
    tag_id = data_manager.get_tag_by_question_id(question_id)
    if tag_id == []:
        tag_id = None
    else:
        tag_id = tag_id[0]['tag_id']
        tag_id = data_manager.get_record_by_id('tag', tag_id)
    if request.method == 'POST':
        ntag = request.form.to_dict()
        question_id = ntag['question_id']
        if ntag['define_own'] == '':
            del ntag['define_own']
            ntag['name'] = ntag.pop('select')
        else:
            del ntag['select']
            ntag['name'] = ntag.pop('define_own')
        if ntag['tag_id'] == '':
            data_manager.insert_new_tag(ntag)
        else:
            data_manager.update_tag(ntag)
        return redirect(f'/question/{question_id}')
    return render_template('add-edit-tag.html',
                           question_id=question_id,
                           tags=tags,
                           tag_id=tag_id)
示例#2
0
def route_delete_answer(answer_id):
    current_answer = data_manager.get_record_by_id('answer', answer_id)[0]
    question_id = current_answer['question_id']

    data_manager.delete_single_answer_by_id(answer_id)
    data_manager.update_view_counter(question_id, -1)
    return redirect(url_for('route_show_question', question_id=question_id))
示例#3
0
def route_edit_answer(answer_id):
    current_answer = data_manager.get_record_by_id('answer', answer_id)[0]
    current_answer['message'] = current_answer['message'].replace('<br/>', "")
    username = session['username']

    if request.method == 'POST':
        edited_answer = {
            'message': request.form['message'].replace('\n', '<br/>'),
            'image': current_answer['image']
        }

        question_id = int(request.form['question_id'])

        if len(request.files) > 0:
            if request.files['image'].filename != "":
                current_image_name = str(request.files['image'])
                normal_image_name = data_manager.get_back_image_name(
                    current_image_name)
                edited_answer['image'] = normal_image_name

                file = request.files['image']
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        data_manager.update_answer(edited_answer, answer_id)
        data_manager.update_view_counter(question_id, -1)
        return redirect(url_for('route_show_question',
                                question_id=question_id))

    return render_template('edit_answer.html',
                           answer=current_answer,
                           username=username,
                           answer_id=answer_id)
示例#4
0
def route_show_question(question_id):
    data_manager.update_view_counter(question_id, 1)
    current_question = data_manager.get_record_by_id('question',
                                                     question_id)[0]
    current_answers = data_manager.get_answers_by_question_id(question_id)
    number_of_answers = len(current_answers)
    current_question_comments = data_manager.get_comments_by_question_id(
        question_id)
    current_answer_comments = data_manager.get_answer_comments()

    if 'username' in session:
        username = session['username']
        return render_template('show_question.html',
                               question_id=question_id,
                               question=current_question,
                               answers=current_answers,
                               question_comments=current_question_comments,
                               answer_comments=current_answer_comments,
                               number_of_answers=number_of_answers,
                               username=username)

    return render_template('show_question.html',
                           question_id=question_id,
                           question=current_question,
                           answers=current_answers,
                           question_comments=current_question_comments,
                           answer_comments=current_answer_comments,
                           number_of_answers=number_of_answers)
示例#5
0
def route_delete_comment(comment_id):
    if request.method == 'POST':
        comment = data_manager.get_record_by_id('comment', comment_id)
        data_manager.delete_by_id('comment', comment_id)
        if comment['question_id'] is not None:
            return redirect(f"/question/{comment['question_id']}")
        return redirect(f"/answer/{comment['answer_id']}")
    return render_template('warning.html', message='Action denied')
示例#6
0
def route_answer_votes(answer_id, operation):
    if request.method == 'POST':
        op_change = '+1' if operation == 'up' else '-1'
        data_manager.update_vote_number('answer', op_change, answer_id)
        answer = data_manager.get_record_by_id('answer', answer_id)
        return redirect(
            url_for('route_question_display',
                    question_id=answer['question_id']))
    return redirect('')
示例#7
0
def route_edit_question(question_id):
    edited_question = request.form.to_dict()
    if edited_question:
        data_manager.update_question(question_id, **edited_question)
        return redirect(
            url_for('route_question_display', question_id=question_id))
    question = data_manager.get_record_by_id("question", question_id)
    return render_template('add-edit.html',
                           data=question,
                           type='question',
                           id=question_id)
示例#8
0
def route_edit_answer(answer_id):
    question_id = request.form.get('question_id')
    if question_id is not None:
        message = request.form.get('message')
        image = request.form.get('image')
        data_manager.update_answer(message, image, answer_id)
        return redirect(
            url_for('route_question_display', question_id=question_id))
    answer = data_manager.get_record_by_id('answer', answer_id)
    return render_template('add-edit.html',
                           data=answer,
                           id=answer_id,
                           type='answer')
示例#9
0
def route_edit_comment(comment_id):
    message = request.form.get('message')
    if message:
        edited_count = request.form.get('edited_count')
        edited_count = 1 if edited_count == 'None' else int(edited_count) + 1
        new_values = {'edited_count': edited_count, 'message': message}
        data_manager.update_comment_by_primary_id(new_values, comment_id)
        parent_id = request.form.get('question_id')
        if parent_id:
            return redirect(
                url_for('route_question_display', question_id=parent_id))
        parent_id = request.form.get('answer_id')
        return redirect(url_for('route_answer_display', answer_id=parent_id))
    comment = data_manager.get_record_by_id('comment', comment_id)
    return render_template('add-edit.html',
                           data=comment,
                           id=comment_id,
                           type='comment')
示例#10
0
def route_question_display(question_id):
    tag = data_manager.get_tag_by_question_id(question_id)
    if tag == []:
        current_tag = None
    else:
        tag = tag[0]['tag_id']
        current_tag = data_manager.get_record_by_id("tag", tag)
    template_name = "record-details.html"
    question = data_manager.get_question_with_user_info(question_id)
    if question is None:
        return render_template(template_name, question_id=question_id)
    answers = data_manager.get_answers_by_question_id(question_id)
    comments = data_manager.get_comment_by_parent_id('question_id',
                                                     question_id)
    return render_template(template_name,
                           question=question,
                           answers=answers,
                           comments=comments,
                           tag=current_tag)
示例#11
0
def route_delete_answer(answer_id):
    answer = data_manager.get_record_by_id('answer', answer_id)
    question_id = answer['question_id']
    data_manager.delete_by_id('answer', answer_id)
    return redirect(url_for('route_question_display', question_id=question_id))