o
    0=gn                     @   sf  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 d dlmZ ddlmZmZmZmZ dd	lmZmZmZmZ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 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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-d S )!    )APIView)Responsestatus)validate_required_paramsvalidate)validate_and_convert_deadline)generics   )ListingListingCategoryListingCategoryServiceListingTerms)ListingTermsSerializerListingSerializerListingCategorySerializer ListingCategoryServiceSerializerListingRecentSerializerListingSingleAPISerializer)success_responsefailed_responsegenerate_random_text)LimitOffsetPagination)NotFoundErrorValidationError)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 )ListingCategoryCRUDViewNc           	   
   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. - ListingCategoryCRUDView GET request startedpk	completedz* - Listing Category retrieved successfullyz'Listing Category retrieved successfullydatar   Tmanyz, - Listing categories retrieved successfullyz)Listing categories retrieved successfullywarnz: Listing category not foundListing category not foundr   codeerrorz> - Unexpected error occurred during Listing 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listing_category
serializerlisting_categoriese rC   ,/var/www/html/django-vendor/listing/views.pyr4      (   
zListingCategoryCRUDView.getc              
   C   s  t d}td| dtd  d zat|g d td|jddd	g td
|jd
d td|jdddg t|jd}|	 r^|
  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t|tjdW  Y d }~S d }~w 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/ - ListingCategoryCRUDView POST request startedtitledeadlinenoterG    length:max:100rH   rI   length:max:250r)   r'   z( - Listing Category created successfullyz%Listing Category Created Successfullyr(   r,   z5 - Validation errors during Listing Category creationr.   z4 - Validation error during Listing Category creationr0   z= - Unexpected error occurred during Listing Category creationr1   )r   r   r2   r   r   r)   r4   r   r   is_validsaver   errorsr   r   HTTP_400_BAD_REQUESTr   r9   r8   r:   r;   r<   r=   r>   r@   verB   rC   rC   rD   post)   s.   zListingCategoryCRUDView.postc           	   
   C   s  t d}td| dtd  d z{t|g d td|jddd	g td
|jd
d td|jdddg |sMtd| d tdt	j
dW S tjj|d}t||jd}| rx|  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t|t	j
dW  Y d }~S d }~w 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. - ListingCategoryCRUDView PUT request startedrF   rG   rJ   rK   rH   rI   rL   r,   z,: Listing category ID is required for updatezListing category ID is requiredr.   r%   rM   r'   z( - Listing Category updated successfullyz%Listing Category Updated Successfullyr(   z3 - Validation errors during Listing Category updatez': Listing category not found for updater-   z2 - Validation error during Listing Category updater0   z; - Unexpected error occurred during Listing Category updater1   )r   r   r2   r   r   r)   r4   r   r   r   rQ   r   r3   r   rN   rO   r   rP   r6   r7   r   r9   r8   r:   )	r;   r<   r&   r=   r>   r?   r@   rS   rB   rC   rC   rD   put?   s<   zListingCategoryCRUDView.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$   z1 - ListingCategoryCRUDView DELETE request startedr%   r'   z( - Listing Category deleted successfullyz%Listing Category deleted successfullyr   r,   z): Listing category not found for deletionzListing Category not foundr.   r0   z= - Unexpected error occurred during Listing Category deletionr1   )r   r   r2   r   r3   r4   deleter   r6   r   r   r7   r8   r9   r:   )r;   r<   r&   r=   r>   r?   rB   rC   rC   rD   rX   \      zListingCategoryCRUDView.deleteNNN__name__
__module____qualname__r4   rT   rU   rX   rC   rC   rC   rD   r      s
    


