Spaces:
Running
Running
| import json | |
| import logging | |
| import re | |
| import pandas as pd | |
| from about import REGISTRATION_REPO | |
| from components.registration.config import PARTICIPANTS_FILE_NAME, PARTICIPANT_COLUMNS | |
| from components.utils import load_data_from_dataset | |
| logger = logging.getLogger(__name__) | |
| def validate_email_domain(email: str) -> bool: | |
| """Return True if email belongs to an academic or registered company domain.""" | |
| academic_tlds = [".edu", ".ac.uk", ".ac.", "university", "univ.", "institute", ".gov"] | |
| return any(token in email.lower() for token in academic_tlds) | |
| def get_team_membership_entry(team_name: str, is_leader: bool) -> dict[str, str | bool]: | |
| """Return a dictionary with information about a team.""" | |
| return {"team_name": team_name, "is_leader": is_leader} | |
| def validate_email_format(email: str) -> bool: | |
| """ | |
| Validate the email format. | |
| :param email: The email address to validate. | |
| :return: True if the email is valid, False otherwise. | |
| """ | |
| return bool(re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", email)) | |
| def is_institutional_email(email: str) -> bool: | |
| """Check if an email address is an institutional email.""" | |
| patterns = [".edu", ".ac.", ".gov", "university", "univ.", "institute", "research"] | |
| return any(p in email.lower() for p in patterns) | |
| def _participant_team_names(participant_row: pd.Series) -> list[str]: | |
| """Return list of team_name the participant already belongs to.""" | |
| memberships = json.loads(participant_row.get("team_memberships") or "[]") | |
| return [m["team_name"] for m in memberships] | |
| def get_user_teams(email: str) -> pd.DataFrame: | |
| """ | |
| Return the teams associated with a given email. | |
| :param email: The email address to query. | |
| :return: The teams associated with a given email as a DataFrame. | |
| """ | |
| participants_df = load_data_from_dataset(REGISTRATION_REPO, PARTICIPANTS_FILE_NAME, PARTICIPANT_COLUMNS) | |
| existing_participant = participants_df[participants_df["email"] == email] | |
| if existing_participant.empty: | |
| return pd.DataFrame(columns=["team_name", "role"]) | |
| existing_teams = json.loads( | |
| existing_participant.iloc[0].get("team_memberships") or "[]" | |
| ) | |
| if not existing_teams: | |
| return pd.DataFrame(columns=["team_name", "role"]) | |
| df = pd.DataFrame(existing_teams) | |
| df["role"] = df["is_leader"].apply(lambda x: "Leader" if x else "Member") | |
| return df[["team_name", "role"]] |