Esempio n. 1
0
def register():
    global contest
    if not app.contest.register or is_logged_in():
        abort(404)

    team_name = ''
    error = []

    if request.method == 'POST':
        # TODO: make sure 'team' and 'password' are in request.form
        team_name = request.form['team']
        password = request.form['password']
        confirm_password = request.form['confirm_password']

        rx = '^[-_A-Za-z0-9 ]{3,20}$'
        if not re.match(rx, team_name):
            error.append('Team name is illegal (it must match %s).' % rx)

        if team_name in app.contest.teams:
            error.append('Team name is already taken.')

        rx = '^[-_A-Za-z0-9 ]{5,20}$'
        if not re.match(rx, password):
            error.append('Password is illegal (it must match %s).' % rx)

        if password != confirm_password:
            error.append('Password confirmation was incorrect.')

        if not error:
            _register_team(team_name, password)
            return redirect(url_for('default.login', register='True'))

    return render_template('register.html', team_name=team_name, error=error)
Esempio n. 2
0
def logout():
    # remove the username from the session if it's there
    if is_logged_in():
        session.pop('Username', None)
        session.pop('Full name', None)
        return json_response(jsonify({'Success': 'Logged out'}), 400)
    else:
        error = jsonify({'Error': 'you are not logged in'})
        return json_response(error, 400)
Esempio n. 3
0
def delete_re_property():
    if is_logged_in():
        properties_col.delete_one({'Landlord': session['Full name']})
        return json_response(data=jsonify({'success': 'property deleted'}),
                             status=200)

    else:
        return json_response(data=jsonify({'error': 'Please log in first!'}),
                             status=400)
Esempio n. 4
0
def create_re_property():
    if request.content_type != JSON_MIME_TYPE:
        error = jsonify({'Error': 'Invalid Content-Type'})
        return json_response(error, 400)

    if is_logged_in():
        data = request.json
        if not all([data.get('Name'), data.get('Landlord')]):
            error = jsonify({'Error': 'Name/landlord missing'})
            return json_response(error, 400)

        properties_col.insert(data)
        return json_response(data='property added', status=200)
    else:
        return json_response(data=jsonify({'error': 'Please log in first!'}),
                             status=400)
Esempio n. 5
0
def edit_re_property():
    if request.content_type != JSON_MIME_TYPE:
        error = jsonify({'Error': 'Invalid Content-Type'})
        return json_response(error, 400)

    if is_logged_in():
        print(session['Full name'])
        data = dict(request.json)
        new_values = {'$set': data}
        properties_col.update_one({'Landlord': session['Full name']},
                                  new_values)
        return json_response(data=jsonify({'success': 'property updated'}),
                             status=200)

    else:
        return json_response(data=jsonify({'error': 'Please log in first!'}),
                             status=400)
Esempio n. 6
0
def login():
    goto = request.args.get('next', url_for('index'))
    registered = request.args.get('register', 'False') == 'True'
    team_name = ''
    bad_login = False
    if is_logged_in():
        return redirect(goto)
    if request.method == 'POST':
        # TODO: make sure 'team' and 'password' are in request.form
        team_name = request.form['team']
        password = request.form['password']
        if team_name in app.contest.teams and app.contest.teams[team_name].password == password:
            session['team'] = team_name
            return redirect(goto)
        else:
            session.pop('team', '')
            bad_login = True
    return render_template('login.html', team_name=team_name, bad_login=bad_login, registered=registered)
Esempio n. 7
0
def edit_user():
    if request.content_type != JSON_MIME_TYPE:
        error = jsonify({'Error': 'Invalid Content-Type'})
        return json_response(error, 400)

    data = dict(request.json)

    if is_logged_in():
        # the user can only change his data and needs to be logged in
        if 'Password' in data:
            # hash the new password
            data['Password'] = bcrypt.generate_password_hash(
                data['Password']).decode('utf-8')
        new_values = {'$set': data}
        users_col.update_one({'_id': ObjectId(session['user_id'])}, new_values)
        return json_response(data=jsonify({'Success': 'User edited'}),
                             status=200)
    else:
        return json_response(data=jsonify({'Error': 'you are not logged in'}),
                             status=400)
