o
    <g$                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ d	gZzddlZW n eyF   d
ZY n	w dZeddg7 Zh dZG dd deZG dd dZdS )aU  
This module houses the GeoIP2 object, a wrapper for the MaxMind GeoIP2(R)
Python API (https://geoip2.readthedocs.io/). This is an alternative to the
Python GeoIP2 interface provided by MaxMind.

GeoIP(R) is a registered trademark of MaxMind, Inc.

For IP-based geolocation, this module requires the GeoLite2 Country and City
datasets, in binary format (CSV will not work!). The datasets may be
downloaded from MaxMind at https://dev.maxmind.com/geoip/geoip2/geolite2/.
Grab GeoLite2-Country.mmdb.gz and GeoLite2-City.mmdb.gz, and unzip them in the
directory corresponding to settings.GEOIP_PATH.
    N)settings)ValidationError)validate_ipv46_address)to_path)RemovedInDjango60Warning)cached_property
HAS_GEOIP2FTGeoIP2GeoIP2Exception>   GeoIP2-CityGeoLite2-CityDBIP-City-LiteGeoIP2-CountryGeoLite2-CountryDBIP-Country-Litec                   @   s   e Zd ZdS )r
   N)__name__
__module____qualname__ r   r   L/var/www/html/venv/lib/python3.10/site-packages/django/contrib/gis/geoip2.pyr
   1   s    c                   @   s   e Zd ZdZdZdZdZdZeeeeeefZ	dZ
dZd*ddZd	d
 Zdd Zedd Zedd Zedd ZddddZdd Zdd Zdd Zdd Zd+d d!Zd"d# Zd$d% Zd&d' Zed(d) ZdS ),r	   r               Nc                 C   s   || j vrtd| |pttdd}|pttdd}|p"ttdd}|s)tdt|}||| || fD ]}| rJ|| _tjj	||d	| _
 nq6td
| jj}|tvr^td| dS )aI  
        Initialize the GeoIP object. No parameters are required to use default
        settings. Keyword arguments may be passed in to customize the locations
        of the GeoIP datasets.

        * path: Base directory to where GeoIP data is located or the full path
            to where the city or country data files (*.mmdb) are located.
            Assumes that both the city and country data sets are located in
            this directory; overrides the GEOIP_PATH setting.

        * cache: The cache settings when opening up the GeoIP datasets. May be
            an integer in (0, 1, 2, 4, 8) corresponding to the MODE_AUTO,
            MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, and MODE_MEMORY,
            `GeoIPOptions` C API settings,  respectively. Defaults to 0,
            meaning MODE_AUTO.

        * country: The name of the GeoIP country data file. Defaults to
            'GeoLite2-Country.mmdb'; overrides the GEOIP_COUNTRY setting.

        * city: The name of the GeoIP city data file. Defaults to
            'GeoLite2-City.mmdb'; overrides the GEOIP_CITY setting.
        z Invalid GeoIP caching option: %s
GEOIP_PATHN
GEOIP_CITYzGeoLite2-City.mmdbGEOIP_COUNTRYzGeoLite2-Country.mmdbzDGeoIP path must be provided via parameter or the GEOIP_PATH setting.)modez@Path must be a valid database or directory containing databases.z#Unable to handle database edition: )cache_optionsr
   getattrr   r   is_file_pathgeoip2databaseReader_reader	_metadatadatabase_typeSUPPORTED_DATABASE_TYPES)selfpathcachecountrycityr'   r   r   r   __init__H   s.   
zGeoIP2.__init__c                 C   s   | j r
| j   d S d S N)r%   closer)   r   r   r   __del__|   s   zGeoIP2.__del__c                 C   s8   | j }d|j d|j }d| jj d| d| j dS )Nv.<z [z	] _path='z'>)r&   binary_format_major_versionbinary_format_minor_version	__class__r   r!   )r)   mversionr   r   r   __repr__   s   zGeoIP2.__repr__c                 C   s
   | j  S r/   )r%   metadatar1   r   r   r   r&      s   
