o
    di6                     @   s   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 dlmZ G d	d
 d
eZG dd deZG dd deZG dd deZdS )    N)settings)geodesic)GoogleV3status)AllowAny)Response)APIViewc                   @   $   e Zd ZdZefZdgZdd ZdS )NearbyPlacesAPIView~  
    API view for retrieving nearby places based on location and optional place type.

    Attributes:
        permission_classes (tuple): Specifies the permissions required to access this view.
        http_method_names (list): A list of HTTP methods that this view supports.

    Methods:
        get(request, *args, **kwargs): Handles GET requests to retrieve nearby places.
    getc                 O   s  |j d}|stdditjdS tj}tt|	d\}}|j d}|j d}	zt
j|d}
| d| tjd	}|rB||d
< |	rH|	|d< |
jd i |}g }|dg D ]l}|
j|d g dd}|d }g }|dg D ]}|d}|rd| d| }|| qqt||f|d d d |d d d fj}||d|dd|d|d d d |d d d |t|dd qX|d}	||	d}t|tjdW S  t
jjy } ztg tjdW  Y d}~S d}~ww )!a  
        Handles GET requests to retrieve nearby places.

        Args:
            request (HttpRequest): The HTTP request object.
            *args: Variable-length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Response: A response containing a list of nearby places.

        Raises:
            HttpResponseBadRequest: If the required 'location' parameter is not provided.
        locationmessagezLocation is requiredr   ,
place_typenext_page_token)key)r   radiustypeZ
page_tokenresultsplace_id)nameformatted_addressformatted_phone_numbergeometryphoto)fieldsresultphotosphoto_referencezMhttps://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=z&key=r   ZlatZlngr   r    r      )r   addressphone_numberlatitude	longituder   distance)r   nextN )GETr   r   r   HTTP_400_BAD_REQUESTr   GOOGLE_MAP_API_KEYmapfloatsplit
googlemapsClientNEAR_BY_PLACE_RADIUSZplaces_nearbyplaceappendr   Z
kilometersroundHTTP_200_OK
exceptionsZApiError)selfrequestargskwargsr   api_keyr%   r&   r   r   ZgmapsparamsZplaces_resultplacesr3   place_detailsr   Z
photo_infor    Z	photo_urlr'   responseerrr)   r)   a/var/www/html/evchargy.com/django-station-ev/station_ev/apps/station/views/nearby_places_views.pyr      s~   






zNearbyPlacesAPIView.getN__name__
__module____qualname____doc__r   permission_classeshttp_method_namesr   r)   r)   r)   rB   r      
    r   c                   @   r
   )GetAddressFromLatLngAPIViewa  
    API view to retrieve address information from latitude and longitude.

    This API endpoint expects GET requests with latitude and longitude query parameters.
    - Required parameters:
        - latitude: Decimal latitude value.
        - longitude: Decimal longitude value.

    Returns a JSON response with the address information found for the given coordinates,
    or an appropriate error message if the request is invalid or the address cannot be found.
    r   c              
   O   s   |j d}|j d}|r|stdditjdS z+tj}t|d}|| d| }|r;d|j	i}	t|	tj
dW S tdd	itjdW S  ty_ }
 ztdd	itjdW  Y d
}
~
S d
}
~
ww )at  
        Handle GET requests to retrieve address information.

        - Retrieves latitude and longitude values from query parameters.
        - Validates that both latitude and longitude are provided.
        - Retrieves API key from settings (assuming `GOOGLE_MAP_API_KEY` is defined).
        - Creates a GoogleV3 geolocator using the API key.
        - Uses the geolocator to perform reverse geocoding on the provided coordinates.
        - Returns a JSON response with the address information if found, otherwise an error message.
        - Handles exceptions gracefully and returns an appropriate error response.
        r%   r&   r   z#Latitude and Longitude are requiredr   r<   z, r#   zAddress not foundN)r*   r   r   r   r+   r   r,   r   reverser#   r6   HTTP_404_NOT_FOUND	Exception)r8   r9   r:   r;   r%   r&   r<   
geolocatorr   r@   er)   r)   rB   r      s2   

zGetAddressFromLatLngAPIView.getNrC   r)   r)   r)   rB   rK      s
    rK   c                   @   r
   )GetLatLngFromAddressAPIViewa  
    API view to retrieve latitude and longitude from an address.

    This API endpoint expects GET requests with an address query parameter.
    - Required parameter:
        - address: Textual description of the address.

    Returns a JSON response with the latitude and longitude coordinates found for the address,
    or an appropriate error message if the request is invalid or the address cannot be found.
    r   c           
   
   O   s   |j d}|stdditjdS z(tj}t|d}||}|r0|j	|j
d}t|tjdW S tdditjdW S  tyT }	 ztdditjdW  Y d}	~	S d}	~	ww )	ah  
        Handle GET requests to retrieve latitude and longitude from an address.

        - Retrieves address value from query parameter.
        - Validates that the address is provided.
        - Retrieves API key from settings (assuming `GOOGLE_MAP_API_KEY` is defined).
        - Creates a GoogleV3 geolocator using the API key.
        - Uses the geolocator to perform geocoding on the provided address.
        - Returns a JSON response with the latitude and longitude coordinates if found, otherwise an error message.
        - Handles exceptions gracefully and returns an appropriate error response.
        r#   r   zAddress param is requiredr   rL   )r%   r&   zLatitude, Longitude not foundN)r*   r   r   r   r+   r   r,   r   Zgeocoder%   r&   r6   rN   rO   )
r8   r9   r:   r;   r#   r<   rP   r   r@   rQ   r)   r)   rB   r      s4   

zGetLatLngFromAddressAPIView.getNrC   r)   r)   r)   rB   rR      rJ   rR   c                   @   r
   )GetPlacesAPIViewr   r   c                 O   s   |j d}|stg tjdS d}tj}||d}tj||d}| }	|j	dkrv|	ddkrv|	rog }
t
|	d	g D ].\}}d
}||ddd}tj||d}| }|j	dkrj|ddkrj|
|d q<t|
S tg tjdS tg tjdS )Nsearchr   z<https://maps.googleapis.com/maps/api/place/autocomplete/json)inputr   )r=      r   OKZpredictionsz7https://maps.googleapis.com/maps/api/place/details/jsonr   zKplace_id,name,formatted_address,geometry,adr_address,formatted_phone_number)r   Zplaceidr   r   )r*   r   r   r   r6   r   r,   requestsjsonstatus_code	enumerater4   )r8   r9   r:   r;   Zinout_searchurlr<   r=   r@   Z
place_dataZplace_details_data_listindexr?   Z
detail_urlZdetail_paramsZdetail_responseZplace_detail_datar)   r)   rB   r   !  s:   


zGetPlacesAPIView.getNrC   r)   r)   r)   rB   rS     s
    rS   )r0   rX   django.confr   Zgeopy.distancer   Zgeopy.geocodersr   rest_frameworkr   Zrest_framework.permissionsr   rest_framework.responser   rest_framework.viewsr	   r   rK   rR   rS   r)   r)   r)   rB   <module>   s     AB