
    six                     X   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	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 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"  G d de      Z# G d de      Z$ G d de      Z% G d de      Z&d Z'd Z(e ed      d               Z)y)    )messages)login_requiredpermission_required)	QueryDict)get_object_or_404redirectrender)method_decorator)gettext_lazy)geodesicstatus)PageNumberPagination)IsAuthenticatedResponse)APIView)Company)GeoFencingSetupForm   )
GeoFencing)*c                   r    e Zd ZegZ e edd      d      d        Z e edd      d      d        Zy	)
GeoFencingSetupGetPostAPIViewgeofencing.view_geofencingTraise_exceptiondispatchnamec                     |j                   j                  j                         }t        t        |j
                        }t        |      }t        |j                  t        j                        S )Npkr   )useremployee_getget_companyr   r   idGeoFencingSetupSerializerr   datar   HTTP_200_OK)selfrequestcompanylocation
serializers        4/var/www/python-projects/worksol/geofencing/views.pygetz!GeoFencingSetupGetPostAPIView.get   sM    
 ,,++779$ZGJJ?.x8

0B0BCC    zgeofencing.add_geofencingc                    |j                   }|j                  j                  sUt        |t              r|j                         }|j                  j                  j                         }|r|j                  |d<   t        |j                         }|j                         r5|j                          t        |j                   t        j                        S t        |j                  t        j                         S )N
company_idr)   r   )r)   r$   is_superuser
isinstancer   dictr%   r&   r'   r(   is_validsaver   r   HTTP_201_CREATEDerrorsHTTP_400_BAD_REQUEST)r+   r,   r)   r-   r/   s        r0   postz"GeoFencingSetupGetPostAPIView.post"   s    
 ||||(($	*yy{ll//;;=G%,ZZ\".GLLA
 OOJOOF4K4KLL
))&2M2MNNr2   N)	__name__
__module____qualname__r   permission_classesr
   r   r1   r>    r2   r0   r   r      s[    )*8$OD	D 7NO	Or2   r   c                   x    e Zd ZegZd Z e edd      d      d        Z e edd      d      d	        Z	y
)GeoFencingSetupPutDeleteAPIViewc                     	 t         j                  j                  |      S # t        $ r}t        j                  |      d }~ww xY w)Nr"   )r   objectsr1   	ExceptionserializersValidationError)r+   r#   es      r0   get_locationz,GeoFencingSetupPutDeleteAPIView.get_location8   sB    	1%%))R)00 	1--a00	1s   " 	AA  Azgeofencing.change_geofencingTr   r   r   c                    | j                  |      }|j                  j                  j                         }|j                  j                  s||j
                  k(  rt        ||j                  d      }|j                         r5|j                          t        |j                  t        j                        S t        |j                  t        j                        S t        j!                  d      )NT)r)   partialr   Access Denied..)rL   r$   r%   r&   r6   r4   r(   r)   r9   r:   r   r   r*   r<   r=   rI   rJ   )r+   r,   r#   r.   r-   r/   s         r0   putz#GeoFencingSetupPutDeleteAPIView.put>   s    
 $$R(,,++779<<$$83F3F(F2w||TJ ""$!
8J8JKKJ--f6Q6QRR))*;<<r2   zgeofencing.delete_geofencingc                 :   | j                  |      }|j                  j                  j                         }|j                  j                  s||j
                  k(  r-|j                          t        ddit        j                        S t        j                  d      )Nmessagez(GeoFencing location deleted successfullyr   rO   )rL   r$   r%   r&   r6   r4   deleter   r   r*   rI   rJ   )r+   r,   r#   r.   r-   s        r0   rS   z&GeoFencingSetupPutDeleteAPIView.deleteO   s    
 $$R(,,++779<<$$83F3F(FOOFG))  ))*;<<r2   N)
