Example #1
0
def answer(id_of_question):
    user = login_check()

    if not user:
        return redirect(url_for('index'))

    db = get_db()
    cur = db.execute(
        "select expert_id ,question_text from questions where id = ? and answer_text is not null",
        [id_of_question])
    question_result = cur.fetchone()

    if question_result:
        question_found = 'Yes'
        question_text = question_result['question_text']

        if question_result['expert_id'] != user['id']:
            return redirect(url_for('login'))

    else:
        question_found = None
        question_text = "Question has been answered."

    if request.method == "POST":
        answer_by_expert = request.form['answer_by_expert']

        db.execute(""" update questions set answer_text = ? where id = ? """,
                   [answer_by_expert, id_of_question])
        db.commit()
        return redirect(url_for('index'))
    return render_template('answer.html',
                           user=login_check(),
                           question_text=question_text,
                           id_of_question=id_of_question,
                           question_found=question_found)
Example #2
0
def index():
    # this user is different variable and user in session is different
    # user remains None until it is not in session(login)
    user = login_check()
    db = get_db()
    if not user:
        hide_it = True
    else:
        hide_it = False

    questions_cur = db.execute('''select 
    questions.question_text,
    questions.id as question_id,
    test_user.name as test_user_name, 
    expert.name as expert_name from questions 
    
    join users as test_user on test_user.id = questions.asked_by_id 
    join users as expert on expert.id = questions.expert_id 
    
    where questions.answer_text is not null''')

    questions_result = questions_cur.fetchall()

    return render_template('home.html',
                           user=user,
                           all_answered_questions=questions_result)
Example #3
0
def unanswered():
    user = login_check()
    if user is None or user['expert'] == 0:
        return redirect(url_for('index'))

    db = get_db()
    # here user id will id of expert from whom test user was asked questions
    expert_id = user['id']

    questions_cur = db.execute(
        '''select 
    questions.question_text as question_text,
    questions.id as question_id,
    users.name as name
    
    from questions join users
    on users.id = questions.asked_by_id
    
    where questions.answer_text is null 
    and questions.expert_id = ?''', [expert_id])

    question_data = questions_cur.fetchall()

    return render_template('unanswered.html',
                           user=user,
                           question_data=question_data)
Example #4
0
def users():
    user = login_check()
    if user is None or user['admin'] == 0:
        return redirect(url_for('index'))
    db = get_db()
    # get data of all users
    user_cur = db.execute('select id, name, expert, admin from users')
    user_result = user_cur.fetchall()
    return render_template('users.html', user=user, user_result=user_result)
Example #5
0
def login_check():
    user_result = None
    if 'user' in session:
        user = session['user']

        db = get_db()
        user_cur = db.execute('select * from users where name=?', [user])
        user_result = user_cur.fetchone()
        if user_result is not None:
            return user_result
Example #6
0
def promoted(user_id):
    db = get_db()
    cur = db.execute("select expert from users where id = ?", [user_id])
    exp = cur.fetchone()
    exp = exp['expert']
    if exp == 0:
        db.execute("update users set expert = 1 where id = ?", [user_id])
    elif exp == 1:
        db.execute("update users set expert = 0 where id = ?", [user_id])
    db.commit()
    return redirect(url_for('users'))
Example #7
0
def food():
    #Intialize the database
    db = get_db()

    if request.method == 'POST':
        name = request.form['food-name']
        protein = int(request.form['protein'])
        carbohydrates = int(request.form['carbohydrates'])
        fat = int(request.form['fat'])

        calories = protein * 4 + carbohydrates * 4 + fat * 9

        db.execute('insert into food (name, protein, carbohydrates, fat, calories) values (?, ?, ?, ?, ?)', \
            [name, protein, carbohydrates, fat, calories])
        db.commit()

    cur = db.execute(
        'select name, protein, carbohydrates, fat, calories from food')
    results = cur.fetchall()

    return render_template('add_food.html', results=results)
