o
    g>                     @   sb  d dl mZ d dlmZ d dl mZmZ ddlmZmZm	Z	 d dl
mZ ddlmZmZ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mZ d dlmZ d dl m!Z! e!d Z"G dd deZ#G dd deZ$G dd deZ%G dd dej&Z'G dd deZ(G dd deZ)G dd deZ*d S )!    status)APIView)genericspermissions   )VendorCategoryVendorCategoryServicesVendor)Listing)VendorCategorySerializer VendorCategoryServicesSerializerVendorSerializerVendorListingCountSerializer)SelectedProviderSerializer)success_responsefailed_responsegenerate_random_text)Response)r
   )SelectedProvider)Application)validate_required_paramsvalidate)record)logger_settingsmsgc                   @   4   e Zd Zd
ddZdddZd
ddZd
dd	ZdS )VendorCategoryCRUDViewNc           	   
   C   $  t d}td| dtd  d zB|r4tjj|d}t|}td| dtd  d t|jd	d
W S tj	 }t|dd}td| dtd  d t|jdd
W S  tj
yl   td| d tdtjd Y S  ty } ztd| dt| d tdtjdW  Y d }~S d }~ww )N   info: startedz- - VendorCategoryCRUDView GET request startedpk	completedz) - Vendor Category retrieved successfullyz&Vendor Category retrieved successfullydatar   Tmanyz+ - Vendor Categories retrieved successfullyz(Vendor Categories retrieved successfullywarnz: Vendor Category not foundVendor Category not foundr   codeerrorz= - Unexpected error occurred during Vendor Category retrievalAn unexpected error occurred)r   r   log_msgr   objectsgetr   r   r'   allDoesNotExistr   r   HTTP_404_NOT_FOUND	ExceptionstrHTTP_500_INTERNAL_SERVER_ERROR)	selfrequestr$   formatprocess_codevendor_category
serializervendor_categoriese rA   +/var/www/html/django-vendor/vendor/views.pyr2      (   
zVendorCategoryCRUDView.getc              
   C   s   t d}td| dtd  d z8t|jd}| r5|  td| dtd  d t|jd	d
W S td| d|j d t	|jt
jdW S  tyo } ztd| dt| d t	dt
jdW  Y d }~S d }~ww )Nr   r    r!   r"   z. - VendorCategoryCRUDView POST request startedr'   r%   z' - Vendor Category created successfullyz$Vendor Category created successfullyr&   r*   z4 - Validation errors during Vendor Category creationr,   r.   z< - Unexpected error occurred during Vendor Category creationr/   )r   r   r0   r   r'   is_validsaver   errorsr   r   HTTP_400_BAD_REQUESTr6   r7   r8   r9   r:   r;   r<   r>   r@   rA   rA   rB   post.   s   zVendorCategoryCRUDView.postc              
   C   D  t d}td| dtd  d zR|s$td| d tdtjd	W S tjj|d
}t	||j
d}| rO|  td| dtd  d t|j
ddW S td| d|j d t|jtjd	W S  tjy|   td| d tdtjd	 Y S  ty } ztd| dt| d tdtjd	W  Y d }~S d }~ww )Nr   r    r!   r"   z- - VendorCategoryCRUDView PUT request startedr*   z+: Vendor Category ID is required for updatezVendor Category ID is requiredr,   r#   rD   r%   z' - Vendor Category updated successfullyz$Vendor Category updated successfullyr&   z2 - Validation errors during Vendor Category updatez&: Vendor Category not found for updater+   r.   z: - Unexpected error occurred during Vendor Category updater/   )r   r   r0   r   r   rH   r   r1   r2   r   r'   rE   rF   r   rG   r4   r5   r6   r7   r8   )r9   r:   r$   r;   r<   r=   r>   r@   rA   rA   rB   put=   ,   zVendorCategoryCRUDView.putc              
   C      t d}td| dtd  d ztjj|d}|  td| dtd  d td	d
