コード例 #1
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def verify_warrior():
    # Attempt to get the form data
    inbound_source = request.form.get('source', False)
    if inbound_source is False:
        return error_page("Form data was malformed.", 400)

    # Attempt to verify the submitted program
    (success, output,
     program_data) = verify_warriors.validate(filter_source(inbound_source))

    if not success:
        return error_page(
            "Submission failed to validate. <br> <pre>{}</pre>".format(output))

    # Reaching this point means that we have a valid program. Now to check
    #   if a program with the same name or author already exists.
    if database.get_warrior_by_name(program_data.name):
        return error_page("A warrior named {} already exists.".format(
            program_data.name))
    if database.get_warrior_by_author(program_data.author):
        return error_page(("A warrior by {} already exists. You may " +
                           "submit only one warrior to each competition. " +
                           "If you submitted a warrior in error, please " +
                           "contact the event staff to have it removed from " +
                           "the system.").format(program_data.author))
    # We can place it in the session and move on to commission, if the user
    #   so desires.
    session['warrior'] = wdr.warrior_to_dict(program_data)

    return render_template('verify_warrior.html',
                           message=output,
                           name=program_data.name,
                           author=program_data.author,
                           success=success)
コード例 #2
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def verify_warrior():
    # Attempt to get the form data
    inbound_source = request.form.get('source', False)
    if inbound_source is False:
        return error_page("Form data was malformed.", 400)

    # Attempt to verify the submitted program
    (success, output, program_data) = verify_warriors.validate(
                                        filter_source(inbound_source))

    if not success:
        return error_page("Submission failed to validate. <br> <pre>{}</pre>"
                          .format(output))

    # Reaching this point means that we have a valid program. Now to check
    #   if a program with the same name or author already exists.
    if database.get_warrior_by_name(program_data.name):
        return error_page("A warrior named {} already exists."
                          .format(program_data.name))
    if database.get_warrior_by_author(program_data.author):
        return error_page(("A warrior by {} already exists. You may " +
                           "submit only one warrior to each competition. " +
                           "If you submitted a warrior in error, please " +
                           "contact the event staff to have it removed from " +
                           "the system.").format(program_data.author))
    # We can place it in the session and move on to commission, if the user
    #   so desires.
    session['warrior'] = wdr.warrior_to_dict(program_data)

    return render_template('verify_warrior.html',
                           message=output,
                           name=program_data.name,
                           author=program_data.author,
                           success=success)
コード例 #3
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def acquire_warrior():
    # Try to get the form data
    name = request.form.get('name', False)
    author = request.form.get('author', False)
    if name is False or author is False:
        return error_page("The request is malformed.", 403)
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        warrior = database.get_warrior_by_name(name)
        return render_template("retrieve.html", warrior=warrior)
    else:
        return error_page("You are not authorized to access the source code " +
                          "of warriors.", 403)
コード例 #4
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def delete_warrior():
    # Try to get the form data
    name = request.form.get('name', False)
    author = request.form.get('author', False)
    if name is False or author is False:
        return error_page("The request is malformed.", 403)
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        database.delete_warrior_by_name(name)
        flash("Successfully deleted {} by {}.".format(name, author))
        return redirect(url_for('list_warriors'))
    else:
        return error_page("You are not authorized to delete warriors.", 403)
コード例 #5
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def delete_warrior():
    # Try to get the form data
    name = request.form.get('name', False)
    author = request.form.get('author', False)
    if name is False or author is False:
        return error_page("The request is malformed.", 403)
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        database.delete_warrior_by_name(name)
        flash("Successfully deleted {} by {}.".format(name, author))
        return redirect(url_for('list_warriors'))
    else:
        return error_page("You are not authorized to delete warriors.", 403)
コード例 #6
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def acquire_warrior():
    # Try to get the form data
    name = request.form.get('name', False)
    author = request.form.get('author', False)
    if name is False or author is False:
        return error_page("The request is malformed.", 403)
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        warrior = database.get_warrior_by_name(name)
        return render_template("retrieve.html", warrior=warrior)
    else:
        return error_page(
            "You are not authorized to access the source code " +
            "of warriors.", 403)
コード例 #7
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def add_warrior():
    # If the database is locked, people can't submit new warriors.
    if database.is_db_locked():
        return error_page("The database is locked; submissions are currently \
                          not allowed.")
    # Otherwise, render the form. Not much else to do.
    return render_template('new_warrior.html')
コード例 #8
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def add_warrior():
    # If the database is locked, people can't submit new warriors.
    if database.is_db_locked():
        return error_page("The database is locked; submissions are currently \
                          not allowed.")
    # Otherwise, render the form. Not much else to do.
    return render_template('new_warrior.html')
コード例 #9
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def pair_warriors():
    from toroid.pairs import make_pairings
    # Check if we're authenticated
    if authentication.is_authenticated():
        # Try to pair all the warriors in the db
        (raw_pairs, raw_odd_one_out) = make_pairings(
                                    database.list_warriors_raw())
        # See if we got back pairings
        if raw_pairs is False:
            return error_page("Pairing failed. Are there warriors in the db?",
                              500)
        # Place results in the session
        session['raw_pairs'] = raw_pairs
        session['raw_odd_one_out'] = raw_odd_one_out
        return redirect(url_for('show_pairs'))
    else:
        return error_page("You are not authorized to generate pairs.", 403)