Example #8
0
def view(date):
    db = get_db()
    cur = db.execute(
        'select id, entry_date from log_date where entry_date = ?', [date])
    date_result = cur.fetchone()

    if request.method == 'POST':
        db.execute(
            'insert into food_date (food_id, log_date_id) values (?, ?)',
            [request.form['food-select'], date_result['id']])
        db.commit()

    d = datetime.strptime(str(date_result['entry_date']), '%Y%m%d')
    pretty_date = datetime.strftime(d, '%B %d, %Y')

    food_cur = db.execute('select id, name from food')
    food_results = food_cur.fetchall()

    log_cur = db.execute(
        '''select food.name, food.protein, food.carbohydrates, food.fat, food.calories 
                            from log_date 
                            join food_date on food_date.log_date_id = log_date.id 
                            join food on food.id = food_date.food_id where log_date.entry_date = ?''',
        [date])
    log_results = log_cur.fetchall()

    totals = {}
    totals['protein'] = 0
    totals['carbohydrates'] = 0
    totals['fat'] = 0
    totals['calories'] = 0

    for food in log_results:
        totals['protein'] += food['protein']
        totals['carbohydrates'] += food['carbohydrates']
        totals['fat'] += food['fat']
        totals['calories'] += food['calories']

    return render_template('day.html', entry_date=date_result['entry_date'], pretty_date=pretty_date, \
                            food_results=food_results, log_results=log_results, totals=totals)
Example #9
0
def index():
    db = get_db()
    if request.method == 'POST':
        date = request.form['date']
        dt = datetime.strptime(date, '%Y-%m-%d')
        database_date = datetime.strftime(dt, '%Y%m%d')

        db.execute('insert into log_date (entry_date) values(?)',
                   [database_date])
        db.commit()

    cur = db.execute(
        '''select log_date.entry_date, sum(food.protein) as protein, sum(food.carbohydrates) as carbohydrates, sum(food.fat) as fat, sum(food.calories) as calories 
                        from log_date 
                        join food_date on food_date.log_date_id = log_date.id 
                        join food on food.id = food_date.food_id 
                        group by log_date.id 
                        order by log_date.entry_date desc''')
    results = cur.fetchall()

    date_results = []

    for i in results:
        single_date = {}

        single_date['entry_date'] = i['entry_date']
        single_date['protein'] = i['protein']
        single_date['carbohydrates'] = i['carbohydrates']
        single_date['fat'] = i['fat']
        single_date['calories'] = i['calories']

        d = datetime.strptime(str(i['entry_date']), '%Y%m%d')
        single_date['pretty_date'] = datetime.strftime(d, '%B %d, %Y')

        date_results.append(single_date)
        print(single_date)

    return render_template('home.html', results=date_results)
Example #10
0
def register():
    user = login_check()
    if user is not None:
        return redirect(url_for('index'))
    warn = ""
    if request.method == "POST":
        db = get_db()
        name = request.form['name']
        password = request.form['password']

        cur = db.execute('select name as nm from users where name = ?', [name])
        cur = cur.fetchone()

        if cur:
            warn = "This Username is already used"
        else:
            hashed_password = generate_password_hash(password, method='sha256')
            db.execute(
                'insert into users (name, password, expert, admin) values (?, ?, ?, ?)',
                [name, hashed_password, 0, 0])
            db.commit()
            warn = "User has been registered"
    return render_template('register.html', warn=warn)
Example #11
0
def question(question_id):
    user = login_check()
    if user is None:
        return redirect(url_for('login'))
    db = get_db()
    cur = db.execute(
        '''select 
    questions.question_text,
    questions.answer_text,
    asked_by.name as asked_by,
    answered_by.name as answered_by
    
    from questions join users as asked_by
    on asked_by.id = questions.asked_by_id 

    join users as answered_by
    on answered_by.id = questions.expert_id 
    
    where questions.id = ? ''', [question_id])

    question_all_data = cur.fetchone()

    return render_template('question.html', user=user, qad=question_all_data)
Example #12
0
def login():
    user = login_check()
    if user is not None:
        return redirect(url_for('index'))

    warning = ''
    if request.method == 'POST':
        db = get_db()
        name = request.form['name']
        password = request.form['password']

        user_cur = db.execute(
            'select id, name, password from users where name=?', [name])
        user_result = user_cur.fetchone()
        if user_result is None:
            warning = 'Warning: User name not found...!'
        elif check_password_hash(user_result['password'], password):
            # session is really like dictionary but it can use with flask secret key
            session['user'] = user_result['name']
            return redirect(url_for('index'))

        else:
            warning = "Warning: Password is incorrect...!"
    return render_template('login.html', user=login_check(), warning=warning)
Example #13
0
def ask():
    user = login_check()

    if not user:
        return redirect(url_for('index'))
    elif user['expert'] != 0:
        return redirect(url_for('index'))

    db = get_db()
    cur = db.execute('select name, expert, id from users')
    user_names = cur.fetchall()

    if request.method == 'POST':
        expert_id, user_ques, user_id = request.form[
            'selection'], request.form['question'], user['id']

        db.execute(
            '''
        insert into questions 
        (question_text, asked_by_id, expert_id)
        values (?, ?, ?)''', [user_ques, user_id, expert_id])
        db.commit()

    return render_template('ask.html', user_names=user_names, user=user)