Esempio n. 1
0
def before_request() -> None:
    """ Get geo data and institutional affiliation from ip address. """
    global geoip_reader
    try:
        if geoip_reader:
            # For new db or new session vars, can force a re-check by incrementing 'geoip.version'.
            geoip_version = '1'
            if 'geoip.version' not in session or session['geoip.version'] != geoip_version:
                session['geoip.version'] = geoip_version
                # https://geoip2.readthedocs.io/en/latest/
                response = geoip_reader.city(request.remote_addr)
                if response:
                    if response.continent:
                        session['continent'] = {
                            'code': response.continent.code,
                            'name': response.continent.names['en']
                        }
                    if response.country and response.country.iso_code:
                        session['country'] = response.country.iso_code
                    if response.subdivisions and response.subdivisions.most_specific and response.subdivisions.most_specific.iso_code:
                        session['subnational'] = response.subdivisions.most_specific.iso_code
                    if response.city and response.city.name:
                        session['city'] = response.city.name
    #except AddressNotFoundError as ex:
    #    logger.debug('problem getting match on IP: %s', ex)
    except ValueError as ex:
        logger.debug('problem with IP address format: %s', ex)
    except Exception as ex:
        logger.debug('problem using geoip: %s', ex)

    try:
        if 'hashed_user_id' not in session:
            if hasattr(request, "auth") and hasattr(request.auth, "user"): # type: ignore
                user_id = str(request.auth.user.user_id).encode('utf-8')   # type: ignore
                salt = bcrypt.gensalt()
                tmp = bcrypt.hashpw(user_id, salt)
                hashed_user_id = str(tmp, 'utf-8')
                session["hashed_user_id"] = hashed_user_id
    except Exception as ex:
        logger.debug('problem creating hashed_user_id: %s', ex)

    try:
        # Institution: store first institution found in a cookie.
        #   Users who visit multiple institutions keep first until session expires.
        #   Multiple device/browsers have separate pendo sessions
        if 'institution' not in session or 'institution_id' not in session:
            inst_hash = get_institution(request.remote_addr)
            if inst_hash != None and inst_hash.get("id") != None:
                session['institution_id'] = inst_hash.get("id")
                session['institution']    = inst_hash.get("label")
    except Exception as ex:
        logger.debug('problem looking up institution: %s', ex)
Esempio n. 2
0
def before_request() -> None:
    """Get instituional affiliation from session."""
    global geoip_reader
    if geoip_reader and 'contintent' not in session:
        session['continent'] = None
        try:
            response = geoip_reader.city(request.remote_addr)
            logger.debug(f'continent {response.continent.code}')
            session['continent'] = {
                'code': response.continent.code,
                'name': response.continent.names['en']
            }

        except Exception as ex:
            logger.debug(f'problem getting match on IP: {ex}')

    if 'institution' not in session:
        logger.debug('Adding institution to session')
        session['institution'] = get_institution(request.remote_addr)
Esempio n. 3
0
def before_request() -> None:
    """Get instituional affiliation from session."""
    if 'institution' not in session:
        logger.debug('Adding institution to session')
        session['institution'] = get_institution(request.remote_addr)
Esempio n. 4
0
def before_request() -> None:
    """Get instituional affiliation from session."""
    if 'institution' not in session:
        session['institution'] = get_institution(request.remote_addr)