Пример #1
0
    def register_athlete(self, strava_athlete: sm.Athlete,
                         access_token: str) -> Athlete:
        """
        Ensure specified athlete is added to database, returns athlete model.

        :return: The added athlete model object.
        :rtype: :class:`bafs.model.Athlete`
        """
        session = meta.scoped_session()
        athlete = session.query(Athlete).get(strava_athlete.id)

        if athlete is None:
            athlete = Athlete()

        athlete.id = strava_athlete.id
        athlete_name = f"{strava_athlete.firstname} {strava_athlete.lastname}"
        athlete.profile_photo = strava_athlete.profile
        athlete.access_token = access_token

        def already_exists(display_name) -> bool:
            return (
                session.query(Athlete).filter(Athlete.id != athlete.id).filter(
                    Athlete.display_name == display_name).count() > 0)

        def unambiguous_display_name() -> str:
            display_name = f"{strava_athlete.firstname} {strava_athlete.lastname[0]}"
            if already_exists(display_name):
                self.logger.info(
                    f"display_name '{display_name}' conflicts, using '{athlete_name}'"
                )
                display_name = athlete_name
            return display_name

        # Only update the display name if it is either:
        # a new athlete, or the athlete name has changed
        try:
            if athlete_name != athlete.name:
                self.logger.info(
                    f"Athlete '{athlete_name}' was renamed '{athlete.name}'")
                athlete.display_name = unambiguous_display_name()
        except:
            self.logger.exception(
                f"Athlete name disambiguation error for {strava_athlete.id}",
                exc_info=True,
            )
            athlete.display_name = athlete_name
        finally:
            athlete.name = athlete_name
            session.add(athlete)
        return athlete
Пример #2
0
def register_athlete(strava_athlete, token_dict):
    """
    Ensure specified athlete is added to database, returns athlete orm.

    :return: The added athlete model object.
    :rtype: :class:`bafs.orm.Athlete`
    """
    athlete = meta.scoped_session().query(Athlete).get(strava_athlete.id)
    if athlete is None:
        athlete = Athlete()
    athlete.id = strava_athlete.id
    athlete.name = '{0} {1}'.format(strava_athlete.firstname,
                                    strava_athlete.lastname).strip()
    # Temporary; we will update this in disambiguation phase.  (This isn't optimal; needs to be
    # refactored....
    #
    #     Where does the disambiguation phase get called now? Nowhere...
    #     so let's fix it here for now.
    #     * Do not override already set display_names.
    #     * Use a first name and last initial (if available).
    #     See also:
    #       https://github.com/freezingsaddles/freezing-web/issues/80
    #       https://github.com/freezingsaddles/freezing-web/issues/75
    #       https://github.com/freezingsaddles/freezing-web/issues/73
    #     - @obscurerichard]
    if athlete.display_name is None:
        if strava_athlete.lastname is None:
            athlete.display_name = strava_athlete.firstname
        else:
            athlete.display_name = '{0} {1}'.format(
                strava_athlete.firstname.strip(),
                strava_athlete.lastname.strip(),
            )
    athlete.profile_photo = strava_athlete.profile

    athlete.access_token = token_dict['access_token']
    athlete.refresh_token = token_dict['refresh_token']
    athlete.expires_at = token_dict['expires_at']
    meta.scoped_session().add(athlete)
    # We really shouldn't be committing here, since we want to disambiguate names after registering
    meta.scoped_session().commit()

    return athlete
Пример #3
0
def register_athlete(strava_athlete, token_dict):
    """
    Ensure specified athlete is added to database, returns athlete orm.

    :return: The added athlete model object.
    :rtype: :class:`bafs.orm.Athlete`
    """
    athlete = meta.scoped_session().query(Athlete).get(strava_athlete.id)
    if athlete is None:
        athlete = Athlete()
    athlete.id = strava_athlete.id
    athlete.name = '{0} {1}'.format(strava_athlete.firstname, strava_athlete.lastname).strip()
    # Temporary; we will update this in disambiguation phase.  (This isn't optimal; needs to be
    # refactored....
    #
    #     Where does the disambiguation phase get called now? Nowhere...
    #     so let's fix it here for now.
    #     * Do not override already set display_names.
    #     * Use a first name and last initial (if available).
    #     See also:
    #       https://github.com/freezingsaddles/freezing-web/issues/80
    #       https://github.com/freezingsaddles/freezing-web/issues/75
    #       https://github.com/freezingsaddles/freezing-web/issues/73
    #     - @obscurerichard]
    if athlete.display_name is None:
        if strava_athlete.lastname is None:
            athlete.display_name = strava_athlete.firstname
        else:
            athlete.display_name = '{0} {1}'.format(
                    strava_athlete.firstname.strip(),
                    strava_athlete.lastname.strip(),
                    )
    athlete.profile_photo = strava_athlete.profile

    athlete.access_token = token_dict['access_token']
    athlete.refresh_token = token_dict['refresh_token']
    athlete.expires_at = token_dict['expires_at']
    meta.scoped_session().add(athlete)
    # We really shouldn't be committing here, since we want to disambiguate names after registering
    meta.scoped_session().commit()

    return athlete
Пример #4
0
    def register_athlete(self, strava_athlete: sm.Athlete, access_token: str):
        """
        Ensure specified athlete is added to database, returns athlete model.

        :return: The added athlete model object.
        :rtype: :class:`bafs.model.Athlete`
        """
        session = meta.scoped_session()
        athlete = session.query(Athlete).get(strava_athlete.id)
        if athlete is None:
            athlete = Athlete()
        athlete.id = strava_athlete.id
        athlete.name = '{0} {1}'.format(strava_athlete.firstname,
                                        strava_athlete.lastname).strip()

        display_name = strava_athlete.firstname + ' ' + strava_athlete.lastname[
            0]

        def already_exists(display_name):
            return session.query(Athlete).filter(Athlete.id != athlete.id)\
                       .filter(Athlete.display_name == display_name)\
                       .count() > 0

        charidx = 1
        while already_exists(display_name):
            try:
                display_name += str(strava_athlete.lastname[charidx])
                charidx += 1
            except IndexError:
                self.logger.warning(
                    "Ran out of last-name letters to disambiguate {}".format(
                        athlete))
                break

        athlete.display_name = display_name
        athlete.profile_photo = strava_athlete.profile

        athlete.access_token = access_token
        session.add(athlete)

        return athlete