fix geoip
This commit is contained in:
@@ -126,6 +126,4 @@ LOGGING = {
|
||||
},
|
||||
}
|
||||
|
||||
#GEOIP_PATH = environ["GEOIP_PATH"]
|
||||
#GEOIP_COUNTRY = "GeoIP.dat"
|
||||
#GEOIP_CITY = "GeoIPCity.dat"
|
||||
GEOIP_PATH = environ["GEOIP_PATH"]
|
||||
|
||||
@@ -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"]
|
||||
list_display = ["email", "created_at", "anonymized_ip", "user_agent"] + ["country_code"]
|
||||
list_filter = ["email", "user_agent"]
|
||||
ordering = ["created_at"]
|
||||
readonly_fields = _all_fields
|
||||
readonly_fields = _all_fields + ["country_name"]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.db import models
|
||||
import geoip2
|
||||
|
||||
#from django.contrib.gis.geoip2 import GeoIP2
|
||||
from django.db import models
|
||||
from django.contrib.gis.geoip2 import GeoIP2
|
||||
|
||||
class Signup(models.Model):
|
||||
email = models.EmailField()
|
||||
@@ -16,6 +17,14 @@ class Signup(models.Model):
|
||||
def __str__(self):
|
||||
return self.email
|
||||
|
||||
#def country(self):
|
||||
# g = GeoIP2()
|
||||
# return g.country(self.anonymized_ip)
|
||||
def _country(self):
|
||||
try:
|
||||
return GeoIP2().country(self.anonymized_ip)
|
||||
except geoip2.errors.AddressNotFoundError:
|
||||
return {}
|
||||
|
||||
def country_name(self):
|
||||
return self._country().get("country_name", "??")
|
||||
|
||||
def country_code(self):
|
||||
return self._country().get("country_code", "??")
|
||||
|
||||
Reference in New Issue
Block a user