Spaces:
Sleeping
Sleeping
| 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 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 get_user_teams(username: str) -> pd.DataFrame: | |
| """ | |
| Return the teams associated with a given username. | |
| :param username:The unique hf username to query. | |
| :return: The teams associated with a given username as a DataFrame, | |
| with human-readable columns for role and registration status. | |
| """ | |
| EMPTY_RESULT = pd.DataFrame( | |
| columns=["team_name", "is_leader", "registration_status"] | |
| ) | |
| participants_df = load_data_from_dataset( | |
| REGISTRATION_REPO, PARTICIPANTS_FILE_NAME, PARTICIPANT_COLUMNS | |
| ) | |
| if participants_df.empty: | |
| return EMPTY_RESULT | |
| existing_participant = participants_df[participants_df["username"] == username] | |
| if existing_participant.empty: | |
| return EMPTY_RESULT | |
| return existing_participant[["team_name", "is_leader", "registration_status"]] | |
| def rename_user_teams(result: pd.DataFrame) -> pd.DataFrame: | |
| result["role"] = result["is_leader"].map( | |
| {True: "Leader", False: "Member", "True": "Leader", "False": "Member"} | |
| ) | |
| return result[["team_name", "role", "registration_status"]].rename( | |
| columns={"team_name": "Team Name"} | |
| ) | |