1
Fork 0

add "city"

This commit is contained in:
Motiejus Jakštys 2024-01-12 07:43:04 +02:00
parent 762de74ffa
commit a7d1fd83ef
2 changed files with 9 additions and 10 deletions

View File

@ -7,7 +7,7 @@ class SignupAdmin(admin.ModelAdmin):
_all_fields = ["email", "created_at", "anonymized_ip", "user_agent"] _all_fields = ["email", "created_at", "anonymized_ip", "user_agent"]
date_hierarchy = "created_at" date_hierarchy = "created_at"
list_display = ["email", "created_at", "anonymized_ip", "user_agent"] + ["country_code"] list_display = ["email", "created_at", "anonymized_ip", "user_agent"] + ["place_name"]
list_filter = ["email", "user_agent"] list_filter = ["email", "user_agent"]
ordering = ["created_at"] ordering = ["created_at"]
readonly_fields = _all_fields + ["country_name"] readonly_fields = _all_fields + ["place_name"]

View File

@ -17,14 +17,13 @@ class Signup(models.Model):
def __str__(self): def __str__(self):
return self.email return self.email
def _country(self): def _city(self):
try: try:
return GeoIP2().country(self.anonymized_ip) return GeoIP2().city(self.anonymized_ip)
except geoip2.errors.AddressNotFoundError: except geoip2.errors.AddressNotFoundError:
return {} return None
def country_name(self): def place_name(self):
return self._country().get("country_name", "??") if city := self._city():
return "{}, {}".format(city["city"], city["country_name"])
def country_code(self): return "??"
return self._country().get("country_code", "??")