Skip to content

Commit

Permalink
user location now set from ip
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Sep 9, 2024
1 parent 7cd95b5 commit c43c99e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions auctions/management/commands/set_user_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import requests
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.utils import timezone

from auctions.models import PageView, UserData
from auctions.models import Location, PageView, UserData


class Command(BaseCommand):
Expand All @@ -14,9 +15,8 @@ def handle(self, *args, **options):
# get users that have been on the site for at least 1 days, but have not set their location
recently = timezone.now() - datetime.timedelta(days=1)
users = UserData.objects.filter(
Q(latitude=0, longitude=0) | Q(location__isnull=True),
last_ip_address__isnull=False,
latitude=0,
longitude=0,
user__date_joined__lte=recently,
).order_by("-last_activity")[:100]
# build a list of IPs - bit awkward as we can't use single quotes here, and it has to be a string, not a list
Expand All @@ -26,7 +26,10 @@ def handle(self, *args, **options):
ip_list += f'"{user.last_ip_address}",'
ip_list = ip_list[:-1] + "]" # trailing , breaks things
# See here for more documentation: https://ip-api.com/docs/api:batch#test
r = requests.post("http://ip-api.com/batch?fields=25024", data=ip_list)
# lat and lng only
# r = requests.post("http://ip-api.com/batch?fields=25024", data=ip_list)
# lat lng and country
r = requests.post("http://ip-api.com/batch?fields=1106113", data=ip_list)
if r.status_code == 200:
ip_addresses = r.json()
# now, we cycle through users again and assign their location based on IP
Expand All @@ -35,8 +38,17 @@ def handle(self, *args, **options):
try:
if user.last_ip_address == value["query"]:
if value["status"] == "success":
user.latitude = value["lat"]
user.longitude = value["lon"]
if not user.latitude:
user.latitude = value["lat"]
if not user.longitude:
user.longitude = value["lon"]
if not user.location:
continent = Location.objects.filter(name=value["continent"]).first()
country = Location.objects.filter(name=value["country"]).first()
default = Location.objects.filter(name="Other").first()
user.location = next(
value for value in [continent, country, default] if value is not None
)
user.save()
print(f"assigning {user.user.email} with IP {user.last_ip_address} a location")
break
Expand All @@ -63,6 +75,7 @@ def handle(self, *args, **options):
:10000
]
for view in pageviews:
print(view)
other_view_with_same_ip = (
PageView.objects.exclude(latitude=0, longitude=0)
.filter(ip_address=view.ip_address)
Expand Down

0 comments on commit c43c99e

Please sign in to comment.