o
    WTfa                     @   s   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 ed	d
 Zedd Zedd ZdS )    N)JsonResponse)render)status)csrf_exempt)verify_token)Coupons)CouponSerializerc                 C   s   t | }|d krtdtjddtjdS | jdkrWt| j}t }|d |_	|d |_
t|d |_|d	 |_|d
 |_|d |_|d |_|  tdtjddtjdS tj }t|dd}t|jtjddtjdS )NzUser is not loginmessager   Fsafer   POSTtitlecodecoupon_typevalue	max_valuecountdescriptionzCoupon added successfullyTmany)couponsr   )r   r   r   HTTP_401_UNAUTHORIZEDmethodjsonloadsbodyr   r   r   intr   r   r   r   r   saveHTTP_200_OKobjectsallr   data)requestuser_idrequest_datacoupon
serializer r(   P/media/darth/New Volume E/PROJECTS/DRIVERZZ/DRIVERZZ_ADMIN_APIS/coupons/views.pyr      s$   







r   c                 C   sB  | j dkrYt| j}|d rMtjj|d d}|d |_|d |_t	|d |_
|d |_|d |_|d	 |_|d
 |_|  tdtjddtjdS tdtjddtjdS | jdrztjj| jdd}t|dd}t|jtjddtjdW S  tjy   tdtjddtjd Y S w tdtjddtjdS )Nr   idr*   r   r   r   r   r   r   r   zCoupon edited Successfullyr	   Fr   zCoupon id is requiredr   )r&   r   zCoupon doesn't exists.)r   r   r   r   r   r    getr   r   r   r   r   r   r   r   r   r   r   r   HTTP_400_BAD_REQUESTGETr   r"   DoesNotExist)r#   r%   r&   r'   r(   r(   r)   
editCoupon'   s.   






r0   c                 C   s   t | }|d krtdtjddtjdS zt| j}tjj	|d d
  tdtjddtjdW S  tjyH   tdtjddtjd Y S w )	NzUser not login.r	   Fr   r*   r+   zCoupon deleted Successfullyz(Coupon doesn't exists. Invalid coupon id)r   r   r   r   r   r   r   r   r    r,   deleter   r/   r-   )r#   r$   r%   r(   r(   r)   deleteCouponF   s   r2   )r   django.httpr   django.shortcutsr   rest_frameworkr   django.views.decorators.csrfr   authentication.authr   coupons.modelsr   coupons.serializerr   r   r0   r2   r(   r(   r(   r)   <module>   s    

