From a7d1fd83ef3da3a3590fe55c61d6f7b69410669a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Fri, 12 Jan 2024 07:43:04 +0200 Subject: [PATCH] add "city" --- app/signup/admin.py | 4 ++-- app/signup/models.py | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/signup/admin.py b/app/signup/admin.py index e077f43..4ecd1be 100644 --- a/app/signup/admin.py +++ b/app/signup/admin.py @@ -7,7 +7,7 @@ class SignupAdmin(admin.ModelAdmin): _all_fields = ["email", "created_at", "anonymized_ip", "user_agent"] 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"] ordering = ["created_at"] - readonly_fields = _all_fields + ["country_name"] + readonly_fields = _all_fields + ["place_name"] diff --git a/app/signup/models.py b/app/signup/models.py index 0e09c42..80e953b 100644 --- a/app/signup/models.py +++ b/app/signup/models.py @@ -17,14 +17,13 @@ class Signup(models.Model): def __str__(self): return self.email - def _country(self): + def _city(self): try: - return GeoIP2().country(self.anonymized_ip) + return GeoIP2().city(self.anonymized_ip) except geoip2.errors.AddressNotFoundError: - return {} + return None - def country_name(self): - return self._country().get("country_name", "??") - - def country_code(self): - return self._country().get("country_code", "??") + def place_name(self): + if city := self._city(): + return "{}, {}".format(city["city"], city["country_name"]) + return "??"