r   c                   @   r   )ListingCategoryServiceCRUDViewNc           	   
   C   r    )Nr!   r"   r#   r$   z5 - ListingCategoryServiceCRUDView GET request startedr%   r'   z2 - Listing category service retrieved successfullyz/Listing category service retrieved successfullyr(   Tr*   z3 - Listing category services retrieved successfullyz0Listing category services retrieved successfullyr,   z$: Listing category service not found"Listing category service not foundr.   r0   zF - Unexpected error occurred during Listing Category Service retrievalr1   )r   r   r2   r   r3   r4   r   r   r)   r5   r6   r   r   r7   r8   r9   r:   )	r;   r<   r&   r=   r>   listing_category_servicer@   listing_category_servicesrB   rC   rC   rD   r4   m   rE   z"ListingCategoryServiceCRUDView.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$   z6 - ListingCategoryServiceCRUDView POST request startedrM   r'   z0 - Listing category service created successfullyz-Listing category service created successfullyr(   r,   z= - Validation errors during Listing Category Service creationr.   r0   zE - Unexpected error occurred during Listing Category Service creationr1   )r   r   r2   r   r)   rN   rO   r   rP   r   r   rQ   r8   r9   r:   )r;   r<   r=   r>   r@   rB   rC   rC   rD   rT      s   z#ListingCategoryServiceCRUDView.postc              
   C   sR  t d}td| dtd  d zYt|ddg |s+td| d	 td
tjdW S tjj	|d}t
||jd}| rV|  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$   z5 - ListingCategoryServiceCRUDView PUT request startedlistingvendor_category_servicer,   z4: Listing Category Service ID is required for updatez'Listing Category Service ID is requiredr.   r%   rM   r'   z0 - Listing category service updated successfullyz-Listing category service updated successfullyr(   z; - Validation errors during Listing Category Service updatez/: Listing category service not found for updatera   r0   zC - Unexpected error occurred during Listing Category Service updater1   )r   r   r2   r   r   r   rQ   r   r3   r4   r   r)   rN   rO   r   rP   r6   r7   r8   r9   r:   )r;   r<   r&   r=   r>   rb   r@   rB   rC   rC   rD   rU      s.   z"ListingCategoryServiceCRUDView.putc              
   C   rV   )Nr!   r"   r#   r$   z8 - ListingCategoryServiceCRUDView DELETE request startedr%   r'   z0 - Listing category service deleted successfullyz-Listing Category Service deleted successfullyrW   r,   z1: Listing category service not found for deletionra   r.   r0   zE - Unexpected error occurred during Listing Category Service deletionr1   )r   r   r2   r   r3   r4   rX   r   r6   r   r   r7   r8   r9   r:   )r;   r<   r&   r=   r>   rb   rB   rC   rC   rD   rX      rY   z%ListingCategoryServiceCRUDView.deleterZ   r[   r\   rC   rC   rC   rD   r`   l   s
    