r?   r@   rA   r   rB   rL   r
   r   rP   rS   rC   r2   r0   rE   rE   5   s^    )*1 :DQ=	= :DQ	=		=r2   rE   c                   $    e Zd ZegZd Zd Zd Zy)&GeoFencingEmployeeLocationCheckAPIViewc                     	 |j                   j                  j                         }|S # t        $ r}t        j                  |      d }~ww xY w)N)r$   r%   r&   rH   rI   rJ   )r+   r,   r-   rK   s       r0   r&   z2GeoFencingEmployeeLocationCheckAPIView.get_companyb   sE    	1ll//;;=GN 	1--a00	1s   %( 	AAAc                     | j                  |      }	 t        j                  j                  |      }|S # t        $ r}t
        j                  |      d }~ww xY wN)r4   r&   r   rG   r1   rH   rI   rJ   )r+   r,   r-   r.   rK   s        r0   get_company_locationz;GeoFencingEmployeeLocationCheckAPIView.get_company_locationi   sV    ""7+	1!))---AHO 	1--a00	1s   !5 	AAAc                 V   t        |j                        }| j                  |      }|j                  r|j	                         r|j
                  |j                  f}|j                  j                  d      |j                  j                  d      f}t        ||      j                  }||j                  k  rt        ddit        j                        S t        ddit        j                        S t        |j                  t        j                        S t         j#                  d      )	Nr5   latitude	longituderR   zInside the geofencer   zOutside the geofencezGeofencing is not yet started..)EmployeeLocationSerializerr)   rZ   startr9   r\   r]   r1   r   metersradius_in_metersr   r   r*   r=   r<   rI   rJ   )r+   r,   r/   company_locationgeofence_centeremployee_locationdistances          r0   r>   z+GeoFencingEmployeeLocationCheckAPIView.postq   s   /W\\B
44W=!!""$$--$..#
 LL$$Z0LL$$[1%! $O5FGNN/@@@#"$9:6CUCU    67!66  J--f6Q6QRR))*KLLr2   N)r?   r@   rA   r   rB   r&   rZ   r>   rC   r2   r0   rU   rU   _   s    )*11Mr2   rU   c                   B    e Zd ZegZ e edd      d      d        Zy)GeoFencingSetUpPermissionCheckr   Tr   r   r   c                     t        d      S )N   r   r   )r+   r,   s     r0   r1   z"GeoFencingSetUpPermissionCheck.get   s    
 s##r2   N)r?   r@   rA   r   rB   r
   r   r1   rC   r2   r0   rg   rg      s0    )*8$O$	$r2   rg   c                     	 | j                   j                  d      }|dk(  ry t        j                  j                  |      }|S # t        $ r}t
        j                  |      d }~ww xY w)Nselected_companyall)r'   )sessionr1   r   rG   rH   rI   rJ   )r,   rk   r-   rK   s       r0   r&   r&      sf    -"??../ABu$//%%)9%: -))!,,-s    A !A 	A(A##A(c                     t        |       }	 t        j                  j                  |      }|S # t        $ r}t
        j                  |      d }~ww xY wrX   rY   )r,   r-   r.   rK   s       r0   rZ   rZ      sP    '"G-%%))W)= -))!,,-s   !/ 	AAAzgeofencing.add_localbackupc                    	 t        t        |             }| j                  dk(  r	 t        | j                  t        |             }|j	                         rR|j                  d      }t        |       |_        |j                          t        j                  | t        d             nt        j                  | d       t        | dd|i      S #  t               }Y xY w#  t        | j                        }Y xY w)	N)instancePOSTF)commitz'Geofencing config created successfully.z	Not validzgeo_config.htmlform)r   rZ   methodrq   r9   r:   r&   r4   r   success_infor	   )r,   rs   
geofencings      r0   geo_location_configry      s    %",@,IJ ~~	5&';G'DD
 ==?%0J$/$8J!OOWa(Q&RSMM';/',vtn==!%"$	5&w||4Ds   C  C CC8N)*django.contribr   django.contrib.auth.decoratorsr   r   django.httpr   django.shortcutsr   r   r	   django.utils.decoratorsr
   django.utils.translationr   rv   geopy.distancer   rest_frameworkr   rest_framework.paginationr   rest_framework.permissionsr   rest_framework.responser   rest_framework.viewsr   base.modelsr   geofencing.formsr   modelsr   rI   r   rE   rU   rg   r&   rZ   ry   rC   r2   r0   <module>r      s    # N ! @ @ 4 6 # ! : 6 , (  0  OG O@'=g '=T)MW )MX$W $-- 12> 3 >r2   