Ejemplo n.º 1
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')
Ejemplo n.º 2
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}')
Ejemplo n.º 3
0
def do_signup():
    nickname = request.forms.get('nickname')
    email = request.forms.get('email')
    password = request.forms.getunicode('password')
    salt = os.urandom(32)
    key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)

    password = salt + key

    new_user = User(nickname=nickname, email=email, password=password)
    session.add(new_user)
    try:
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()

    return redirect('/')
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)