def test_check_event(session_scope_function): config = read_config(ramp_config_template()) # addition of event require some problem problem_names = ['iris', 'boston_housing'] for problem_name in problem_names: setup_ramp_kits_ramp_data(config, problem_name) ramp_config = generate_ramp_config(config) add_problem(session_scope_function, problem_name, ramp_config['ramp_kits_dir'], ramp_config['ramp_data_dir']) for problem_name in problem_names: event_name = '{}_test'.format(problem_name) event_title = 'event title' add_event(session_scope_function, problem_name, event_name, event_title, ramp_config['sandbox_name'], ramp_config['ramp_submissions_dir'], is_public=True, force=False) event = get_event(session_scope_function, None) assert len(event) == 2 assert isinstance(event, list) event = get_event(session_scope_function, 'iris_test') scores_iris = ('acc', 'error', 'nll', 'f1_70') _check_event(session_scope_function, event, 'iris_test', 'event title', True, scores_iris) # add event for second time without forcing should raise an error err_msg = 'Attempting to overwrite existing event.' with pytest.raises(ValueError, match=err_msg): add_event(session_scope_function, 'iris', 'iris_test', event_title, ramp_config['sandbox_name'], ramp_config['ramp_submissions_dir'], is_public=True, force=False) # add event by force add_event(session_scope_function, 'iris', 'iris_test', event_title, ramp_config['sandbox_name'], ramp_config['ramp_submissions_dir'], is_public=True, force=True) event = get_event(session_scope_function, 'iris_test') _check_event(session_scope_function, event, 'iris_test', 'event title', True, scores_iris) delete_event(session_scope_function, 'iris_test') event = get_event(session_scope_function, None) assert len(event) == 1
def test_event_model_backref(session_scope_module, backref, expected_type): event = get_event(session_scope_module, 'iris_test') backref_attr = getattr(event, backref) assert isinstance(backref_attr, list) # only check if the list is not empty if backref_attr: assert isinstance(backref_attr[0], expected_type)
def test_even_model_timestamp(session_scope_module, opening, public_opening, closure, properties, expected_values): # check the property linked to the opening/closure of the event. event = get_event(session_scope_module, 'iris_test') # store the original timestamp before to force them init_opening = event.opening_timestamp init_public_opening = event.public_opening_timestamp init_closure = event.closing_timestamp # set to non-default values the date if necessary event.opening_timestamp = opening if opening is not None else init_opening event.public_opening_timestamp = (public_opening if public_opening is not None else init_public_opening) event.closing_timestamp = closure if closure is not None else init_closure for prop, exp_val in zip(properties, expected_values): assert getattr(event, prop) is exp_val # reset the event since that we are sharing the dataset across all the # module tests. event.opening_timestamp = init_opening event.public_opening_timestamp = init_public_opening event.closing_timestamp = init_closure
def test_event_model_score(session_scope_module): # Make Model usable in declarative mode set_query_property(Model, session_scope_module) event = get_event(session_scope_module, 'iris_test') assert repr(event) == 'Event(iris_test)' assert issubclass(event.Predictions, BasePrediction) assert isinstance(event.workflow, Workflow) assert event.workflow.name == 'Estimator' event_type_score = event.official_score_type assert event_type_score.name == 'acc' event_type_score = event.get_official_score_type(session_scope_module) assert event_type_score.name == 'acc' assert event.combined_combined_valid_score_str is None assert event.combined_combined_test_score_str is None assert event.combined_foldwise_valid_score_str is None assert event.combined_foldwise_test_score_str is None event.combined_combined_valid_score = 0.1 event.combined_combined_test_score = 0.2 event.combined_foldwise_valid_score = 0.3 event.combined_foldwise_test_score = 0.4 assert event.combined_combined_valid_score_str == '0.1' assert event.combined_combined_test_score_str == '0.2' assert event.combined_foldwise_valid_score_str == '0.3' assert event.combined_foldwise_test_score_str == '0.4'
def test_event_status(client_session, makedrop_event, opening_date, public_date, closing_date, expected): # checks if the event status is displayed correctly client, session = client_session # change the datetime stamps for the event event = get_event(session, 'iris_test_4event') event.opening_timestamp = opening_date event.public_opening_timestamp = public_date event.closing_timestamp = closing_date session.commit() # GET: access the problems page without login rv = client.get('/problems') assert rv.status_code == 200 event_idx = rv.data.index(b'iris_test_4event') event_class_idx = rv.data[:event_idx].rfind(b'<i class') assert expected in rv.data[event_class_idx:event_idx] # GET: access the problems when logged-in with login_scope(client, 'test_user', 'test') as client: rv = client.get('/problems') assert rv.status_code == 200 event_idx = rv.data.index(b'iris_test_4event') event_class_idx = rv.data[:event_idx].rfind(b'<i class') assert expected in rv.data[event_class_idx:event_idx]
def competition_leaderboard(event_name): """Landing page for the competition leaderboard for all users. Parameters ---------- event_name : str The event name. """ event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user(u'{}: no event named "{}"'.format( flask_login.current_user.firstname, event_name)) add_user_interaction(db.session, interaction='looking at leaderboard', user=flask_login.current_user, event=event) admin = is_admin(db.session, event_name, flask_login.current_user.name) leaderboard_html = event.public_competition_leaderboard_html leaderboard_kwargs = dict(leaderboard=leaderboard_html, leaderboard_title='Leaderboard', sorting_column_index=0, sorting_direction='asc', event=event, admin=admin) return render_template('leaderboard.html', **leaderboard_kwargs)
def approve_sign_up_for_event(event_name, user_name): """Approve a user for a specific event. This way of approval is usually used by clicking in an email sent to the admin. Parameters ---------- event_name : str The name of the event. user_name : str The name of the user. """ event = get_event(db.session, event_name) user = User.query.filter_by(name=user_name).one_or_none() if not is_admin(db.session, event_name, flask_login.current_user.name): return redirect_to_user( u'Sorry {}, you do not have admin rights'.format( flask_login.current_user.firstname), is_error=True) if not event or not user: return redirect_to_user(u'No event {} or no user {}'.format( event_name, user_name), is_error=True) sign_up_team(db.session, event.name, user.name) subject = ('Signed up for the RAMP event {}'.format(event.name)) body = ('{}, you have been registered to the RAMP event {}. ' 'You can now proceed to your sandbox and make submission.' '\nHave fun!!!'.format(flask_login.current_user.name, event.name)) send_mail(to=flask_login.current_user.email, subject=subject, body=body) return redirect_to_user(u'{} is signed up for {}.'.format(user, event), is_error=False, category='Successful sign-up')
def event_plots(event_name): """Landing page of the plot illustrating the score evolution over time for a specific RAMP event. Parameters ---------- event_name : str The name of the event. """ event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user('{}: no event named "{}"' .format(flask_login.current_user.firstname, event_name)) if event: p = score_plot(db.session, event) script, div = components(p) return render_template('event_plots.html', script=script, div=div, event=event) return redirect_to_user('Event {} does not exist.' .format(event_name), is_error=True)
def user_event(event_name): """Landing page for a given event. Parameters ---------- event_name : str The event name. """ if flask_login.current_user.access_level == 'asked': msg = 'Your account has not been approved yet by the administrator' logger.error(msg) return redirect_to_user(msg) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user('{}: no event named "{}"'.format( flask_login.current_user.firstname, event_name)) event = get_event(db.session, event_name) if event: if app.config['TRACK_USER_INTERACTION']: add_user_interaction(db.session, interaction='looking at event', event=event, user=flask_login.current_user) admin = is_admin(db.session, event_name, flask_login.current_user.name) approved = is_user_signed_up(db.session, event_name, flask_login.current_user.name) asked = is_user_sign_up_requested(db.session, event_name, flask_login.current_user.name) return render_template('event.html', event=event, admin=admin, approved=approved, asked=asked) return redirect_to_user('Event {} does not exist.'.format(event_name), is_error=True)
def test_event_team_model(session_scope_module): event = get_event(session_scope_module, 'iris_test') team = get_team_by_name(session_scope_module, 'test_user') event_team = (session_scope_module.query(EventTeam).filter( EventTeam.event_id == event.id).filter( EventTeam.team_id == team.id).one()) assert repr(event_team) == "Event(iris_test)/Team({})".format('test_user')
def dashboard_submissions(event_name): """Show information about all submissions for a given event. Parameters ---------- event_name : str The name of the event. """ if not is_admin(db.session, event_name, flask_login.current_user.name): return redirect_to_user( 'Sorry {}, you do not have admin rights' .format(flask_login.current_user.firstname), is_error=True ) event = get_event(db.session, event_name) # Get dates and number of submissions submissions = \ (Submission.query .filter(Event.name == event.name) .filter(Event.id == EventTeam.event_id) .filter(EventTeam.id == Submission.event_team_id) .order_by(Submission.submission_timestamp) .all()) submissions = [sub for sub in submissions if sub.is_not_sandbox] timestamp_submissions = [ sub.submission_timestamp.strftime('%Y-%m-%d %H:%M:%S') for sub in submissions] name_submissions = [sub.name for sub in submissions] cumulated_submissions = list(range(1, 1 + len(submissions))) training_sec = [ ( sub.training_timestamp - sub.submission_timestamp ).total_seconds() / 60. if sub.training_timestamp is not None else 0 for sub in submissions ] dashboard_kwargs = {'event': event, 'timestamp_submissions': timestamp_submissions, 'training_sec': training_sec, 'cumulated_submissions': cumulated_submissions, 'name_submissions': name_submissions} failed_leaderboard_html = event.failed_leaderboard_html new_leaderboard_html = event.new_leaderboard_html approved = is_user_signed_up( db.session, event_name, flask_login.current_user.name ) asked = is_user_sign_up_requested( db.session, event_name, flask_login.current_user.name ) return render_template( 'dashboard_submissions.html', failed_leaderboard=failed_leaderboard_html, new_leaderboard=new_leaderboard_html, admin=True, approved=approved, asked=asked, **dashboard_kwargs)
def test_cv_fold_model_backref(session_scope_module, backref, expected_type): event = get_event(session_scope_module, 'iris_test') cv_fold = (session_scope_module.query(CVFold).filter( CVFold.event_id == event.id).first()) backref_attr = getattr(cv_fold, backref) assert isinstance(backref_attr, list) # only check if the list is not empty if backref_attr: assert isinstance(backref_attr[0], expected_type)
def test_event_model_property(session_scope_module): event = get_event(session_scope_module, 'iris_test') assert repr(event) == 'Event(iris_test)' assert issubclass(event.Predictions, BasePrediction) assert isinstance(event.workflow, Workflow) assert event.workflow.name == 'Estimator' assert event.n_participants == 2 assert event.n_jobs == 2
def leaderboard(event_name): """Landing page showing all user's submissions information. Parameters ---------- event_name : str The name of the event. """ event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user( '{}: no event named "{}"' .format(flask_login.current_user.firstname, event_name)) if app.config['TRACK_USER_INTERACTION']: add_user_interaction( db.session, interaction='looking at leaderboard', user=flask_login.current_user, event=event ) if is_accessible_leaderboard(db.session, event_name, flask_login.current_user.name): leaderboard_html = event.public_leaderboard_html_with_links else: leaderboard_html = event.public_leaderboard_html_no_links if event.official_score_type.is_lower_the_better: sorting_direction = 'asc' else: sorting_direction = 'desc' leaderboard_kwargs = dict( leaderboard=leaderboard_html, leaderboard_title='Leaderboard', sorting_column_index=4, sorting_direction=sorting_direction, event=event ) if is_admin(db.session, event_name, flask_login.current_user.name): failed_leaderboard_html = event.failed_leaderboard_html new_leaderboard_html = event.new_leaderboard_html template = render_template( 'leaderboard.html', failed_leaderboard=failed_leaderboard_html, new_leaderboard=new_leaderboard_html, admin=True, **leaderboard_kwargs ) else: template = render_template( 'leaderboard.html', **leaderboard_kwargs ) return template
def private_leaderboard(event_name): """Landing page for the private leaderboard. Parameters ---------- event_name : str The event name. """ if not flask_login.current_user.is_authenticated: return redirect(url_for('auth.login')) event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user( '{}: no event named "{}"' .format(flask_login.current_user.firstname, event_name) ) if (not is_admin(db.session, event_name, flask_login.current_user.name) and (event.closing_timestamp is None or event.closing_timestamp > datetime.datetime.utcnow())): return redirect(url_for('ramp.problems')) if app.config['TRACK_USER_INTERACTION']: add_user_interaction( db.session, interaction='looking at private leaderboard', user=flask_login.current_user, event=event ) leaderboard_html = event.private_leaderboard_html admin = is_admin(db.session, event_name, flask_login.current_user.name) if event.official_score_type.is_lower_the_better: sorting_direction = 'asc' else: sorting_direction = 'desc' approved = is_user_signed_up( db.session, event_name, flask_login.current_user.name ) asked = approved template = render_template( 'leaderboard.html', leaderboard_title='Leaderboard', leaderboard=leaderboard_html, sorting_column_index=5, sorting_direction=sorting_direction, event=event, private=True, admin=admin, asked=asked, approved=approved ) return template
def test_sandbox_upload_file(client_session, makedrop_event, submission_dir, filename): client, session = client_session sign_up_team(session, "iris_test_4event", "test_user") config = ramp_config_template() ramp_config = generate_ramp_config(read_config(config)) # upload file in sandbox.html path_submissions = os.path.join(ramp_config["ramp_kit_dir"], submission_dir) with login_scope(client, "test_user", "test") as client: rv = client.get("http://localhost/events/iris_test_4event/sandbox") assert rv.status_code == 200 # choose file and check if it was uploaded correctly path_submission = os.path.join(path_submissions, filename) assert os.path.isfile(path_submission) rv = client.post( "http://localhost/events/iris_test_4event/sandbox", headers={ "Referer": "http://localhost/events/iris_test_4event/sandbox" }, data={"file": (open(path_submission, "rb"), filename)}, follow_redirects=False, ) assert rv.status_code == 302 assert ( rv.location == "http://localhost/events/iris_test_4event/sandbox") # code of the saved file with open(path_submission, "r") as file: submitted_data = file.read() # code from the db event = get_event(session, "iris_test_4event") sandbox_submission = get_submission_by_name(session, "iris_test_4event", "test_user", event.ramp_sandbox_name) submission_code = sandbox_submission.files[-1].get_code() # get user interactions from db and check if 'upload' was added user_interactions = get_user_interactions_by_name(session, "test_user") # check if the code of the submitted file in the 'submission_code' assert submitted_data is not None assert submitted_data in submission_code # check if the user_interaction was added to the db assert "upload" in user_interactions["interaction"].values
def test_event_team_model_backref(session_scope_module, backref, expected_type): event = get_event(session_scope_module, 'iris_test') team = get_team_by_name(session_scope_module, 'test_user') event_team = (session_scope_module.query(EventTeam).filter( EventTeam.event_id == event.id).filter( EventTeam.team_id == team.id).one()) backref_attr = getattr(event_team, backref) assert isinstance(backref_attr, list) # only check if the list is not empty if backref_attr: assert isinstance(backref_attr[0], expected_type)
def test_is_accessible_event(session_toy_db, event_name, user_name, is_accessible): # force one of the user to not be approved if user_name == 'test_user': user = get_user_by_name(session_toy_db, user_name) user.access_level = 'asked' session_toy_db.commit() # force an event to be private if event_name == 'boston_housing_test': event = get_event(session_toy_db, event_name) event.is_public = False session_toy_db.commit() assert is_accessible_event(session_toy_db, event_name, user_name) is is_accessible
def test_event_score_type_model_backref(session_scope_module, backref, expected_type): event = get_event(session_scope_module, 'iris_test') # get only the accuracy score event_type_score = \ (session_scope_module.query(EventScoreType) .filter(EventScoreType.event_id == event.id) .filter(EventScoreType.name == 'acc') .one()) backref_attr = getattr(event_type_score, backref) assert isinstance(backref_attr, list) # only check if the list is not empty if backref_attr: assert isinstance(backref_attr[0], expected_type)
def test_event_score_type_model_property(session_scope_module): event = get_event(session_scope_module, 'iris_test') # get only the accuracy score event_type_score = \ (session_scope_module.query(EventScoreType) .filter(EventScoreType.event_id == event.id) .filter(EventScoreType.name == 'acc') .one()) assert repr(event_type_score) == "acc: Event(iris_test)" assert isinstance(event_type_score.score_type_object, Accuracy) assert event_type_score.is_lower_the_better is False assert event_type_score.minimum == pytest.approx(0) assert event_type_score.maximum == pytest.approx(1) assert event_type_score.worst == pytest.approx(0) assert callable(event_type_score.score_type_object.score_function)
def test_sign_up_for_event(client_session): client, session = client_session # trigger that the event does not exist with login_scope(client, 'test_user', 'test') as client: rv = client.get('/events/xxx/sign_up') assert rv.status_code == 302 assert rv.location == 'http://localhost/problems' with client.session_transaction() as cs: flash_message = dict(cs['_flashes']) assert "no event named" in flash_message['message'] # GET: sign-up to a new controlled event add_user(session, 'yy', 'yy', 'yy', 'yy', 'yy', access_level='user') with login_scope(client, 'yy', 'yy') as client: rv = client.get('/events/iris_test/sign_up') assert rv.status_code == 302 assert rv.location == 'http://localhost/problems' with client.session_transaction() as cs: flash_message = dict(cs['_flashes']) assert "Sign-up request is sent" in flash_message['Request sent'] # make sure that the database has been updated for our session session.commit() event_team = get_event_team_by_name(session, 'iris_test', 'yy') assert not event_team.approved # check that we are informing the user that he has to wait for approval rv = client.get('/events/iris_test') assert rv.status_code == 200 assert b'Waiting approval...' in rv.data # GET: sign-up to a new uncontrolled event event = get_event(session, 'boston_housing_test') event.is_controled_signup = False session.commit() with login_scope(client, 'yy', 'yy') as client: rv = client.get('/events/boston_housing_test/sign_up') assert rv.status_code == 302 assert (rv.location == 'http://localhost/events/boston_housing_test/sandbox') with client.session_transaction() as cs: flash_message = dict(cs['_flashes']) assert "is signed up for" in flash_message['Successful sign-up'] # make sure that the database has been updated for our session session.commit() event_team = get_event_team_by_name(session, 'boston_housing_test', 'yy') assert event_team.approved
def my_submissions(event_name): """Landing page of all user's submission information. Parameters ---------- event_name : str The name of the event. """ event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user( '{}: no event named "{}"' .format(flask_login.current_user.firstname, event_name) ) if app.config['TRACK_USER_INTERACTION']: add_user_interaction( db.session, interaction='looking at my_submissions', user=flask_login.current_user, event=event ) if not is_accessible_code(db.session, event_name, flask_login.current_user.name): error_str = ('No access to my submissions for event {}. If you have ' 'already signed up, please wait for approval.' .format(event.name)) return redirect_to_user(error_str) # Doesn't work if team mergers are allowed event_team = get_event_team_by_name(db.session, event_name, flask_login.current_user.name) leaderboard_html = event_team.leaderboard_html failed_leaderboard_html = event_team.failed_leaderboard_html new_leaderboard_html = event_team.new_leaderboard_html admin = is_admin(db.session, event_name, flask_login.current_user.name) if event.official_score_type.is_lower_the_better: sorting_direction = 'asc' else: sorting_direction = 'desc' return render_template('leaderboard.html', leaderboard_title='Trained submissions', leaderboard=leaderboard_html, failed_leaderboard=failed_leaderboard_html, new_leaderboard=new_leaderboard_html, sorting_column_index=4, sorting_direction=sorting_direction, event=event, admin=admin)
def test_unit_test_dispatcher(session_toy): # make sure that the size of the list is bigger than the number of # submissions config = read_config(database_config_template()) event_config = read_config(ramp_config_template()) dispatcher = Dispatcher(config=config, event_config=event_config, worker=CondaEnvWorker, n_workers=100, hunger_policy='exit') # check that all the queue are empty assert dispatcher._awaiting_worker_queue.empty() assert dispatcher._processing_worker_queue.empty() assert dispatcher._processed_submission_queue.empty() # check that all submissions are queued submissions = get_submissions(session_toy, 'iris_test', 'new') dispatcher.fetch_from_db(session_toy) # we should remove the starting kit from the length of the submissions for # each user assert dispatcher._awaiting_worker_queue.qsize() == len(submissions) - 2 submissions = get_submissions(session_toy, 'iris_test', 'sent_to_training') assert len(submissions) == 6 # start the training dispatcher.launch_workers(session_toy) # be sure that the training is finished while not dispatcher._processing_worker_queue.empty(): dispatcher.collect_result(session_toy) assert len(get_submissions(session_toy, 'iris_test', 'new')) == 2 assert (len(get_submissions(session_toy, 'iris_test', 'training_error')) == 2) assert len(get_submissions(session_toy, 'iris_test', 'tested')) == 4 dispatcher.update_database_results(session_toy) assert dispatcher._processed_submission_queue.empty() event = get_event(session_toy, 'iris_test') assert event.private_leaderboard_html assert event.public_leaderboard_html_with_links assert event.public_leaderboard_html_no_links assert event.failed_leaderboard_html assert event.new_leaderboard_html is None assert event.public_competition_leaderboard_html assert event.private_competition_leaderboard_html
def sign_up_for_event(event_name): """Landing page to sign-up to a specific RAMP event. Parameters ---------- event_name : str The name of the event. """ event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user('{}: no event named "{}"' .format(flask_login.current_user.firstname, event_name)) if app.config['TRACK_USER_INTERACTION']: add_user_interaction(db.session, interaction='signing up at event', user=flask_login.current_user, event=event) ask_sign_up_team(db.session, event.name, flask_login.current_user.name) if event.is_controled_signup: admin_users = User.query.filter_by(access_level='admin') for admin in admin_users: subject = ('Request to sign-up {} to RAMP event {}' .format(event.name, flask_login.current_user.name)) body = body_formatter_user(flask_login.current_user) url_approve = ('http://{}/events/{}/sign_up/{}' .format( app.config['DOMAIN_NAME'], event.name, flask_login.current_user.name )) body += ('Click on this link to approve the sign-up request: {}' .format(url_approve)) send_mail(admin, subject, body) return redirect_to_user("Sign-up request is sent to event admins.", is_error=False, category='Request sent') sign_up_team(db.session, event.name, flask_login.current_user.name) return redirect_to_sandbox( event, '{} is signed up for {}.' .format(flask_login.current_user.firstname, event), is_error=False, category='Successful sign-up' )
def test_sandbox_save_file(client_session, makedrop_event): client, session = client_session sign_up_team(session, "iris_test_4event", "test_user") example_code = "example content" with login_scope(client, "test_user", "test") as client: rv = client.get("http://localhost/events/iris_test_4event/sandbox") assert rv.status_code == 200 rv = client.post( "http://localhost/events/iris_test_4event/sandbox", headers={ "Referer": "http://localhost/events/iris_test_4event/sandbox" }, data={ "estimator": example_code, "code-csrf_token": "temp_token" }, follow_redirects=False, ) assert rv.status_code == 200 # code from the db event = get_event(session, "iris_test_4event") sandbox_submission = get_submission_by_name(session, "iris_test_4event", "test_user", event.ramp_sandbox_name) submission_code = sandbox_submission.files[-1].get_code() # get user interactions from db and check if 'save' was added user_interactions = get_user_interactions_by_name(session, "test_user") assert "save" in user_interactions["interaction"].values assert example_code in submission_code # make sure that after changing the code example # and reloading the page the code is still changed with login_scope(client, "test_user", "test") as client: rv = client.get("http://localhost/events/iris_test_4event/sandbox") assert rv.status_code == 200 assert example_code.encode() in rv.data
def private_competition_leaderboard(event_name): """Landing page for the private competition leaderboard. Parameters ---------- event_name : str The event name. """ if not flask_login.current_user.is_authenticated: return redirect(url_for('auth.login')) event = get_event(db.session, event_name) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user(u'{}: no event named "{}"'.format( flask_login.current_user.firstname, event_name)) if (not is_admin(db.session, event_name, flask_login.current_user.name) and (event.closing_timestamp is None or event.closing_timestamp > datetime.datetime.utcnow())): return redirect(url_for('ramp.problems')) if app.config['TRACK_USER_INTERACTION']: add_user_interaction(db.session, interaction='looking at private leaderboard', user=flask_login.current_user, event=event) admin = is_admin(db.session, event_name, flask_login.current_user.name) approved = is_user_signed_up(db.session, event_name, flask_login.current_user.name) asked = approved leaderboard_html = event.private_competition_leaderboard_html leaderboard_kwargs = dict(leaderboard=leaderboard_html, leaderboard_title='Leaderboard', sorting_column_index=0, sorting_direction='asc', event=event, admin=admin, asked=asked, approved=approved) return render_template('leaderboard.html', **leaderboard_kwargs)
def test_is_accessible_leaderboard(session_toy_db): event_name = 'iris_test' # simulate a user which is not authenticated user = get_user_by_name(session_toy_db, 'test_user_2') user.is_authenticated = False assert not is_accessible_leaderboard(session_toy_db, event_name, user.name) # simulate a user which authenticated and author of the submission to a # public event user.is_authenticated = True assert not is_accessible_leaderboard(session_toy_db, event_name, user.name) # simulate an admin user user = get_user_by_name(session_toy_db, 'test_iris_admin') user.is_authenticated = True assert is_accessible_leaderboard(session_toy_db, event_name, 'test_iris_admin') # simulate a close event event = get_event(session_toy_db, event_name) event.closing_timestamp = datetime.datetime.utcnow() assert not is_accessible_leaderboard(session_toy_db, event_name, 'test_user_2')
def test_submit_button_enabled_disabled(client_session, makedrop_event, opening_date, public_date, closing_date, expected): client, session = client_session event = get_event(session, 'iris_test_4event') event.opening_timestamp = opening_date event.public_opening_timestamp = public_date event.closing_timestamp = closing_date session.commit() sign_up_team(session, 'iris_test_4event', 'test_user') with login_scope(client, 'test_user', 'test') as client: rv = client.get('http://localhost/events/iris_test_4event/sandbox') assert rv.status_code == 200 # check for update button status on the generated .html if expected == b'event-close': assert 'disabled' in str(rv.data) # should to be disabled else: assert 'disabled' not in str(rv.data) # should not be disabled
def user_event(event_name): """Landing page for a given event. Parameters ---------- event_name : str The event name. """ if flask_login.current_user.access_level == 'asked': msg = 'Your account has not been approved yet by the administrator' logger.error(msg) return redirect_to_user(msg) if not is_accessible_event(db.session, event_name, flask_login.current_user.name): return redirect_to_user('{}: no event named "{}"' .format(flask_login.current_user.firstname, event_name)) event = get_event(db.session, event_name) if event: if app.config['TRACK_USER_INTERACTION']: add_user_interaction(db.session, interaction='looking at event', event=event, user=flask_login.current_user) description_f_name = os.path.join( event.problem.path_ramp_kit, '{}_starting_kit.html'.format(event.problem.name) ) with codecs.open(description_f_name, 'r', 'utf-8') as description_file: description = description_file.read() admin = is_admin(db.session, event_name, flask_login.current_user.name) approved = is_user_signed_up( db.session, event_name, flask_login.current_user.name ) asked = approved return render_template('event.html', description=description, event=event, admin=admin, approved=approved, asked=asked) return redirect_to_user('Event {} does not exist.' .format(event_name), is_error=True)
def test_correct_message_sandbox(client_session, makedrop_event, opening_date, public_date, closing_date, expected): client, session = client_session event = get_event(session, 'iris_test_4event') event.opening_timestamp = opening_date event.public_opening_timestamp = public_date event.closing_timestamp = closing_date session.commit() sign_up_team(session, 'iris_test_4event', 'test_user') with login_scope(client, 'test_user', 'test') as client: rv = client.get('http://localhost/events/iris_test_4event/sandbox') assert rv.status_code == 200 if NOW < opening_date: assert "Event submissions will open on the " in str(rv.data) elif NOW < closing_date: assert "Event submissions are open until " in str(rv.data) else: assert "This event closed on the " in str(rv.data)