Esempio n. 8
0
def view_scoreboard(opts):
    phase = app.contest.get_current_phase()
    if not phase.scoreboard_problems:
        abort(404)
    opts = {s.split('=', 1)[0]: (s.split('=', 1)[1] if '=' in s else True) for s in opts.split(',')}
    opts['groups'] = set(opts.get('groups', '+'.join(app.contest.groups.keys())).split('+'))

    cur_time = datetime.datetime.now()
    subs = Submission.query.filter(Submission.submitted <= cur_time).filter(Submission.problem.in_(phase.scoreboard_problems)).order_by(Submission.submitted).all()
    sb = {team: {problem: ScoreboardTeamProblem() for problem in phase.scoreboard_problems} for team, v in app.contest.teams.items() if len(v.groups & opts['groups']) > 0}

    for sub in subs:
        if sub.problem not in phase.scoreboard_problems:
            continue

        if sub.team not in sb:
            continue

        cur = sb[sub.team][sub.problem]

        if (phase.frozen is not None and \
          sub.submitted >= app.contest.second_format(60.0 * phase.frozen) and \
          (not is_logged_in() or get_team().name != sub.team)):
            cur.submit_new()
        else:
            cur.submit((sub.submitted - app.contest.start).total_seconds(), sub.score, sub.points)

    ssb = sorted((-sum(sb[team][problem].points() for problem in phase.scoreboard_problems),
                  sum(sb[team][problem].time_penalty() for problem in phase.scoreboard_problems),
                  team) for team in sb.keys())

    sb = [(s[2], -s[0], s[1], sb[s[2]]) for s in ssb]

    if opts.get('full', False):
        return render_template('scoreboard_full.html', scoreboard=sb)
    else:
        return render_template('scoreboard.html', scoreboard=sb)
Esempio n. 9
0
def view_problem(problem_id, sub_id):

    problem = app.contest.problems.get(problem_id)
    phase = app.contest.get_current_phase()

    team = get_team()
    sub = None
    if not problem:
        abort(404)
    if request.method == 'POST':

        if problem_id not in phase.submit_problems:
            abort(404)

        if not is_logged_in():
            return redirect(url_for('default.login', next=request.path))
        if ('answer_file' in request.files or 'answer' in request.form):

            if 'answer_file' in request.files:
                answer = request.files['answer_file'].read()
                try:
                    answer = answer.decode('utf-8')
                except UnicodeDecodeError:
                    answer = text.bytes2unicode(answer)

            if 'answer_file' not in request.files or not answer:
                answer = request.form['answer']

            score, response = problem.score(answer)

            sub = Submission(
                team=team.name,
                problem=problem_id,
                answer=answer,
                submitted=datetime.datetime.now(),
                score=score,
                points=int(round((score/100) * problem.points)),
                judge_response=response
            )


            app.db.session.add(sub)
            app.db.session.flush()
            app.db.session.commit()
        else:
            abort(400)

        # return redirect(url_for('default.list_submissions'))

    if problem_id not in phase.visible_problems:
        abort(404)

    
    if sub_id is not None:
        team = get_team()
        sub = Submission.query.filter_by(id=sub_id).first()
        if not sub or sub.team != team.name:
            abort(404)
    if team:
        max_sub = Submission.query.filter_by(problem=problem_id, team=team.name).order_by(Submission.points.desc(), Submission.submitted.desc()).first()
    else:
        max_sub = None

    if max_sub is not None and sub is not None and max_sub.id == sub.id:
        sub = None
    return render_template('problem.html', problem=problem, sub=sub, max_sub=max_sub)