(ip, user_id)
Fills the user location based on url.
Fills the user location based on url.
Fills the user location based on url .
def detect_location(ip, user_id):
    """
    Fills the user location based on url.
    """
    from biostar.accounts.models import Profile

    msg = f"location check for \tid={user_id}\tip={ip}"
    # The lookup needs to be turned on.
    if not settings.LOCATION_LOOKUP:
        message(f"skip {msg}")
        return

    message(f"{msg}")

    # Get the profile for the user
    profile = Profile.objects.filter(user__id=user_id).first()
    template = "messages/location-set.md"

    # Skip value if it has the word unknown in it
    def get(data, attr):
        value = data.get(attr, '')
        return "" if "unknown" in value.lower() else value.title()

    # Check and log location.
    if not profile.location:
        try:
            url = f"http://ip-api.com/json/{ip}"
            message(url)
            message(f"{ip}, {profile.user}, {url}")
            req = Request(url=url, headers={'User-Agent': 'Mozilla/5.0'})
            resp = urlopen(req, timeout=3).read()
            data = json.loads(resp)

            # Add the userid to the json
            data['uid'] = user_id

            # Log the return data.
            message(data)

            # city = get(data, "city")
            # region = get(data, "regionName")
            # country = get(data, "country")
            location = get(data, "country")

            msg = f"location result for \tid={user_id}\tip={ip}\tloc={location}"
            if location:
                Profile.objects.filter(user__id=user_id).update(location=location)
                context = dict(profile=profile, location=location)
                create_messages(template=template, user_ids=[user_id], extra_context=context)
                message(f"updated profile: {msg}")
            else:
                message(f"empty location: {msg}")

        except Exception as exc:

            message(exc)
def detect_location ( ip , user_id ) : from biostar . accounts . models import Profile msg = f"location check for \tid={user_id}\tip={ip}" # The lookup needs to be turned on. if not settings . LOCATION_LOOKUP : message ( f"skip {msg}" ) return message ( f"{msg}" ) # Get the profile for the user profile = Profile . objects . filter ( user__id = user_id ) . first ( ) template = "messages/location-set.md" # Skip value if it has the word unknown in it def get ( data , attr ) : value = data . get ( attr , '' ) return "" if "unknown" in value . lower ( ) else value . title ( ) # Check and log location. if not profile . location : try : url = f"http://ip-api.com/json/{ip}" message ( url ) message ( f"{ip}, {profile.user}, {url}" ) req = Request ( url = url , headers = { 'User-Agent' : 'Mozilla/5.0' } ) resp = urlopen ( req , timeout = 3 ) . read ( ) data = json . loads ( resp ) # Add the userid to the json data [ 'uid' ] = user_id # Log the return data. message ( data ) # city = get(data, "city") # region = get(data, "regionName") # country = get(data, "country") location = get ( data , "country" ) msg = f"location result for \tid={user_id}\tip={ip}\tloc={location}" if location : Profile . objects . filter ( user__id = user_id ) . update ( location = location ) context = dict ( profile = profile , location = location ) create_messages ( template = template , user_ids = [ user_id ] , extra_context = context ) message ( f"updated profile: {msg}" ) else : message ( f"empty location: {msg}" ) except Exception as exc : message ( exc )