Exemple #1
0
def path_item(path_id):
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    path_items = session.query(PathItems).filter_by(user_id=user.id,
                                                    path_id=path_id).all()
    if path_items:
        return template('path_items', path=path_items, path_id=path_id)
    else:
        return template('path_no_items', path_id=path_id)
Exemple #2
0
def delete_path_name(path_id):
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    path = session.query(PathName).filter_by(user_id=user.id,
                                             id=path_id).first()

    if request.GET.delete:
        session.delete(path)
        return redirect('/path')
    return template('path_delete', path=path)
Exemple #3
0
def edit_path_name(path_id):
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    old_path_name = session.query(PathName).filter_by(user_id=user.id,
                                                      id=path_id).first()
    if request.GET.edit:
        path_name_edited = request.GET.path_name.strip()
        old_path_name.name = path_name_edited
        return redirect('/path')
    else:
        return template('path_name_edit', old_path_name=old_path_name)
Exemple #4
0
def path_names():
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    if user:
        path_names_all = session.query(PathName).filter_by(
            user_id=user.id).all()

        return template('path_names', path_names=path_names_all)

    else:
        return redirect('/login')
Exemple #5
0
def delete_path_item(path_id, item_id):
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    path_item = session.query(PathItems).filter_by(user_id=user.id,
                                                   path_id=path_id,
                                                   id=item_id).first()

    if request.GET.delete:
        session.delete(path_item)
        return redirect(f'/path/{path_id}')
    return template('path_item_delete',
                    path_item=path_item,
                    path_id=path_id,
                    item_id=item_id)
Exemple #6
0
def edit_path_item(path_id, item_id):
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    old_item_name = session.query(PathItems).filter_by(user_id=user.id,
                                                       path_id=path_id,
                                                       id=item_id).first()
    if request.GET.edit:
        item_name_edited = request.GET.item_name.strip()
        old_item_name.name = item_name_edited
        return redirect(f'/path/{path_id}')
    else:
        return template('path_item_edit',
                        old_item_name=old_item_name,
                        path_id=path_id,
                        item_id=item_id)
Exemple #7
0
def path_training(path_id):
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    path_items = session.query(PathItems).filter_by(user_id=user.id,
                                                    path_id=path_id).all()
    path_item_list = []
    for item in path_items:
        item = str(item)
        path_item_list.append(item)
    global path_global
    path_global = path_item_list

    return template('path_training',
                    path_all=len(path_global),
                    path_global=path_global,
                    path_time=5000)
Exemple #8
0
def add_path_name():
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    path_name = request.forms.getunicode('path_name')
    path_name_new = PathName(user_id=user.id, name=path_name)
    session.add(path_name_new)
    session.commit()
    return redirect('/path')
Exemple #9
0
def add_path_item(path_id):
    item = request.forms.getunicode('item')
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    # path = session.query(PathItems).filter_by(user_id=user.id, path_id=path_id).first()
    new_item = PathItems(user_id=user.id, path_id=path_id, name=item)
    session.add(new_item)
    session.commit()
    return redirect(f'/path/{path_id}')
Exemple #10
0
def profile():
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    # registered user
    if user:
        results_number = session.query(NumberResults).filter_by(
            user_id=user.id).order_by(NumberResults.attempt_id.desc()).first()
        results_cards = session.query(CardsResults).filter_by(
            user_id=user.id).order_by(CardsResults.attempt_id.desc()).first()
        results_pi = session.query(PiResults).filter_by(
            user_id=user.id).order_by(PiResults.attempt_id.desc()).first()

        return template('profile',
                        user=user_global,
                        results_number=results_number,
                        results_cards=results_cards,
                        results_pi=results_pi)
    else:
        return template('profile',
                        user='******'re not logged in',
                        results_number='no results',
                        results_cards='no results',
                        results_pi='no results')
Exemple #11
0
def do_login():
    nickname = request.forms.get('nickname')
    password_to_check = request.forms.getunicode(
        'password')  # str - user's input password

    global user_global
    user_global = nickname

    user = session.query(User).filter_by(nickname=nickname).first()
    if user:
        password_from_storage = user.password  #  bytes in sql

        salt = password_from_storage[:32]
        key = password_from_storage[32:]

        new_key = hashlib.pbkdf2_hmac('sha256',
                                      password_to_check.encode('utf-8'), salt,
                                      100000)

        if new_key == key:
            # return '<h1>You are logged in! </h1>'
            return redirect('/profile')
    return '<h1>Invalid user or password!</h1>'
Exemple #12
0
def results_pi():
    url = '/pi'
    global pi_length
    global pi_field_length
    if pi_length % pi_field_length == 0:
        count = pi_length // pi_field_length
    else:
        count = pi_length // pi_field_length + 1
    input_pi = ''
    for i in range(1, count + 1):
        i = str(i)
        pi_answer = request.forms.get('pi_answer' +
                                      i)  # сколько цифр после запятой
        input_pi += pi_answer
    print(input_pi)

    # Define Pi global
    global pi

    pi = pi_reader(pi_length)
    # pi = pi[2:] # pi without '3.'

    input_pi = list(input_pi)
    pi = list(pi)
    result_lists = itertools.zip_longest(input_pi, pi)
    results_status = ''

    # find a User in SQL
    # global user_global
    try:
        user = session.query(User).filter_by(nickname=user_global).first()
    except:
        user = ''
    # registered user
    # if user:
    # find User's Results Table
    # find user's table of results (last result)
    # results_table = session.query(PiResults).filter_by(user_id=user.id).order_by(
    #     PiResults.attempt_id.desc()).first()

    # Comparing two lists (computer list and user list)

    # RESULTS
    if input_pi == pi:
        results_status = "WIN"

        # if it is a first time, create a table, else - find win_amount and add 1
        pi_signs_amount = len(pi)
        if user:
            results = PiResults(user_id=user.id,
                                pi_signs_amount=pi_signs_amount)
            session.add(results)
            session.commit()
            results = results.pi_signs_amount

    else:
        results_status = "LOOSE"
        pi_signs_amount = 'Mistakes'
    return template(
        'results',
        zipped_list=result_lists,
        results_status=results_status,
        win_results=f'Вы знаете {pi_signs_amount} знаков Пи после запятой',
        loose_results='Not counted',
        url=url)
