Exemple #1
0
def add_question():
    table_head = data_manager.get_table_head('question')
    if request.method == 'POST':
        new_record = {
            table_head[1]:
            str(strftime("%Y-%m-%d %H:%M:%S", gmtime())),
            table_head[2]:
            '0',
            table_head[3]:
            '0',
            table_head[4]:
            request.form[table_head[4]],
            table_head[5]:
            request.form[table_head[5]],
            table_head[6]:
            f'{request.form[table_head[6]] if request.form[table_head[6]] else None}'
        }
        data_manager.insert_record('question', new_record)
        new_record_id = data_manager.select_sql(
            'question',
            column='id',
            clause='WHERE',
            condition=[table_head[1], '=', new_record[table_head[1]]])
        return redirect(f'/question/{new_record_id[0]["id"]}')
    return render_template('new_question.html', table_head=table_head)
Exemple #2
0
def add_comment(table_name, id_):
    comment_head = data_manager.get_table_head('comment')
    if request.method == 'POST':
        if table_name == 'question':
            new_record = {
                comment_head[1]: id_,
                comment_head[3]: request.form[comment_head[3]],
                comment_head[4]: str(strftime("%Y-%m-%d %H:%M:%S", gmtime())),
                comment_head[5]: '0'
            }
            data_manager.insert_record('comment', new_record)
        elif table_name == 'answer':

            new_record = {
                comment_head[1]: '0',
                comment_head[2]: id_,
                comment_head[3]: request.form[comment_head[3]],
                comment_head[4]: str(strftime("%Y-%m-%d %H:%M:%S", gmtime())),
                comment_head[5]: '0'
            }
            data_manager.insert_record('comment', new_record)
        return redirect(f'/{table_name}/{id_}')
    return render_template('new_comment.html',
                           table_head=comment_head,
                           table_name=table_name,
                           id=id_)
Exemple #3
0
def user_in_db(name, password):
    user = data_manager.select_query(table='users',
                                     clause='WHERE',
                                     condition=['name', '=', name])
    print(user)
    if not user:
        data_manager.insert_record('users', {
            'name': name,
            'password': hash.hash_password(password)
        })
        return {"registration_legit": True}
    return {"registration_legit": False}
def save_record(table_name):
    if "user" not in session:
        return "HTTP/1.1 401 Unauthorized"
    else:
        record_to_save = request.form.to_dict()
        if table_name == "boards":
            record_to_save["user_id"] = session["user"]["id"]
        new_record = data_manager.insert_record(table_name, record_to_save)
        json_record = json.dumps(new_record)
        return json_record
Exemple #5
0
def add_answer(question_id):
    table_head = data_manager.get_table_head('answer')
    if request.method == 'POST':
        new_record = {
            table_head[1]:
            str(strftime("%Y-%m-%d %H:%M:%S", gmtime())),
            table_head[2]:
            '0',
            table_head[3]:
            str(question_id),
            table_head[4]:
            request.form[table_head[4]],
            table_head[5]:
            f'{request.form[table_head[5]] if request.form[table_head[5]] else None}'
        }
        data_manager.insert_record('answer', new_record)
        return redirect(f'/question/{question_id}')
    return render_template('new_answer.html',
                           table_head=table_head,
                           question_id=question_id)
def register_user():
    new_user = request.form.to_dict()
    new_password_hash = password_handler.create_password_hash(
        new_user["password"])
    dict_of_user = {
        "user_name": new_user["user_name"],
        "password_hash": new_password_hash
    }
    try:
        session["user"] = data_manager.insert_record("users", dict_of_user)
        return redirect(url_for("boards"))
    except KeyError:
        return render_template("login.html",
                               error_message="User name already exists")
Exemple #7
0
def create_new_board():
    content = {}
    data = request.get_json()
    content['title'] = data
    return data_manager.insert_record(table_name='boards', records=content)