1
Fork 0
e11sync/app/signup/models.py

30 lines
807 B
Python
Raw Normal View History

2024-01-12 01:31:38 +02:00
import geoip2
2023-12-14 18:27:56 +02:00
2024-01-12 01:31:38 +02:00
from django.db import models
from django.contrib.gis.geoip2 import GeoIP2
2024-01-12 00:58:20 +02:00
2023-12-14 18:27:56 +02:00
class Signup(models.Model):
email = models.EmailField()
created_at = models.DateTimeField(auto_now_add = True)
anonymized_ip = models.GenericIPAddressField()
user_agent = models.CharField(max_length = 255)
class Meta:
constraints = [
models.UniqueConstraint(name = "unique_email", fields = ["email"]),
]
def __str__(self):
return self.email
2024-01-12 00:58:20 +02:00
2024-01-12 07:43:04 +02:00
def _city(self):
2024-01-12 01:31:38 +02:00
try:
2024-01-12 07:43:04 +02:00
return GeoIP2().city(self.anonymized_ip)
2024-01-12 01:31:38 +02:00
except geoip2.errors.AddressNotFoundError:
2024-01-12 07:43:04 +02:00
return None
2024-01-12 01:31:38 +02:00
2024-01-12 07:43:04 +02:00
def place_name(self):
if city := self._city():
return "{}, {}".format(city["city"], city["country_name"])
return "??"