コード例 #10
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def pair_warriors():
    from toroid.pairs import make_pairings
    # Check if we're authenticated
    if authentication.is_authenticated():
        # Try to pair all the warriors in the db
        (raw_pairs,
         raw_odd_one_out) = make_pairings(database.list_warriors_raw())
        # See if we got back pairings
        if raw_pairs is False:
            return error_page("Pairing failed. Are there warriors in the db?",
                              500)
        # Place results in the session
        session['raw_pairs'] = raw_pairs
        session['raw_odd_one_out'] = raw_odd_one_out
        return redirect(url_for('show_pairs'))
    else:
        return error_page("You are not authorized to generate pairs.", 403)
コード例 #11
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def purge_db():
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        database.purge_warrior_db()
        flash("Purged the database.")
        return redirect(url_for("admin"))
    else:
        return error_page("It's not nice to try and purge the database " +
                          "without authentication...", 403)
コード例 #12
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def purge_db():
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        database.purge_warrior_db()
        flash("Purged the database.")
        return redirect(url_for("admin"))
    else:
        return error_page(
            "It's not nice to try and purge the database " +
            "without authentication...", 403)
コード例 #13
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def admin():
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        return render_template('admin.html',
                               n_warriors=len(database.list_warriors_raw()),
                               n_permutations=num_permutations(),
                               locked=database.is_db_locked())
    else:
        return error_page("You are not authorized to access the admin panel.",
                          403)
コード例 #14
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def admin():
    # Make sure the user is authenticated. If not, kick them out.
    if authentication.is_authenticated():
        return render_template('admin.html',
                               n_warriors=len(database.list_warriors_raw()),
                               n_permutations=num_permutations(),
                               locked=database.is_db_locked())
    else:
        return error_page("You are not authorized to access the admin panel.",
                          403)
コード例 #15
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def commit_warrior():
    # Attempt to get the warrior out of the session
    warrior_dict = session.get('warrior', False)
    # All warriors start with a score of 0
    warrior_dict["score"] = 0
    if warrior_dict is False:
        return error_page('Request was malformed.', 400)

    warrior = wdr.dict_to_warrior(warrior_dict)
    ident = database.add_warrior(warrior)
    flash(('Thank you, {}. Your warrior {} was successfully' +
           ' added to the database as ID #{}.')
          .format(warrior.author, warrior.name, ident))
    return(redirect(url_for('list_warriors')))
コード例 #16
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def show_pairs():
    if authentication.is_authenticated():
        # Try and get data from the session
        raw_pairs = session.get('raw_pairs', False)
        raw_odd_one_out = session.get('raw_odd_one_out', None)
        if raw_pairs is False or raw_odd_one_out is None:
            return error_page("Your pair data is invalid. Regenerate.", 400)

        # We have data, convert it.
        pairs = wdr.dictpairlist_to_warriorpairlist(raw_pairs)
        if raw_odd_one_out is not False:
            odd_one_out = wdr.dict_to_warrior(raw_odd_one_out)
        else:
            odd_one_out = False

        # OK! Render.
        return render_template('pairs.html',
                               pairs=pairs,
                               odd_one_out=odd_one_out)

    else:
        return error_page("You're not allowed to make pairs. You can't " +
                          "have any to view... why are you here?", 500)
コード例 #17
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def commit_warrior():
    # Attempt to get the warrior out of the session
    warrior_dict = session.get('warrior', False)
    # All warriors start with a score of 0
    warrior_dict["score"] = 0
    if warrior_dict is False:
        return error_page('Request was malformed.', 400)

    warrior = wdr.dict_to_warrior(warrior_dict)
    ident = database.add_warrior(warrior)
    flash(('Thank you, {}. Your warrior {} was successfully' +
           ' added to the database as ID #{}.').format(warrior.author,
                                                       warrior.name, ident))
    return (redirect(url_for('list_warriors')))
コード例 #18
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def show_pairs():
    if authentication.is_authenticated():
        # Try and get data from the session
        raw_pairs = session.get('raw_pairs', False)
        raw_odd_one_out = session.get('raw_odd_one_out', None)
        if raw_pairs is False or raw_odd_one_out is None:
            return error_page("Your pair data is invalid. Regenerate.", 400)

        # We have data, convert it.
        pairs = wdr.dictpairlist_to_warriorpairlist(raw_pairs)
        if raw_odd_one_out is not False:
            odd_one_out = wdr.dict_to_warrior(raw_odd_one_out)
        else:
            odd_one_out = False

        # OK! Render.
        return render_template('pairs.html',
                               pairs=pairs,
                               odd_one_out=odd_one_out)

    else:
        return error_page(
            "You're not allowed to make pairs. You can't " +
            "have any to view... why are you here?", 500)
コード例 #19
0
ファイル: views.py プロジェクト: SilverWingedSeraph/corewar32
def login():
    # If we don't have form data, simply show the form or the admin page.
    if request.method == 'GET':
        if authentication.is_authenticated():
            return redirect(url_for('admin'))
        else:
            return render_template('login.html')
    # If we do have form data, validate the password and take the appropriate
    #   action
    if request.method == 'POST':
        password = request.form.get('password', '')
        if authentication.check_password(password):
            authentication.login()
            return redirect(url_for('login'))
        else:
            return error_page('The password was incorrect.', 403)
コード例 #20
0
ファイル: views.py プロジェクト: NoraCodes/corewar32
def login():
    # If we don't have form data, simply show the form or the admin page.
    if request.method == 'GET':
        if authentication.is_authenticated():
            return redirect(url_for('admin'))
        else:
            return render_template('login.html')
    # If we do have form data, validate the password and take the appropriate
    #   action
    if request.method == 'POST':
        password = request.form.get('password', '')
        if authentication.check_password(password):
            authentication.login()
            return redirect(url_for('login'))
        else:
            return error_page('The password was incorrect.', 403)