Exemplo n.º 1
0
def _get_user_language(user_id: int, session: Session) -> str:
    """Returns a user's language setting.

    Args:
        user_id: The User's ID.
        session: The database connection.

    Returns:
        The language name.

    Raises:
        HTTPException: If no language or multiple languages were
            found for the user.
    """
    try:
        user = get_users(session, id=user_id)[0]
    except IndexError:
        logger.exception("User was not found in the database.")
        return ""
    try:
        return _get_language_by_id(user.language_id, session)
    except (AttributeError, MultipleResultsFound, NoResultFound) as e:
        logger.critical(e)
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail='Error raised',
        )
Exemplo n.º 2
0
def _get_user_language(user_id: int, session: SessionLocal) -> str:
    """
    Gets a user-id and returns the language he speaks
    Uses the DB"""
    try:
        user = get_users(session, id=user_id)[0]
    except IndexError:
        logger.exception("User was not found in the database.")
        return ""
    else:
        return user.language
Exemplo n.º 3
0
def get_participants_statistics(
    db: Session,
    userid: int,
    start: datetime.datetime,
    end: datetime.datetime,
) -> Dict[str, Dict[str, Union[str, int]]]:
    """calculate statistics for events participants relevant for
        the requested user and the requested date range.
        part of the logic is performed in the db,
        while the rest is in the code.

    Args:
        db: db session.
        userid: requested user id number for statistics.
        start: start of date range.
        end: end of date range.

    Returns: json with
        max_events: maximum number of events the user has
            with same participant.
        participant_name: relevant participant name.
    """
    by_user_id = UserEvent.user_id == userid
    by_not_user_id = UserEvent.user_id != userid
    user_to_event = (UserEvent, UserEvent.event_id == Event.id)
    participant_count = func.count(UserEvent.user_id)
    subquery = (db.query(
        Event.id).join(user_to_event).filter(by_user_id).filter(
            get_date_filter_between_dates(start, end)).subquery())
    event_participants = (db.query(
        UserEvent.user_id, participant_count).filter(by_not_user_id).filter(
            UserEvent.event_id.in_(subquery)).group_by(
                UserEvent.user_id).order_by(participant_count.desc()).first())
    if event_participants:
        return {
            "participants_statistics": {
                "max_events":
                event_participants[1],
                "participant_name":
                get_users(db, id=event_participants[0])[0].username,
            }
        }
    return {
        "participants_statistics": {
            "max_events": 0,
            "participant_name": ""
        }
    }
Exemplo n.º 4
0
def send_in_app_invitation(participants: List[str], event: Event,
                           session: Session) -> bool:
    """Sends an in-app invitation for registered users."""

    for participant in participants:
        # email is unique
        recipient = get_users(email=participant, session=session)[0]

        if recipient.id != event.owner.id:
            session.add(Invitation(recipient=recipient, event=event))

        else:
            # if user tries to send to themselves.
            return False

    session.commit()
    return True
Exemplo n.º 5
0
 def test_get_users_failure(self, session, user):
     assert get_users(username='******', session=session) == []
     assert get_users(wrong_param=user.username, session=session) == []
Exemplo n.º 6
0
 def test_get_users_success(self, user, session):
     assert get_users(username=user.username, session=session) == [user]
     assert get_users(password=user.password, session=session) == [user]
     assert get_users(email=user.email, session=session) == [user]