r`   c                   @   r   )ListingTermsCRUDViewNc           	   
   C   r    )Nr!   r"   r#   r$   z+ - ListingTermsCRUDView GET request startedr%   r'   z& - Listing term retrieved successfullyz#Listing term retrieved successfullyr(   Tr*   z' - Listing terms retrieved successfullyz$Listing terms retrieved successfullyr,   z: Listing term not foundzListing term not foundr.   r0   z: - Unexpected error occurred during Listing Term retrievalAn unexpected error occurred: )r   r   r2   r   r3   r4   r   r   r)   r5   r6   r   r   rQ   r8   r9   r:   )	r;   r<   r&   r=   r>   listing_termr@   listing_termsrB   rC   rC   rD   r4      rE   zListingTermsCRUDView.getc              
   C   sP  t d}td| dtd  d zJt|dg td|jdddg t|jd	}| rG|	  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|  tt|tjdW  Y d }~S d }~w 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, - ListingTermsCRUDView POST request startedrI   rJ   rL   rM   r'   z$ - Listing term created successfullyz!Listing Term created successfullyr(   r,   z1 - Validation errors during Listing Term creationr.   : Validation error - r0   z9 - Unexpected error occurred during Listing Term creationrg   )r   r   r2   r   r   r)   r4   r   rN   rO   r   rP   r   r   rQ   r   r9   r8   r:   rR   rC   rC   rD   rT      s*   zListingTermsCRUDView.postc           	   
   C   s  t d}td| dtd  d zet|ddg td|jddd	g |s7td
| d tdtj	dW S t
jj|d}t||jd}| rb|  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|  tt|tj	dW  Y d }~S d }~w 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+ - ListingTermsCRUDView PUT request startedr&   rI   rJ   rL   r,   z(: Listing Term ID is required for updatezListing Term ID is requiredr.   r%   rM   r'   z$ - Listing term updated successfullyz!Listing Term updated successfullyr(   z/ - Validation errors during Listing Term updatez#: Listing Term not found for updateListing Term not foundrj   r0   z7 - Unexpected error occurred during Listing Term updaterg   )r   r   r2   r   r   r)   r4   r   r   rQ   r   r3   r   rN   rO   r   rP   r6   r7   r   r9   r8   r:   )	r;   r<   r&   r=   r>   rh   r@   rS   rB   rC   rC   rD   rU      s8   zListingTermsCRUDView.putc              
   C   rV   )Nr!   r"   r#   r$   z. - ListingTermsCRUDView DELETE request startedr%   r'   z$ - Listing term deleted successfullyz!Listing Term deleted successfullyrW   r,   z%: Listing Term not found for deletionrk   r.   r0   z9 - Unexpected error occurred during Listing Term deletionrg   )r   r   r2   r   r3   r4   rX   r   r6   r   r   r7   r8   r9   r:   )r;   r<   r&   r=   r>   rh   rB   rC   rC   rD   rX      rY   zListingTermsCRUDView.deleterZ   r[   r\   rC   rC   rC   rD   rf      s
    


rf   c                   @   r   )ListingCRUDViewNc           	   
   C   s   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dW  Y d }~S d }~ww )Nr!   r"   r#   r$   z& - ListingCRUDView GET request startedr%   r'   z! - Listing retrieved successfullyzListing retrieved successfullyr(   Tr*   z" - Listings retrieved successfullyzListings retrieved successfullyr,   : Listing not foundzListings not foundr.   r0   z5 - Unexpected error occurred during Listing retrievalrg   rW   )r   r   r2   r   r3   r4   r   r   r)   r5   r6   r   r   r7   r8   r9   )	r;   r<   r&   r=   r>   rd   r@   listingsrB   rC   rC   rD   r4     s(   
zListingCRUDView.getc           	   
   C   s  t d}td| dtd  d zit|g d td|jddd	g t|jd
d}td|jdddg |j }||d
< t	|jd}|
 rf|  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|  tt|tjdW  Y d }~S d }~w ty } ztd| dt| d tddW  Y d }~S d }~ww )Nr!   r"   r#   r$   z' - ListingCRUDView POST request startedrF   rG   rJ   rK   rH   rI   rL   rM   r'   z - Listing created successfullyzListing created successfullyr(   r,   z, - Validation errors during Listing creationr.   rj   r0   z4 - Unexpected error occurred during Listing creationrg   rW   )r   r   r2   r   r   r)   r4   r   copyr   rN   rO   r   rP   r   r   rQ   r   r9   r8   )	r;   r<   r=   r>   rH   r)   r@   rS   rB   rC   rC   rD   rT   #  s2   
zListingCRUDView.postc           	   
   C   s  t d}td| dtd  d zzt|g d td|jddd	g t|jd
d td|jdddg |sLtd| d tdt	j
dW S tjj|d}t||jd}| rw|  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|  tt|t	jdW  Y d }~S d }~w 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& - ListingCRUDView PUT request startedrF   rG   rJ   rK   rH   rI   rL   r,   z: Listing ID is requiredzListing ID is requiredr.   r%   rM   r'   z - Listing updated successfullyzListing updated successfullyr(   z* - Validation errors during Listing updatez: Listing not found for updateListing not foundrj   r0   z2 - Unexpected error occurred during Listing updaterg   )r   r   r2   r   r   r)   r4   r   r   r   r7   r   r3   r   rN   rO   r   rP   rQ   r6   r   r9   r8   r:   )	r;   r<   r&   r=   r>   rd   r@   rS   rB   rC   rC   rD   rU   ;  s<   zListingCRUDView.putc              
   C   s   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yF   td| d t	dd
 Y S  t
yk } ztd| dt| d t	dtjdW  Y d }~S d }~ww )Nr!   r"   r#   r$   z) - ListingCRUDView DELETE request startedr%   r'   z - Listing deleted successfullyzListing deleted successfullyrW   r,   rm   rp   r0   z4 - Unexpected error occurred during Listing deletionrg   r.   )r   r   r2   r   r3   r4   rX   r   r6   r   r8   r9   r   r:   )r;   r<   r&   r=   r>   rd   rB   rC   rC   rD   rX   X  s   zListingCRUDView.deleterZ   r[   r\   rC   rC   rC   rD   rl     s
    


rl   c                   @      e Zd ZdddZdS )ListingCountViewNc              
   C   s   t d}td| dtd  d ztj }td| dtd  d|  td|itjd	W S  t	y\ } z!td
| dt
| d td
dt
| itjd	W  Y d }~S d }~ww )Nr!   r"   r#   r$   z' - ListingCountView GET request startedr'   z0 - Total listings count retrieved successfully: total_listingsr)   r   r0   z; - Unexpected error occurred while retrieving listing countrg   )r   r   r2   r   r3   countr   r   HTTP_200_OKr8   r9   r:   )r;   r<   r=   r>   rs   rB   rC   rC   rD   r4   i  s"   
zListingCountView.getr[   r]   r^   r_   r4   rC   rC   rC   rD   rr   h      rr   c                   @   rq   )ListingRecentViewNc              
   C   sT  t d}td| dtd  d zktjd}td| d t }d|_t|j	
d	|j|_t|j	
d
d|_|||}t|dd}td| d | }tj }	dd |	D }
td| d |j||
|d}td| d ||W S  ty } z!td| dt| d tddt| itjdW  Y d }~S d }~ww )Nr!   r"   r#   r$   z( - ListingRecentView GET request startedz-created_atz:: Listings ordered by creation date retrieved successfully
   limitoffsetr   Tr*   z>: Listings pagination and serialization completed successfullyc                 S   s   g | ]}|j qS rC   )description).0catrC   rC   rD   
<listcomp>  s    z)ListingRecentView.get.<locals>.<listcomp>z1: Categories retrieved and descriptions extracted)rn   total_count
categoriesapplied_countz%: Response data prepared successfullyr0   z/ - Unexpected error occurred during GET requestrg   rt   )r   r   r2   r   r3   order_byr   default_limitintquery_paramsr4   r{   r|   paginate_querysetr   ru   r   r5   r)   get_paginated_responser8   r9   r   r   r:   )r;   r<   r=   r>   rn   	paginatorresult_pager@   r   r   category_descresponse_datarB   rC   rC   rD   r4   }  s>   
zListingRecentView.getr[   rw   rC   rC   rC   rD   ry   |  rx   ry   c                   @   s   e Zd Zej ZeZdS )ListingSingleAPIViewN)	r]   r^   r_   r   r3   r5   querysetr   serializer_classrC   rC   rC   rD   r     s    
r   c                   @   s   e Zd Zdd ZdS )#SearchListingFromNameOrCategoryViewc           	   
   O   s   |j d}|j d}z4tj }|r|j|d}|r"|j|d}| s0tdg itj	dW S t
|dd}td|jitj	dW S  ty] } ztd	t|itjdW  Y d }~S d }~w tyw } ztd	d
itjdW  Y d }~S d }~ww )NrG   category)title__icontains)'listingcategory__description__icontainsrn   r   Tr*   r0   zAn unexpected error occurred.)GETr4   r   r3   r5   filterexistsr   r   rv   r   r)   r   r9   r7   r8   r:   )	r;   r<   argskwargsrG   r   rn   r@   rB   rC   rC   rD   r4     s&   
"z'SearchListingFromNameOrCategoryView.getNrw   rC   rC   rC   rD   r     s    r   N).rest_framework.viewsr   rest_framework.responser   rest_frameworkr   utils.validationr   r   utils.helperr   r	   modelsr   r   r   r   serializersr   r   r   r   r   r   r   r   r   rest_framework.paginationr   utils.exceptionr   r   utils.loggerr   
utils.datar   r2   r   r`   rf   rl   rr   ry   RetrieveAPIViewr   r   rC   rC   rC   rD   <module>   s.     YLU["