W S  tjyH   td| d t	dt
jd Y S  tym } ztd| dt| d t	dt
jdW  Y d }~S d }~ww )Nr   r    r!   r"   z0 - VendorCategoryCRUDView DELETE request startedr#   r%   z' - Vendor Category deleted successfullyz$Vendor Category deleted successfullyr   r*   z(: Vendor Category not found for deletionr+   r,   r.   z< - Unexpected error occurred during Vendor Category deletionr/   )r   r   r0   r   r1   r2   deleter   r4   r   r   r5   r6   r7   r8   )r9   r:   r$   r;   r<   r=   r@   rA   rA   rB   rP   S   s   zVendorCategoryCRUDView.deleteNNN__name__
__module____qualname__r2   rJ   rL   rP   rA   rA   rA   rB   r      s
    


r   c                   @   r   )VendorCategoryServicesCRUDViewNc           	   
   C   r   )Nr   r    r!   r"   z5 - VendorCategoryServicesCRUDView GET request startedr#   r%   z1 - Vendor Category Service retrieved successfullyz.Vendor Category Service retrieved successfullyr&   Tr(   z2 - Vendor Category Services retrieved successfullyz/Vendor Category Services retrieved successfullyr*   z#: Vendor Category Service not found!Vendor Category Service not foundr,   r.   z- - Unexpected error occurred during retrievalr/   )r   r   r0   r	   r1   r2   r   r   r'   r3   r4   r   r   r5   r6   r7   r8   )	r9   r:   r$   r;   r<   vendor_servicer>   vendor_servicesr@   rA   rA   rB   r2   d   rC   z"VendorCategoryServicesCRUDView.getc              
   C   s  t d}td| dtd  d zKt|ddg td|jddd	g t|jd
}| rH|	  td| dtd  d t
|jddW S td| d|j d t|jtjdW S  ty } ztd| dt| d tdtjdW  Y d }~S d }~ww )Nr   r    r!   r"   z6 - VendorCategoryServicesCRUDView POST request startedtitler=    zlength:max:50rD   r%   z/ - Vendor Category Service created successfullyz,Vendor Category Service created successfullyr&   r*   z< - Validation errors during Vendor Category Service creationr,   r.   z, - Unexpected error occurred during creationr/   )r   r   r0   r   r   r'   r2   r   rE   rF   r   rG   r   r   rH   r6   r7   r8   rI   rA   rA   rB   rJ   y   s"   z#VendorCategoryServicesCRUDView.postc              
   C   rK   )Nr   r    r!   r"   z5 - VendorCategoryServicesCRUDView PUT request startedr*   z3: Vendor Category Service ID is required for updatez&Vendor Category Service ID is requiredr,   r#   rD   r%   z/ - Vendor Category Service updated successfullyz,Vendor Category Service updated successfullyr&   z: - Validation errors during Vendor Category Service updatez.: Vendor Category Service not found for updaterX   r.   z* - Unexpected error occurred during updater/   )r   r   r0   r   r   rH   r	   r1   r2   r   r'   rE   rF   r   rG   r4   r5   r6   r7   r8   )r9   r:   r$   r;   r<   rY   r>   r@   rA   rA   rB   rL      rM   z"VendorCategoryServicesCRUDView.putc              
   C   rN   )Nr   r    r!   r"   z8 - VendorCategoryServicesCRUDView DELETE request startedr#   r%   z/ - Vendor Category Service deleted successfullyz,Vendor Category Service deleted successfullyrO   r*   z0: Vendor Category Service not found for deletionrX   r,   r.   z, - Unexpected error occurred during deletionr/   )r   r   r0   r	   r1   r2   rP   r   r4   r   r   r5   r6   r7   r8   )r9   r:   r$   r;   r<   rY   r@   rA   rA   rB   rP      s   z%VendorCategoryServicesCRUDView.deleterQ   rR   rS   rA   rA   rA   rB   rW   c   s
    


rW   c                   @      e Zd Zdd ZdS )CreateVendorViewc              
   O   s   t d}td| dtd  d |j}zStjj|d r.td| d td	t	j
