30 lines
807 B
Python
30 lines
807 B
Python
import geoip2
|
|
|
|
from django.db import models
|
|
from django.contrib.gis.geoip2 import GeoIP2
|
|
|
|
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
|
|
|
|
def _city(self):
|
|
try:
|
|
return GeoIP2().city(self.anonymized_ip)
|
|
except geoip2.errors.AddressNotFoundError:
|
|
return None
|
|
|
|
def place_name(self):
|
|
if city := self._city():
|
|
return "{}, {}".format(city["city"], city["country_name"])
|
|
return "??"
|