Merge branch 'feature/region_name_resolving' into 'develop'

Feature/region name resolving

See merge request gm/gm-backend!55
This commit is contained in:
d.kuzmenko 2019-11-07 09:55:20 +00:00
commit 19d23668e1
2 changed files with 17 additions and 1 deletions

View File

@ -1,11 +1,13 @@
"""Main app methods.""" """Main app methods."""
import logging import logging
from typing import Tuple, Optional
from django.conf import settings from django.conf import settings
from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
from geoip2.models import City from geoip2.models import City
from typing import Union, Tuple, Optional
import pycountry
from main import models from main import models
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -83,3 +85,16 @@ def determine_user_city(ip_addr: str) -> Optional[City]:
except Exception as ex: except Exception as ex:
logger.warning(f'GEOIP Base exception: {ex}') logger.warning(f'GEOIP Base exception: {ex}')
return None return None
def determine_subdivision(
country_alpha2_code: str,
subdivision_code: Union[str, int]
) -> pycountry.Subdivision:
"""
:param country_alpha2_code: country code according to ISO 3166-1 alpha-2 standard
:param subdivision_code: subdivision code according to ISO 3166-2 without country code prefix
:return: subdivision
"""
iso3166_2_subdivision_code = f'{country_alpha2_code}-{subdivision_code}'
return pycountry.subdivisions.get(code=iso3166_2_subdivision_code)

View File

@ -17,6 +17,7 @@ markdown
django-filter==2.1.0 django-filter==2.1.0
djangorestframework-xml djangorestframework-xml
geoip2==2.9.0 geoip2==2.9.0
pycountry==19.8.18
django-phonenumber-field[phonenumbers]==2.1.0 django-phonenumber-field[phonenumbers]==2.1.0
django-timezone-field==3.1 django-timezone-field==3.1