d
W S t|jd}| rT|j|d td| dtd  d t|jt	jdW S td| d|j  t|jt	j
dW S  ty } ztd| dt| d tddit	jdW  Y d }~S d }~ww )Nr   r    r!   r"   z( - CreateVendorView POST request starteduserr*   z&: Vendor for this user already exists.z$Vendor for this user already exists.)r   r   rD   r%   z - Vendor created successfullyr   z: Validation errors - r.   z3 - Unexpected error occurred during vendor creationr/   )r   r   r0   r`   r
   r1   filterexistsr   r   rH   r   r'   rE   rF   r   HTTP_201_CREATEDrG   r6   r7   r8   )r9   r:   argskwargsr<   r`   r>   r@   rA   rA   rB   rJ      s0   zCreateVendorView.postN)rT   rU   rV   rJ   rA   rA   rA   rB   r^          r^   c                   @   s*   e Zd Zej ZeZe	j
gZdd ZdS )UpdateVendorViewc                 C   s   t jj| jjdS )Nr_   )r
   r1   r2   r:   r`   )r9   rA   rA   rB   
get_object   s   zUpdateVendorView.get_objectN)rT   rU   rV   r
   r1   r3   querysetr   serializer_classr   IsAuthenticatedpermission_classesrh   rA   rA   rA   rB   rg      s
    
rg   c                   @   r]   )VendorListingCountViewc                 C   sj   z"t jj|d}tjj|d }||d}t|}t|jt	j
dW S  t jy4   tdt	jd Y S w )Nid)user_listing)	vendor_idtotal_applied_listingsr   zVendor not found)r.   r-   )r
   r1   r2   r   ra   countr   r   r'   r   HTTP_200_OKr4   r   r5   )r9   r:   rq   vendorrr   r'   r>   rA   rA   rB   r2      s   zVendorListingCountView.getNrT   rU   rV   r2   rA   rA   rA   rB   rm      rf   rm   c                   @   s   e Zd ZdddZdS )SelectedVendorsByUserNc              
   C   s   z<t jj|d}| stdddtjdW S tjj|d}| s-tdddtjdW S t|dd	}t	d
d|j
dW S  ty] } zt	ddt| dtjdW  Y d }~S d }~ww )N)user_idfailedz#No applications found for the user.)r   r   r   )application_id__inzNo vendors selected by user.Tr(   successzVendors retrieved successfully)r   r   r'   zAn unexpected error occurred: )r   r1   ra   rb   r   r   r5   r   r   r   r'   r6   r7   r8   )r9   r:   rx   r;   applicationsselected_providersr>   r@   rA   rA   rB   r2      s"   
*zSelectedVendorsByUser.getrR   rv   rA   rA   rA   rB   rw      s    rw   c                   @   r]   )SearchVendorFromCategoryViewc                 O   s   |j d}|stdditjdS z	tjj|d}W n tjy-   tdditj	d Y S w tjj
|d}dd	 |D }td
|iS )Ncategory_idr.   z!category_id parameter is requiredr   rn   zCategory not found)r=   c              	   S   s*   g | ]}|j |j|j|j|j|jd qS )ro   contact_detailcompany_registration_document
pan_number
vat_number
ceo_detailr   ).0ru   rA   rA   rB   
<listcomp>  s    	z4SearchVendorFromCategoryView.get.<locals>.<listcomp>vendors)GETr2   r   r   rH   r   r1   r
   r4   r5   ra   r   )r9   r:   rd   re   r   categoryr   vendor_listrA   rA   rB   r2      s   	z SearchVendorFromCategoryView.getNrv   rA   rA   rA   rB   r~      rf   r~   N)+rest_frameworkr   rest_framework.viewsr   r   r   modelsr   r	   r
   listing.modelsr   serializersr   r   r   r   r   utils.helperr   r   r   rest_framework.responser   application.modelsr   r   utils.validationr   r   utils.loggerr   
utils.datar   r0   r   rW   r^   UpdateAPIViewrg   rm   rw   r~   rA   rA   rA   rB   <module>   s0    KN	