Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add teams 2/3: Add functions to manage teams #547

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ramp-database/ramp_database/model/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ def set_n_submissions(self):
each team."""
self.n_submissions = 0
for event_team in self.event_teams:
# substract one for starting kit
self.n_submissions += len(event_team.submissions) - 1
if event_team.team.is_individual:
# substract one for starting kit
self.n_submissions += len(event_team.submissions) - 1
else:
self.n_submissions += len(event_team.submissions)

@property
def Predictions(self):
Expand Down Expand Up @@ -567,3 +570,8 @@ def __init__(self, event, team):

def __repr__(self):
return "{}/{}".format(self.event, self.team)

@property
def is_locked(self):
"""Check if event team is locked"""
return len(self.submissions) > 1
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def setup_ramp_kit_ramp_data(
'it, you need to set "force=True".'
)
shutil.rmtree(problem_kit_path, ignore_errors=True)
ramp_kit_url = "https://github.com/ramp-kits/{}.git".format(problem_name)
ramp_kit_url = f"https://github.com/ramp-kits/{problem_name}.git"
kwargs = {}
if depth is not None:
kwargs["depth"] = depth
Expand Down
88 changes: 88 additions & 0 deletions ramp-database/ramp_database/tools/_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
reduces the complexity of having queries and database connection in the same
file. Then, those queries are tested through the public API.
"""
from typing import Optional, List

from ..model import Event
from ..model import EventAdmin
from ..model import EventTeam
Expand All @@ -14,6 +16,7 @@
from ..model import SubmissionSimilarity
from ..model import Team
from ..model import User
from ..model import UserTeam
from ..model import Workflow
from ..model import WorkflowElement
from ..model import WorkflowElementType
Expand Down Expand Up @@ -145,6 +148,57 @@ def select_event_team_by_name(session, event_name, team_name):
)


def select_event_team_by_user_name(
session, event_name: str, user_name: str
) -> Optional[EventTeam]:
"""Query an event-team entry given the event and user name.

Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to query the database.
event_name : str
The name of the RAMP event.
user_name : str
The name of the user.

Returns
-------
event_team : :class:`ramp_database.model.EventTeam`
The queried event-team.
"""
event = select_event_by_name(session, event_name)
user = select_user_by_name(session, user_name)
if event is None or user is None:
return None

event_team = (
session.query(EventTeam)
.filter(
EventTeam.event == event,
EventTeam.team_id == UserTeam.team_id,
UserTeam.user == user,
UserTeam.status == "accepted",
)
.first()
)

if event_team is None:
# No classical team found. Fall back to individual teams

event_team = (
session.query(EventTeam)
.filter(
EventTeam.event == event,
EventTeam.team_id == Team.id,
Team.admin == user,
Team.name == user.name,
)
.one_or_none()
)
return event_team


def select_user_by_name(session, user_name):
"""Query an user given its name.

Expand Down Expand Up @@ -203,6 +257,40 @@ def select_team_by_name(session, team_name):
return session.query(Team).filter(Team.name == team_name).one_or_none()


def select_team_invites_by_user_name(
session, event_name: str, user_name: str
) -> List[Team]:
"""Query a team given its name.

Parameters
----------
session : :class:`sqlalchemy.orm.Session`
The session to query the database.
event_name : str
The name of the RAMP event.
user_name : str
The user name to query.

Returns
-------
teams : a list of :class:`ramp_database.model.Team`
A list of team invites
"""
return (
session.query(Team)
.filter(
EventTeam.team_id == Team.id,
EventTeam.event_id == Event.id,
Team.id == UserTeam.team_id,
UserTeam.user_id == User.id,
UserTeam.status == "asked",
User.name == user_name,
Event.name == event_name,
)
.all()
)


def select_problem_by_name(session, problem_name):
"""Query a problem given its name.

Expand Down
18 changes: 16 additions & 2 deletions ramp-database/ramp_database/tools/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import shutil
from typing import Optional

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -36,7 +37,14 @@

# Add functions: add information to the database
# TODO: move the queries in "_query"
def add_submission(session, event_name, team_name, submission_name, submission_path):
def add_submission(
session,
event_name,
team_name,
submission_name,
submission_path,
user_name: Optional[str] = None,
):
"""Create a submission in the database and returns an handle.

Parameters
Expand All @@ -53,6 +61,9 @@ def add_submission(session, event_name, team_name, submission_name, submission_p
The path of the files associated to the current submission. It will
corresponds to the key `ramp_kit_subissions_dir` of the dictionary
created with :func:`ramp_utils.generate_ramp_config`.
user_name: str, default=None
Optional user name to make the submission as for non
individual teams. This is only used for audits.

Returns
-------
Expand Down Expand Up @@ -101,7 +112,10 @@ def add_submission(session, event_name, team_name, submission_name, submission_p
)

submission = Submission(
name=submission_name, event_team=event_team, session=session
name=submission_name,
event_team=event_team,
session=session,
user_name=user_name,
)
for cv_fold in event.cv_folds:
submission_on_cv_fold = SubmissionOnCVFold(
Expand Down
Loading