zGeoIP2._metadatac                 C      d| j jv S )NCityr&   r'   r1   r   r   r   is_city      zGeoIP2.is_cityc                 C   r=   )NCountryr?   r1   r   r   r   
is_country   rA   zGeoIP2.is_countryFrequire_cityc                C   s   t |ttjtjfstdt|j |r | js t	d| j
 t |tr:zt| W n ty9   t|}Y nw | jrA| jjn| jj}||S )NzSGeoIP query must be a string or instance of IPv4Address or IPv6Address, not type %szInvalid GeoIP city data file: )
isinstancestr	ipaddressIPv4AddressIPv6Address	TypeErrortyper   r@   r
   r!   r   r   socketgethostbynamer%   r-   r,   )r)   queryrE   functionr   r   r   _query   s    

zGeoIP2._queryc                 C   s   | j |dd}|jr|jd nd}i d|jjd|jjd|jjd|jjd	|jj	d
|jjd|jj
d|jjd|jjd|jjd|jjd|rO|j	ndd|rW|jndd|jjd|jjd|rj|j	S dS )z
        Return a dictionary of city information for the given IP address or
        Fully Qualified Domain Name (FQDN). Some information in the dictionary
        may be undefined (None).
        TrD   r   Naccuracy_radiusr-   continent_codecontinent_namecountry_codecountry_nameis_in_european_unionlatitude	longitude
metro_codepostal_coderegion_coderegion_name	time_zonedma_coderegion)rQ   subdivisionslocationrR   r-   name	continentcoder,   iso_coderW   rX   rY   rZ   postalr^   )r)   rO   responser`   r   r   r   r-      sJ   	

zGeoIP2.cityc                 C      |  |d S )z9Return the country code for the given IP Address or FQDN.rU   r,   r)   rO   r   r   r   rU         zGeoIP2.country_codec                 C   ri   )z9Return the country name for the given IP Address or FQDN.rV   rj   rk   r   r   r   rV      rl   zGeoIP2.country_namec                 C   s2   | j |dd}|jj|jj|jj|jj|jjdS )z
        Return a dictionary with the country code and name when given an
        IP address or a Fully Qualified Domain Name (FQDN). For example, both
        '24.124.1.80' and 'djangoproject.com' are valid parameters.
        FrD   )rS   rT   rU   rV   rW   )rQ   rd   re   rc   r,   rf   rW   )r)   rO   rh   r   r   r   r,      s   zGeoIP2.countryrY   rX   c                    s0   t jdtdd | | t fdd|D S )Nz<GeoIP2.coords() is deprecated. Use GeoIP2.lon_lat() instead.r   
stacklevelc                 3   s    | ]} | V  qd S r/   r   ).0odatar   r   	<genexpr>   s    z GeoIP2.coords.<locals>.<genexpr>)warningswarnr   r-   tuple)r)   rO   orderingr   rr   r   coords   s   
zGeoIP2.coordsc                 C      |  |}|d |d fS )z@Return a tuple of the (longitude, latitude) for the given query.rY   rX   r-   r)   rO   rs   r   r   r   lon_lat      
zGeoIP2.lon_latc                 C   rz   )z@Return a tuple of the (latitude, longitude) for the given query.rX   rY   r{   r|   r   r   r   lat_lon   r~   zGeoIP2.lat_lonc                 C   s   ddl m} || |ddS )z/Return a GEOS Point object for the given query.r   )Pointi  )srid)django.contrib.gis.geosr   r}   )r)   rO   r   r   r   r   geos   s   zGeoIP2.geosc                 C   s   t jdtdd t||S )Nz2GeoIP2.open() is deprecated. Use GeoIP2() instead.r   rn   )ru   rv   r   r	   )cls	full_pathr+   r   r   r   open   s   
zGeoIP2.open)Nr   NN)rm   )r   r   r   	MODE_AUTOMODE_MMAP_EXT	MODE_MMAP	MODE_FILEMODE_MEMORY	frozensetr   r!   r%   r.   r2   r;   r   r&   r@   rC   rQ   r-   rU   rV   r,   ry   r}   r   r   classmethodr   r   r   r   r   r	   5   s>    
4



	)__doc__rH   rM   ru   django.confr   django.core.exceptionsr   django.core.validatorsr   django.utils._osr   django.utils.deprecationr   django.utils.functionalr   __all__geoip2.databaser"   ImportErrorr   r(   	Exceptionr
   r	   r   r   r   r   <module>   s*    