Exemple #13
0
def cards_input():
    url = '/cards_memory'
    user_input_list = []
    global cards_shuffled_global
    for i in range(1, len(cards_shuffled_global) + 1):
        i = str(i)
        user_answer_cards = request.forms.getunicode('card' + i)
        user_answer_cards = str(user_answer_cards)
        user_input_list.append(user_answer_cards)

    # status Win or Loose
    results_status = ''
    # make the zip lists of results for template
    result_lists = itertools.zip_longest(user_input_list,
                                         cards_shuffled_global)

    # find a User in SQL
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    # registered user
    if user:
        # find User's Results Table
        # find user's table of results (last result)
        results_table = session.query(CardsResults).filter_by(
            user_id=user.id).order_by(CardsResults.attempt_id.desc()).first()

        # Comparing two lists (computer list and user list)

        # TRUE
        # add +1 to the win_amount in SQL
        if user_input_list == cards_shuffled_global:
            results_status = 'WIN'
            # if it is a first time, create a table, else - find win_amount and add 1
            win_amount_new = 1 if (results_table
                                   == None) else results_table.win_amount + 1
            loose_results = 0 if (results_table
                                  == None) else results_table.loose_amount

            results = CardsResults(user_id=user.id,
                                   win_amount=win_amount_new,
                                   loose_amount=loose_results)
            session.add(results)
            session.commit()
            win_results = results.win_amount

        # FALSE
        # add +1 to the loose_amount in SQL
        else:
            results_status = 'LOOSE'
            # if it is a first time, create a table, else - find loose_amount and add 1
            loose_amount_new = 1 if (
                results_table == None) else results_table.loose_amount + 1
            win_results = 0 if (results_table
                                == None) else results_table.win_amount

            results = CardsResults(user_id=user.id,
                                   win_amount=win_results,
                                   loose_amount=loose_amount_new)
            session.add(results)
            session.commit()
            loose_results = results.loose_amount
        return template('results',
                        zipped_list=result_lists,
                        results_status=results_status,
                        win_results=win_results,
                        loose_results=loose_results,
                        url=url)
    # not registered user

    return template('results',
                    zipped_list=result_lists,
                    results_status='You\'re not logged in',
                    win_results='You\'re not logged in',
                    loose_results='You\'re not logged in',
                    url=url)
Exemple #14
0
def number_results():
    url = '/number_memory'  # training one more time
    # get the list of user's answers
    # global input_numbers_global
    user_input_list = []
    for i in range(len(random_numbers_global)):  # amount of numbers
        i += 1  # counter (fields starting count from 1)
        i = str(i)  #  counter to string
        # the same counter is in a template, so we can define user's answer
        user_input = request.forms.get(
            'number' + i,
            type=int)  # get the user's input ('i' is a number of field)
        user_input_list.append(
            user_input)  # add user's inputs in a global list

    # status Win or Loose
    results_status = ''
    # make the zip lists of results for template
    result_lists = itertools.zip_longest(user_input_list,
                                         random_numbers_global)

    # find a User in SQL
    global user_global
    user = session.query(User).filter_by(nickname=user_global).first()
    # registered user
    if user:
        # find User's Results Table
        # find user's table of results (last result)
        results_table = session.query(NumberResults).filter_by(
            user_id=user.id).order_by(NumberResults.attempt_id.desc()).first()

        # Comparing two lists (computer list and user list)

        # TRUE
        # add +1 to the win_amount in SQL
        if user_input_list == random_numbers_global:
            results_status = 'WIN'
            # if it is a first time, create a table, else - find win_amount and add 1
            win_amount_new = 1 if (results_table
                                   == None) else results_table.win_amount + 1
            loose_results = 0 if (results_table
                                  == None) else results_table.loose_amount

            results = NumberResults(user_id=user.id,
                                    win_amount=win_amount_new,
                                    loose_amount=loose_results)
            session.add(results)
            session.commit()
            win_results = results.win_amount

        # FALSE
        # add +1 to the loose_amount in SQL
        else:
            results_status = 'LOOSE'
            # if it is a first time, create a table, else - find loose_amount and add 1
            loose_amount_new = 1 if (
                results_table == None) else results_table.loose_amount + 1
            win_results = 0 if (results_table
                                == None) else results_table.win_amount

            results = NumberResults(user_id=user.id,
                                    win_amount=win_results,
                                    loose_amount=loose_amount_new)
            session.add(results)
            session.commit()
            loose_results = results.loose_amount
        return template('results',
                        zipped_list=result_lists,
                        results_status=results_status,
                        win_results=win_results,
                        loose_results=loose_results,
                        url=url)
    # not registered user

    return template('results',
                    zipped_list=result_lists,
                    results_status='You\'re not logged in',
                    win_results='You\'re not logged in',
                    loose_results='You\'re not logged in',
                    url=url)