Fix #3391 -- Don't crash on GeoIP lookup failure

This commit is contained in:
Raphael Michel
2023-06-06 17:12:17 +02:00
parent 0b4064f14f
commit 50ff968c17
2 changed files with 12 additions and 4 deletions

View File

@@ -61,6 +61,7 @@ from django.utils.timezone import get_current_timezone, now
from django.utils.translation import gettext_lazy as _, pgettext_lazy from django.utils.translation import gettext_lazy as _, pgettext_lazy
from django_countries import countries from django_countries import countries
from django_countries.fields import Country, CountryField from django_countries.fields import Country, CountryField
from geoip2.errors import AddressNotFoundError
from phonenumber_field.formfields import PhoneNumberField from phonenumber_field.formfields import PhoneNumberField
from phonenumber_field.phonenumber import PhoneNumber from phonenumber_field.phonenumber import PhoneNumber
from phonenumber_field.widgets import PhoneNumberPrefixWidget from phonenumber_field.widgets import PhoneNumberPrefixWidget
@@ -356,9 +357,12 @@ class WrappedPhoneNumberPrefixWidget(PhoneNumberPrefixWidget):
def guess_country_from_request(request, event): def guess_country_from_request(request, event):
if settings.HAS_GEOIP: if settings.HAS_GEOIP:
g = GeoIP2() g = GeoIP2()
res = g.country(get_client_ip(request)) try:
if res['country_code'] and len(res['country_code']) == 2: res = g.country(get_client_ip(request))
return Country(res['country_code']) if res['country_code'] and len(res['country_code']) == 2:
return Country(res['country_code'])
except AddressNotFoundError:
pass
return guess_country(event) return guess_country(event)

View File

@@ -25,6 +25,7 @@ import time
from django.conf import settings from django.conf import settings
from django.contrib.gis.geoip2 import GeoIP2 from django.contrib.gis.geoip2 import GeoIP2
from django.core.cache import cache from django.core.cache import cache
from geoip2.errors import AddressNotFoundError
from pretix.helpers.http import get_client_ip from pretix.helpers.http import get_client_ip
@@ -50,7 +51,10 @@ def _get_country(request):
if not _geoip: if not _geoip:
_geoip = GeoIP2() _geoip = GeoIP2()
res = _geoip.country(get_client_ip(request)) try:
res = _geoip.country(get_client_ip(request))
except AddressNotFoundError:
return None
return res['country_code'] return res['country_code']