o
    E6i                    @   s  d dl mZmZ d dlmZmZ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 d dlmZ d d	lmZ d d
lZd d
lZd d
lZd d
lZd d
lZd dlmZ d dlmZm Z m!Z! ddl"m#Z# 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,m-Z- d dl.m/Z/ d dl0m!Z! d dl1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> ddl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZa d dlbmcZcmdZdmeZe d dlfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZt d dluT d dlvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d d
lZd d
lZd dlmZ d d lmZ d d!lmZ d d"lmZ d d#lmZ d d$lmZmZmZmZ d d%lmZ d d&lmZ d d
l$Z$d d'lmZ d d
lZd dl)m*Z* d dlmZ d d"lmZ d d(lmZ d d)lmZ d d*lmZ d d
lZd d+lmZ d d,lmZ d d
lZd dlm'Z' d d-lumZ d d.lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d d/lmZ d0d1 Zd2d3 Zd4d5 Zd[d7d8Zd9d: Zd;d< Zd=d> Zd?d@ ZdAdB ZdCdD ZdEdF ZdGdH ZdIdJ ZdKdL Ze
e	eg dMdNdO Ze
e	edPdQgdRdS ZdTdU ZdVdW Ze
e	edPdQgdXdY Zee
e	edPdQgdZd[ Zd\d] Zee
e	edPdQgd^d_ Zd`da Zdbdc Ze
e	edPdQgd\dddeZe
e	edPdQgdfdg Ze
e	edPdQgdhdi Ze
e	edPdQgdjdk Zee
e	edPdQgdldm Ze
e	edPdQgdndo Ze
dpdq Ze
e	edPdQgdrds Ze
e	edPdQgdtdu Ze
e	edPdQgdvdw Ze
e	edPdQgd\dxdyZe
e	edPdQgdzd{ Ze
e	edPdQgd|d} Ze
e	edPdQgd~d Ze
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgdd Zdd Ze
e	edPdQgdd Zdd Ze
e	edPdQgdd Ze
dd Ze
dd Zdd Ze
e	edPdQgdd Zd dl+m,Z, dd Zdd Zdd Ze
e	edPdQgdd Ze
e	edPdQgd]ddZe
e	eg dd^ddZe
e	eg ddd Ze
e	edPdQgdd Zdd Ze
e	edgdd Ze	edPdQgdd Zdd Ze
e	edPdQgdd Ze	edPdQgdd Zee
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgdd Ze	edPdQgdd Zee
e	edPdQgdd Z e
e	edPdQgddÄ Ze
e	edPdQgddń Ze	edPdQgddǄ Zee
e	edPdQgddɄ Ze
e	edPdQgdd˄ Ze
e	edPdQgdd̈́ ZG ddτ deZG ddф deZe	eddgddԄ Z	ee
e	edgddք Z
e
e	edgdd؄ Ze
e	edgddڄ Ze
e	eddgd\dd܄Ze
e	edgddބ ZG dd dejZdd Zdd Zdd Zdd Zedd Zedd Zdd Ze
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgdd Ze	eg ddd Zee
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgdd Ze
e	edPdQgd d Z e
e	edPdQgdd Z!e
e	edPdQgdd Z"e
e	edPdQgdd Z#e
e	edPdQgdd	 Z$e
e	edPdQgd
d Z%e
e	edPdQgdd Z&e
e	edPdQgdd Z'e
e	edPdQgdd Z(e
e	edPdQgdd Z)e
e	edPdQgdd Z*e
e	edPdQgdd Z+e
e	edPdQgdd Z,e
dd Z-e
dd Z.e
dd Z/e
e	edPdQgd d! Z0ee
e	edPdQgd"d# Z1e
e	edPdQgd\d$d%Z2e
e	edPdQgd&d' Z3e
e	edPdQgd(d) Z4e
e	edPdQgd*d+ Z5e
e	edPdQgd,d- Z6e
e	edPdQgd.d/ Z7e
e	edPdQgd0d1 Z8e
e	edPdQgd2d3 Z9e
e	edPdQgd4d5 Z:e
e	eddgd\d6d7Z;d8d9 Z<e
e	edgd:d; Z=e
e	edPdQgd<d= Z>ed>d? Z?d@dA Z@dBdC ZAdDdE ZBdFdG ZCdHdI ZDedJgdKdL ZEedJgdMdN ZFe	edPdQgdOdP ZGee
e	edPdQgdQdR ZHe
e	edPdQgdSdT ZIe
e	edPdQgdUdV ZJdWdX ZKdYdZ ZLd
S (_      )renderredirect)authenticateloginlogout)user_passes_testlogin_required)api_view)require_POST)inlineformset_factory)SumAvgDateTimeField)ValidationError)FN)csrf_exempt)datetimedatetimezone   )shop_add_status_change)chain)get_object_or_404)settings)JsonResponseHttpResponse)messages)r   )	Paginator	EmptyPagePageNotAnInteger)apps)FAQAboutUsAppUrl	ContactUsPrivacyPolicyReturnPolicyTermsAndConditions)"AboutUsForm
AppUrlFormContactUsFormFAQFormPrivacyPolicyFormRefundPolicyFormShopFormStoreUserFormTermsAndConditionsFormUserEditFormUserFormUserModalFormBankDetailsFormCategoryFormProductSubCategoryFormProductFormTagFormDynamicFilterFormSpecialListFormDeliveryBoyFormSKUFormCourierDetailsForm
CouponFormDeliverySlotFormDiscountFormAdFormCustomProductFormProductionUnitFormCustomOrderFormMessageFormYouMayAlsoLikeFormDeliverySettingsFormDeliveryDiscountFormCustomCouponSettingsForm)ShopBankDetailsProductionUnit)ProductCategoryProductSubCategoryProductsSalesUnitProductSelectionProductImageProductVideoTagsDynamicFilteringSpecialListSKUCustomProductCustomProductImageCustomProductVideoYouMayAlsoLike)*)CustomOrderTrackingDeliveryBoysOrderProductCustomizationOrderProductsOrdersOrderDeliveryCouponsDeliverySlotDiscountAdsPaymentCommunicationMessageDeliveryDiscountDeliverySettingsCustomCouponSettingsCustomCoupon)	send_mail)EMAIL_HOST_USER)	timedelta)initialize_fcm_app)r   )reverse)Group)CountQr   Prefetch)render_to_string)autocomplete)IsAuthenticated)APIView)Responsestatus)
parse_date)EmailMessage)LoggingOperation)"send_whatsapp_message_delivery_boyemail_sendingsend_notificationsend_whatsapp_messagesmsshop_admin_status_update&shop_admin_status_update_only_one_shop'category_based_shop_admin_status_celery+sub_category_based_shop_admin_status_celerycategory_product_list.product_availability_shop_status_change_celery;master_product_edit_celery_task_to_update_shop_availability,update_sku_long_distance_status_check_celery!long_distance_status_check_celerysku_delete_long_distance_celery!long_distance_master_product_edit)groupc              
   C   sx   zt d| d|  d tj| |tj|dgdd}|W S  ty; } zt d|  d| d	 W Y d }~d S d }~ww )
NzSending Email to (z) with subject ()F   args	countdownzEmail Send Failed for ():  )logginginfor   apply_asyncr   rn   	Exception)subjectmessage
email_listfail_silently
email_datae r   ;/var/www/html/Testing_prj/Navya-Bakers/adminportal/views.py	EmailSendQ   s   &r   c              
   C   sn   zt j| |j|||j|jgdd tdddW S  ty6 } ztd| d|  W Y d }~d S d }~ww )Nr   r   r   znotification send successfullyr|   msgzNotification send Failed on (r   )	r   r   order_IDpkuuidr   r   r   error)	fcm_tokenordertitledescription	user_uuidr   r   r   r   NotificationInitf   s    r   c              
   C   s^   zt d tj| |||gdd W d S  ty. } zt d|  W Y d }~d S d }~ww )Nz#Sending Whatsapp Message to Shipperr   r   )Send Whatsapp message to Shipper Failed: )r   r   r   r   r   r   )numberorderIDnametemplate_namer   r   r   r   WhatsappMessaget   s   
r      c                    s(   t jt j  d fddt| D S )N c                 3   s    | ]}t  V  qd S N)secretschoice).0_
charactersr   r   	<genexpr>   s    z+generate_random_password.<locals>.<genexpr>)stringascii_lettersdigitsjoinrange)lengthr   r   r   generate_random_password   s   r   c                 C   sf   i }| j dkr-| jdd}| jdd}t| ||d}|d ur)t| | tdS ddi}t| d	|S )
NPOSTusernamer   password)r   r   homepager   zinvalid username or passwordz
login.html)methodr   getr   
auth_loginr   r   )requestr   emailr   userr   r   r   r      s   

r   c                    s    fdd}|S )Nc                    s
   | j  v S r   	user_type)r   allowed_rolesr   r   inner_function   s   
z'check_user_role.<locals>.inner_functionr   )r   r   r   r   r   check_user_role   s   r   c                 C   s"   | rt ||  d d}|S d}|S )Nd      r   round)orders_todayfailed_order_todayavg_failed_ordersr   r   r   get_average_failed_orders   s
   r   c                 C      i }| d r=|d r=t | d |d  d d}|dkr,|d }t |d|d< d|d< |S d| }t |d|d< d|d< |S | d rT|d sTd}t |d|d< d|d< |S d	}t |d|d< d|d< |S )
Nweek_revenuelast_week_revenuer   r   revenue_differenceincreaseflagdecreaser   r   )r   week_before_last_week_revenuerevenue_difference_outputweek_revenue_percent_with_weekr   r   r   get_week_difference_output   s.   r   c                 C   r   )
Ntoday_revenuelast_week_avg_revenuer   r   today_revenue_percent_with_weekr   r   r   r   r   )r   last_week_revenue_avgrevenue_percent_datar   r   r   r   get_revenue_percent_data   s6   
r   c                 C   s   i }| r5|r5t | | dd }|dkr$|d }t |d|d< d|d< |S d| }t |d|d< d|d< |S | rH|sHd}t |d|d< d|d< |S d}t |d|d< d|d< |S )Nr   r   today_count_percent_with_weekr   r   r   r   r   )r   last_week_revenue_countrevenue_count_datar   r   r   r   get_revenue_count_data   s*   
r   c                 C   sR   t jj| j| j|jdjddgdjtdd}t	|d r!|d ndd	|d< |S )
N)created_date__yearcreated_date__month
store_uuid	New OrderFailedorder_status__ingrand_totalmonth_revenuer   r   r   )
r`   objectsfilteryearmonthr   exclude	aggregater   r   )todayshop_idr   r   r   r   get_month_revenue   s   
r  c                 C   sl  d }t jdj| jjd}|r|d }d| jd< tjjddgdj||jd		 }t
tjj|d
|jdd}t|}tjj|d|jd	 }t||}tjj|d|jd	 }	tjj|g d|jd	 }
tjj|jdjdgd	 }tjj||jd	jddgdjtdd}d}|d rt|d r|d ndd}|tdd }|tdd }tjj|||jdjddgdjtdd}tjj|||jdjddgdjtdd}t||}tjj|j||djddgdjtdd}t||}tjj|j||djddgd	 }t||}t||}d}|	r'|r't|	| d d}tjj|j|djddgdjtdd}d}|d rJt|d d}tjj|j|djddgddjtddd d d! }|D ]}|d" r{t|d" d|d"< qki d#|d$|d%|	d&|
d'|d(|d)|d|d*|d+|d,|d-|d.|d/|d0|d1d2d3|}|S d S )4Nunit_admin_userunit_admin_user__uuidr   Yesproduct_viewr   r   r   )created_date__dater   	Confirmed)r  order_statusr   r   	Deliveredr  ViewedOrder Packed)r  r   r   )r   r   r   r   r      days)created_date__date__gtecreated_date__date__lter   r   r   )r   r  r  r   r   )r   r  store_uuid__unit_nametotal_amount-total_amountr   r  r   new_order_todayfulfiled_ordercurrently_unassigned_orderfailed_orderavg_failed_ordertotal_ordersr   grand_total_by_storer   r   delivery_success_percentageavg_today_revenuer   home_activeactivenew_order_data)rJ   r   prefetch_relatedr   r   r   sessionr`   r  countlistvalueslenr   r  r   r   ro   r   r   r   r   r  annotateorder_by)r   r  shop_id_verifyr  r   r,  r!  r   r   r"  r#  r&  r   today_revenue_datalast_week_startweek_before_last_weekr   r   r   r   r   r   r   r   r(  r)  avg_today_revenue_dataresultsicontextr   r   r   shop_admin_homepage  s0  











	
r=  c                 C   sF  d| j d< tjjddgdj|d }ttjj|ddd	}t|}tjj|dd }t	||}tjj|d
d }tjjg d|d }|t
dd }	|	t
dd }
tjj|	|djddgdjtdd}tjj|
|	djddgdjtdd}i }|d r|d rt|d |d  d d}|dkr|d }t|d|d< d|d< n3d| }t|d|d< d|d< n#|d r|d sd}t|d|d< d|d< nd}t|d|d< d|d< tjj|	|djddgdjtdd}tjj|djddgdjtdd}d}|d rt|d d}i }|d rQ|d rQt|d |d  d d}|dkrA|d }t|d|d < d|d< n5d| }t|d|d < d|d< n%|d ri|d sid}t|d|d < d|d< nd}t|d|d < d|d< tjj|	|djddgd }i }|r|rt|| d d}|dkr|d }t|d|d!< d|d< n1d| }t|d|d!< d|d< n!|r|sd}t|d|d!< d|d< nd}t|d|d!< d|d< d}|r|rt|| d d}tjj|djddgdjtdd}d}|d rt|d d}tjjdgd }tjj|j|jd
d"jtdd#}t|d$ r4|d$ ndd|d$< tjj|d%d&jddgdd'jtdd(d)d d* }|D ]}|d+ rjt|d+ d|d+< qZi d,|d-|d.|d/|d0|d1|d2|d|d$|d3|d!|d |d4|d5|d6|d7d8d9|}|S ):NNor  r   r   r   r  r  )r  r  r   r  r  )r   r  r  r  )r  r  r   r  r  r   r   r   r   r   r   r   r   r   r  r  r   r   r   r   )r   r   r  r   r   Local Ordersr  
order_typer  r  r   r   r  r   r!  r"  r#  r$  r   r&  r'  r(  r)  r   r*  r+  r,  r.  r`   r   r  r   r/  r0  r1  r2  r   ro   r  r   r   r   r  r  r3  r4  )r   r  r   r,  r!  r   r   r"  r#  r7  r8  r   r   r   r   r   r   r6  r   r   r   r   r   r(  r)  r9  r&  r   r:  r;  r<  r   r   r   nbc_admin_homepagez  sl  
















	

rD  c                 C   sf  d| j d< tjjddgdjd|d }ttjj|ddd	d
}t|}tjj|ddd	 }tjj|ddd	 }t	||}tjjg d|dd }|t
dd }	|	t
dd }
tjjd|	|djddgdjtdd}tjjd|
|	djddgdjtdd}i }|d r|d rt|d |d  d d}|dkr|d }t|d|d< d|d< n3d| }t|d|d< d|d< n#|d r|d sd}t|d|d< d|d< nd}t|d|d< d|d< tjjd|	|djddgdjtdd}tjjd|djddgdjtdd}d}|d rt|d d}i }|d rZ|d  rZt|d |d   d d}|dkrJ|d }t|d|d!< d|d< n5d| }t|d|d!< d|d< n%|d rr|d  srd}t|d|d!< d|d< nd}t|d|d!< d|d< tjjd|	|dd" }i }|r|rt|| d d}|dkr|d }t|d|d#< d|d< n1d| }t|d|d#< d|d< n!|r|sd}t|d|d#< d|d< nd}t|d|d#< d|d< d}|r|rt|| d d}tjjd|djddgdjtdd}d}|d rt|d d}tjjdd$jdgd }tjjd|j|jd%jddgdjtdd&}t|d' rD|d' ndd|d'< tjj|dd(jddgdd)jtdd*d+d d, }|D ]}|d- rzt|d- d|d-< qji d.|d/|d0|d1|d2|d3|d4|d|d'|d5|d#|d!|d6|d7|d8|d9d:d;|}|S )<Nr>  r  r   r   r   Long Distance Orders)rB  r  r  )r  r  rB  r   r  r  )r   r  rB  r  r  )rB  r  r  r   r  r  r   r   r   r   r   r   r   r   r   r  r  r   r   r   )rB  r  r  r  r   rB  )rB  r   r   r   r   rA  pu_uuid__pu_namer  r   r   r  r   r!  r"  r#  r$  r   r&  grand_total_by_pur(  r)  r   r*  r+  r,  rC  )r   r  r   r,  r!  r"  r   r   r#  r7  r8  r   r   r   r   r   r   r6  r   r   r   r   r   r(  r)  r9  r&  r   r:  r;  r<  r   r   r   pu_admin_home_page
  s  



















		

rI  )Super Admin	NBC Admin
Shop AdminPU Adminc                 C   sn   i }t   }| jjdkrt| |}n| jjdks | jjdkr&t| |}n| jjdkr1t| |}t| d|S )NrL  rK  rJ  rM  z
index.html)	r   nowr   r   r   r=  rD  rI  r   )r   r<  r  r   r   r   	home_page  s   
rO  rJ  rK  c              	   C      t jd}t|d}| jd}z||}W n ttfy(   |d}Y n t	y6   ||j
}Y nw |dd}t| d|S )N-created_at
   pager   r+  )	shop_datasales_unit_activezshop_list.html)rJ   r   r4  r   GETr   get_pager   	TypeErrorr   	num_pagesr   )r   shops	paginatorrS  r<  r   r   r   	shop_list  s   

r\  c              	   C   s   g }t j D ]F}tjj|d }|r|j}n|j}|jdkr,|t| |||d q|jdkr=|t| |||d q|jdkrM|t| |||d q|S )NskuVisible)
sales_unitr^  r|   shop_admin_statusDisabledzOut of Stock)rV   r   r   rP   firstr|   
sku_statusappend)shopproduct_enter_listr;  sales_statusr|   r   r   r   shop_status_initialization  s(   


ri  c                 C   s   t | j }t |j }|| }g d}g d}g d}t|t|@ }t|t|@ }	t|t|@ }
d}|r>d}|S |	rDd}|S |
rHd}|S )N)		unit_name	unit_codeunit_locationr   
contact_nor|   r  delivery_modedelivery_radius)latitude	longitudestreetcitystate_or_provincedistrictpin_code)account_name	bank_namebranch_name	ifsc_codeaccount_numbergstr   r   r   )r0  errorskeysset)	shop_form	bank_formshop_error_columnsbank_error_columns
error_liststep1_columnsstep2_columnsstep3_columnsstep1step2step3rS  r   r   r   shop_add_error_page_find  s&   r  c                 C   sT  g }t  }| jdkrt| j}t| j}| rw| rw| }|jdd}||_|  t|}t	j
| d}d}	|j D ]}
t||	t|
jgdd q@z | j}d}d	}	d
|j d|j }tj
j|||	|d W tdS    Y tdS ||j t||}||||dd|d}t| d|S t }t }g }||||dddd}t| d|S )Nr   FcommitzShop createdz,New shop assigned you as the unit admin userTr   rf  zShop CreatedShop z created with code r   log_typer   r   r\  
modal_hider+  )r}  r  r  user_modal_form
show_modalrU  rS  shop_add.htmlr   )r3   r   r.   r   r4   is_validsaverf  ri  rP   r   bulk_creater  allrm   rn   r   r   rj  rk  r   creater   re  r}  r  r   )r   r}  r  r  r  rf  bank_detailsrg  r   r   r   log_userr  r   rS  r<  r   r   r   shop_add  sj   



		r  c           	   
   C   s  t jj|d}|r~tjj|d  | }|  zFz
tjd|jd W n t	y? } zt
d|  W Y d }~nd }~ww | j}d}d}d|j d	|j d
}tjj||||d W tdS  t	y} } zt
d|  W Y d }~tdS d }~ww tdS )Nr   )r`  zShop Delete)senderr  z.Signal Status Updation on Shop Delete Failed: rf  zShop Deletedr    with code  deletedr  zShop Deletion Error: r\  )rJ   r   r   rP   deleterc  r   sendr   r   r   r   r   rj  rk  r   r  r   )	r   r  rf  rT  r   r  r  r   r   r   r   r   shop_delete>  s2   
r  c                 C   s(   zt jj| |||d W d S    Y d S )Nr  )r   r   r  )r  r  r   r   r   r   r   log]  s   r  c                 C   s   t t|d}tjj|d}|rt| d tdS |rB|  | j}d}d}d|j	 d|j
 d}t|||| t| d	 tdS t| d
 tdS )Nr   categoryz$Couldn't delete subcategories existsproductzCategory Deletedz	Cagegory r  r  zCategory deleted successfullyzSomething went wronglist_category)r   rM   rN   r   r   r   warningr  r   category_namecategory_coder  successr   )r   category_idr  sub_categoryr  r  r   r   r   r   r   category_deletef  s    r  c                 C   s   t jj|d}|jj}|r:|  z$| j}d}d}d|j d|j d|jj	 d|jj
 }t|||| W n   Y tdt| d	S )
Nr  r  zSub category deletedSub category r   deleted for category  with code /adminportal/add-sub-category//)rN   r   r   r  r   r  r   sub_category_namesub_category_coder  r  r  r   str)r   sub_category_idr  r  r  r  r   r   r   r   r   sub_category_delete  s   (r  c                 C   sP   | |j kr$|j D ]}d}d|j d|j  }t||t|jgdd q
d S d S )NzShop status updatedz
Your shop z changed their status to Tr  )r|   r  r  rj  rm   rn   r   )existing_shop_statusrf  r   r   r   r   r   r   send_email_on_status_change  s   
r  c              
   C   s  t t|d}|j}tjj|d }g }| jdkrt| j	|d}t
| j	|d}| r| r| }|jdd}||_|  t|| z)| j}d}	d}
d	|j d
|j d d	|j d
|j d}t||	|
| W tdS  ty } ztd|  W Y d }~tdS d }~ww ||j ||j t||}||d|d}t| d|S t|d}t
|d}||ddd}t| d|S )Nr  rf  r   instanceFr  rf  zShop Updatedr  r  r   updatedz&Shop Updation Error(logging section): r\  r+  )r  r  rU  rS  zshop_edit.htmlr   )r   rJ   r|   rK   r   r   rc  r   r.   r   r4   r  r  rf  r  r   rj  rk  r  r   r   r   r   re  r}  r  r   )r   r  rf  r  r  r}  r  r  r  r  r   r   r   rS  r<  r   r   r   	shop_edit  sH   





r  c              	   C   s   t jjddd}t|d}| jd}z||}W n tt	fy,   |d}Y n t
y:   ||j}Y nw |dd}t| d	|S )
NCustomer)user_type__exact-created_daterR  rS  r   r+  )usersusers_activez
users.html)Usersr   r  r4  r   rV  r   rW  r   rX  r   rY  r   )r   r  r[  rS  r<  r   r   r   r    s   

r  c                 C   sr   g }t t|d}| jdkr't| j|d}| r |  tdS ||j	 nt|d}||d|d}t
| d|S )Nr  r   r  r  r+  r}  formpublic_users_activeuser_idedit_users.html)r   r  r   r1   r   r  r  r   re  r}  r   )r   r  r}  r   r  r<  r   r   r   	user_edit  s   

r  c                 C   s  g }| j dkr}t| j}| rv|jd }|jd }|jd }|jd }|jd }|jd }t||||||d}	t }
|	|
 d	}d
|
 }t|	j	}zt
||t|gdd |	  tdW S    ddgig}||d}t| d| Y S ||j nt }g }||d}t| d|S )Nr   
first_name	last_namer   r   phone_numberr|   )r  r  r   r   r  r|   #Orderpicky Registration Successful NCongratulations, your account has been successfully created. Your password is Fr  r  zPlease enter valid email)r}  	user_formzadd_user.html)r   r2   r   r  cleaned_datar  r   set_passwordr  r   rm   rn   r  r   r   re  r}  )r   r}  r  r  r  r   r   r  r|   user_objmy_passwordr   r   	recepientr<  r   r   r   user_add  sT   












r  c                 C   "   t jj|d}|r|  tdS )Nr  r  )r  r   r   r  r   )r   r  r   r   r   r   user_delete6  s   r  c           
      C   s   g }t  }| jdkrIt| j}| rB| }t }||_d}d| }t|j	}t
||t|gdd t }d||d}	t| d|	S ||j nt }g }|||d	d
}	t| d|	S )Nr   r  r  Tr  r  )r  r  r  r  
modal_show)r  r  r}  r  )r.   r   r3   r   r  r  r   r   r  r   rm   rn   r   re  r}  )
r   r}  r  r  r  r  r   r   r  r<  r   r   r   user_add_modal@  s:   



r  c                 C   s   t |  tdS )Nz/adminportal/login/)r   r   r   r   r   r   admin_logoutp  s   r  c           
      C   s   g }t t|d}t }|| |  d}d| }t|j}t||t|gdd t	
| t	jd t|d}||d|d	}	t| d
|	S )Nr  z%Navya Bakers Registration Successful r  Fr  zGPassword Reset Successfully. You will receive a mail with the password.r  r+  r  r  )r   r  r   r  r  r  r   rm   rn   r   add_messageSUCCESSr1   r   )
r   r  r}  r   r  r   r   	recipientr  r<  r   r   r   reset_passwordv  s   




r  c              	   C      t jd}t|d}| jd}z||}W n ttfy(   |d}Y n t	y6   ||j
}Y nw |dd}t| d|S )Nr  rR  rS  r   r+  )category_listcategory_activezcategory_list.html)rM   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   category_datar[  rS  r  r<  r   r   r   r    s   

r  c                 C   s6  g }t t|d}tjj|d}t }| jdkrt| j| j	|d}|
 rp| jddr/d |j_| jddr:d |j_| jddrEd |j_| }z| j}d	}d
}	d|j d|j d}
t|||	|
 W tdS    Y tdS ||j |d|j|dt|d}t| d|S t|d}|d|j|d}t| d|S )Nr  r  r   r  image1r   image2image3r  zCategory updated	Category r  r  r  T)category_formsubcategory_statusr  sub_category_formsubcategory_addr  category_add.htmlr  r  r  r  )r   rM   rN   r   r   r6   r   r5   r   FILESr  r   r  iconstandard_imagebanner_imager  r   r  r  r  r   re  r}  r   r0  r   )r   r  r}  r  sub_category_listr  r  r  r  r   r   r<  r   r   r   category_edit  sF   

r  c                 C   s   g }| j dkret| j| j}t }| rS| }z| j}d}d}d|j d|j	 d}	t
||||	 W n   Y tt|jd}
t|
d}|d	|j|d
}t| d|S ||j ||dd}t| d|S t }g }||ddd}t| d|S )Nr   r  zCategory createdr  r  z createdr  r  Tr  r  r   )r}  r  r  F)r}  r  r  r  )r   r5   r   r  r6   r  r  r   r  r  r  r   rM   r   r   re  r}  )r   r  r}  r  r  r  r  r  r   r   category_instancer<  r   r   r   add_category  sD   

r  c                 C   sb  g }t t|d}| jdkrt| j}tjj|d}t|d}|	 rl|j
dd}||_|
  z$| j}d}	d}
d	|j d
|j d|jj d|jj }t||	|
| W n   Y t }|d||dt|d}t| d|S ||j |r||d||dt|d}n||d||d}t| d|S t|d}t }tjj|d}g }|d||dt|d}t| d|S )Nr  r   r  r  Fr  r  Sub category createdr  r   created for category r  T)r  r  r  r  r  r  r  )r}  r  r  r  r  r  r  )r}  r  r  r  r  )r   rM   r   r6   r   rN   r   r   r5   r  r  r  r   r  r  r  r  r  r0  r   re  r}  )r   r  r}  r  r  r  r  r  r  r  r   r   r<  r   r   r   add_sub_category  sX   


(	

r  c                 C   s   t t|d}|jj}g }| jdkr\t| j| j|d}| rU|	  z$| j
}d}d}d|j d|j d|jj d	|jj }	t||||	 W n   Y td
t| dS ||j nt|d}||d||d}
t| d|
S )Nr  r   r  r  zSub category updatedr  r   updated for category r  r  r  r+  )r}  r  r  r  r  zedit_sub_category.html)r   rN   r  r   r   r6   r   r  r  r  r   r  r  r  r  r  r   r  re  r}  r   )r   r  r  r  r}  r  r  r  r   r   r<  r   r   r   edit_sub_category_rowI  s,   
(
r  c                 C   s|  g }d}d}d}| j dkr| jd}|r%tt|d}t| j| j|d}nt| j| j}| r| }| j	d}|D ]
}t
jj||d	 q<| j	d
}	|	D ]
}
tjj||
d qOd}d}z/| j}d}d}d|j d|j d|jj d|jj d|jj d|jj d}t|||| W n   Y td||d|jidS ||j td|jdS t }||d|||d}t| d|S )a)  
    In the products listing on side bar. we have master product listing section.
    On clicking the master product, master product listing will open.
    On the listing page we have a add button.
    We can add an new master product by clicking this button.
    :param request:
    :return:
    FTNr   
product_idr  r  imagesr  imagevideosr  videor  zMaster Product AddedProduct r  z added for category  () and sub category ) idr  
sku_activedetails_activer  r  r}  r+  )r}  product_formr  r  r  r  zproduct_add.html)r   r   r   r   rO   r7   r  r  r  getlistrQ   r   r  rR   r   	item_name	item_codeitem_categoryr  r  item_sub_categoryr  r  r  r   r  re  r}  r   )r   r}  r  r  r  r   r  r  r  r  r  r  r  r   r   r<  r   r   r   master_product_addj  s\   
>		r  c              	   C   s  t t|d}| jdkrt| j}| r|jdd}|jdkr'|jdkr'd|_	||_
| }tj D ]d}tjj|||j|jdd	\}}|jdkrg|j
j| |j| |j
jj| |j
jj| |j	rtjj|j
jd
jdd tjj|j
jjdjdd tjj|j
jjdjdd |  q3z%| j}	d}
d}d|j d|j d|j
j d|j
j d	}t|	|
|| W n   Y t d|j|j|j!|j"|j#|jdS t d|j$dS t dddS )a5  
    Sku is different sub category of product.
    for eg: if we consider a biscuit. Same biscuit have 50g, 60g packet etc......
    We can add sku from submitting a modal from from master product create and edit form
    in master product listing

    :param request:
    :param product_id:
    :return:
    r  r   Fr     r_  Tra  r|   r`  r^  defaultsr  long_distance_availabilityr  r  z	SKU AddedSKU r  z added for product (r
  r  )r|   sku_namesku_codesku_quantitysku_unitsku_mrpsku_idr   r|   r}  Invalid request methodr|   r   )%r   rO   r   r<   r   r  r  sku_expiry_durationrd  r  r  rJ   r   r  rP   get_or_creater`  addr  r  r   r  updaterM   r   rN   r   r   r!  r  r  r  r   r"  r#  r$  r}  )r   r   r  r  r^  sku_datar;  sales_productr   r  r  r   r   r   r   r   add_sku  s^   





*
r/  c                 C   s0   t jj|dd}dd |D }td|dS )z
    Used to list the skus in master product add and edit form.
    Master product add : product_add.html,
    Master product edit : product_edit_01.html
    :param request:
    :param product_id:
    :return:
    r   
created_atc              	   S   s*   g | ]}|j |j|j|j|j|jd qS ))r%  r   r!  r"  r#  r$  )r  r   r!  r"  r#  r$  )r   r^  r   r   r   
<listcomp>   s    
zget_skus.<locals>.<listcomp>r  )r|   skus)rV   r   r   r4  r   )r   r   r3  sku_listr   r   r   get_skus  s
   	r5  c           
   	   C   s   | j dkrg| jd}tt|d}|jj}|jjj}|jj	j}|
  tj|||gdd tj|||gdd z%| j}d}d}d|j d	|j d
|jj d|jj d	}	t||||	 W n   Y tddiS tdddS )z
    Delete Sku function used to delete an sku from product edit and product add section.
    product edit : product_edit_01.html,
    product add : product_add.html
    :param request:
    :return:
    r   r%  r  r   r   r  zSKU Deletedr  r  z deleted for product r  r
  r|   r  r   zInvalid requestr(  )r   r   r   r   rV   r  r  r  r   r  r  r   r   r   r   r   r!  r  r  r  r   )
r   r%  r^  sku_product_idsku_category_idsku_sub_category_idr  r  r   r   r   r   r   
delete_sku,  s&   



*r9  c                 C   sp   z&t jj|d}d|j|j|j|j|j|j|j	|j
|j|jd
d}t|W S  t jy7   tddd Y S w )z3
    Used to get a sku detail with the sku id.
    r  r  )
r   r!  r"  r#  r$  r)  sku_bulk_qty_limitsame_day_deliverycustomization_availablerd  )r|   r^  r   zSKU not found.r(  )rV   r   r   r   r!  r"  r#  r$  r)  r:  r;  r<  rd  r   DoesNotExist)r   r%  r^  datar   r   r   get_skuX  s&   
r?  c                 C   sT  t j D ]}tjj|| | j| jdd\}}| j|_| j|_|  t	j
| jgdd | jdkrt j D ]X}tjj| |d }|rw|jdkrg| jj| | j| | jjj| | jjj| q6| j| tj
| jgdd q6t j D ]}| j| q|tj
| jgdd q6qt j D ]}| j| qtj
| jgdd qdS )aQ  
    On sku change we need to update the status to every shop the sku is assigned
    to.
    Based on the sku and shop admin status we are checking the availability of product.
    If shop_admin status is not visible we check for long distance availability based on
    sku status.
    :param sku:
    :param sku_data:
    :return:
    r  r  r   r   r_  r^  r`  N)rJ   r   r  rP   r*  rd  ra  r|   r  r   r   r  r   rc  r  r`  r+  r  r  remover   )r-  r;  r.  r   rT  sales_unit_status_checkrf  r   r   r   sku_update_status_changesw  s@   


rC  c           
   
   C   s   t t|d}| jdkrmt| j|d}| re| }t| z%| j}d}d}d|j	 d|j
 d|jj d	|jj d
	}t|||| W n ty] }	 ztd|	  W Y d}	~	nd}	~	ww tdddS td|jdS tdddS )z
    Update an sku listed in the master product listing.
    update the sku in edit or add sku page.
    product edit : product_edit_01.html,
    product add : product_add.html

    :param request:
    :param sku_id:
    :return:
    r  r   r  r  zSKU Updatedr  r  z updated for product r  r
  zSKU Updated Error (logging): Nr  zSKU updated successfully.r(  r   r&  r'  )r   rV   r   r<   r   r  r  rC  r   r   r!  r  r  r  r  r   r   r   r   r}  )
r   r%  r^  r  r-  r  r  r   r   r   r   r   r   
update_sku  s(   
*rD  c                 C   s   t t|d}| jdkr[| jd}|durT||_|  t| z(| j}d}d}d|j	 d|j
 d	| d
|jj d|jj d}t|||| W n   Y tdddS tdddS tdddS )z
    Sku status update from product listing.
    When we click on the drop down on product listing, sku will list.
    On the sku list we have the option to change the sku status.

    :param request:
    :param sku_id:
    :return:
    r  r   rd  Nr  zSKU Status Updatedr  r  z updated status to  for product r  r
  r  z SKU status updated successfully.r(  r   z"No status provided in the request.z-Invalid request method. Only POST is allowed.)r   rV   r   r   r   rd  r  rC  r   r   r!  r  r  r  r  r   )r   r%  r^  r|   r  r  r   r   r   r   r   update_sku_from_product_list  s$   

0rF  c                 C   s  t t|d}g }d}d}|j}|j}| jd}| jd}	| jdkrt| j| j|d}
|
	 r|

 }| jd}|D ]
}tjj||d	 q<| jd
}|D ]
}tjj||d qOd}d}tj|j|j|jgd tj|j|j|	|gdd z/| j}d}d}d|j d|j d|jj d|jj d|jj d|jj d}t|||| W n   Y td||d|jidS ||
j td|
jdS t|d}
||
d||||d}t | d|S )a  
        Editing the product from the product listing page.
        In the side bar we have master product section
        On opening the master product we have the option to edit
        the product.
    :param request:
    :param product_id:
    :return:
    r  FTr  r  r   r  r  r  r  r  r   r   r   r  zMaster Product Updatedr  r  r  r  r	  r
  r  r  r  r+  )r}  r  r  r  r  r  r   zproduct_edit_01.html)!r   rO   r  r  r   r   r   r7   r  r  r  r  rQ   r   r  rR   r   r   r   r  r   r   r  r  r  r  r  r  r  r   re  r}  r   )r   r   r  r}  r  r  existing_categoryexisting_sub_categoryto_categoryto_sub_categoryr  r  r  r  r  r  r  r   r   r<  r   r   r   master_product_edit  sb   
>	

rL  c                 C      t t|d}|jj}|  z5| j}d}d}d|jj d|jj d|jjj	 d|jjj
 d|jjj d|jjj d	}t|||| W n   Y td
|dS )zV
    deleting product image
    :param request:
    :param image_id:
    :return:
    r  r  zMaster Product Image deletedr  r  z  deleted its image for category r  r	  r
  edit_master_productr0  )r   rQ   r  r  r  r   r  r  r  r  r  r  r  r  r  r   )r   image_idr  r   r  r  r   r   r   r   r   delete_image_  s   JrP  c                 C   rM  )zZ
    deleting the product video
    :param request:
    :param video_id:
    :return:
    r  r  zMaster Product Video deletedr  r  z  deleted its video for category r  r	  r
  rN  r0  )r   rR   r  r  r  r   r  r  r  r  r  r  r  r  r  r   )r   video_idr  r   r  r  r   r   r   r   r   delete_videov  s   JrR  c           	      C   s  | st jj|jdjdd n1d}| D ]}tjj|dd }|r1t jj|jdjdd d} nq|s@t jj|jdjdd tjj|ddd}|sYtjj|jdjdd d	S d}|D ]}tjj|dd }|r{tjj|jdjdd d} nq]|stjj|jdjdd d	S )
z
    long distance product availability check and update the availability
    :param long_dist_sku_sub_category:
    :param product_category:
    :param product_sub_category:
    :return:
    r  Fr  r_  r^  r|   Tr  product__item_categoryrd  sku_expiry_duration__gtN)	rN   r   r   r   r,  rP   rc  rV   rM   )	long_dist_sku_sub_categoryproduct_categoryproduct_sub_categorylong_dist_sub_category_flagr;  long_dist_sales_check_sublong_dist_sku_categorylong_dist_sku_category_flaglong_dist_sales_check_catr   r   r   #long_distance_master_product_delete  sR   r_  c                 C   s4  t jj|d}| }|j}|j}|rtjj|d}|r(t| tj	d tdS |
  tjj|ddd}t||| tj|j|jgdd z2| j}d	}	d
}
d|j d|j d|jj d|jj d|jj d|jj d}t||	|
| W tdS  ty } ztd|  W Y d}~tdS d}~ww tdS )z]
    deleting the master product
    :param request:
    :param product_id:
    :return:
    r  )Productz1Your Product used in ads! couldn't delete productr_  r  product__item_sub_categoryrd  rV  r   r   r  zMaster Product deletedr  r  r  r  r	  r
  z&Master Product Delete Error(logging): Nlist_master_product)rO   r   r   rc  r  r  re   r   r  WARNINGr  rV   r_  r   r   r   r   r  r  r  r  r  r  r  r   r   r   r   )r   r   r  product_datarX  rY  adsrW  r  r  r   r   r   r   r   r   master_product_delete  s>   	
 >
rg  )r   c           !   
   C   s  | j d}| j d}| j d}| j d}tjj|dd}|r,|j||d}n|j|d}g }| D ]}|j|j|j|j	j
|jjg d	}g }	|j D ]}
|
j|
j|
j|
j|
j|
j|
jd
}ztjj||
dddd\}}|jdkr|
j|j |
jj|j |
jj	j|j |
jjj|j ntj|
j|jjgdd |jdkr|
j dkrd|
_!|
"  tjj|
jjdj#dd t$jj|
jj	jdj#dd t%jj|
jjjdj#dd nt&jj|
jdj#dd t&jj|
jddd}|stjj|
jjdj#dd n6d}|D ]!}tjj|dd' }|r1tjj|
jjdj#dd d} nq|sCtjj|
jjdj#dd t&jj|
jjddd}|s`t%jj|
jjjdj#dd n8d}|D ]"}tjj|dd' }|rt%jj|
jjjdj#dd d} nqd|st%jj|
jjjdj#dd t&jj|
jj	ddd}|st$jj|
jj	jdj#dd nAd}|D ]"}tjj|dd' }|rt$jj|
jj	jdj#dd d} nq|st$jj|
jj	jdj#dd nt&jj|
jdj#dd t&jj|
jddd}|stjj|
jjdj#dd n6d}|D ]!}tjj|dd' }|r;tjj|
jjdj#dd d} nq|sMtjj|
jjdj#dd t&jj|
jjddd}|sjt%jj|
jjjdj#dd n8d}|D ]"}tjj|dd' }|rt%jj|
jjjdj#dd d} nqn|st%jj|
jjjdj#dd t&jj|
jj	ddd}|st$jj|
jj	jdj#dd n8d}|D ]"}tjj|dd' }|rt$jj|
jj	jdj#dd d} nq|st$jj|
jj	jdj#dd |j|d< |j|d< |	(| W qR   d|d< d|d< |	(| Y qR|	|d< |(| q8t)|d}|*d}t+d d!|i}| j,j-d"krDt+d#d!|i} nt+d$d!|i} t.| |d%dd&S )'a  
    this is a function to filter and list the products based on sub category
    we have this drop down menu having the list of sub categories
    if we select one sub category based on that sub category product will list on the below table.
    :param request:
    :return:
    r  subcategory_idr  product_typeri  r  )r  r  r  r   r  r  r  r  r^  r   r   r!  r"  r#  r$  rd  r_  r  r  r   r   r  Tr  r  r  Fr  rd  rV  rS  ra  rT  r|   ra  rb  r^  rR  r   pagination.htmlre  rL  product_list_page.htmlproduct_page.htmltbody
paginationsafe)/rV  r   rO   r   r   r4  r   r  r  r  r  r  r  r3  r  r   r!  r"  r#  r$  rd  rP   r*  ra  r`  r+  r  r   r   r  r   r|   r)  r  r  r,  rM   rN   rV   rc  re  r   rW  rv   r   r   r   )!r   r  rh  r  ri  product_list	list_datar;  re  r4  jr-  r.  r   long_dist_sku_productlong_dist_product_flagxlong_dist_sales_checkrW  rZ  ar[  r\  r]  br^  cdr   r[  productsrt  rs  r   r   r   subcategory_product_list  s  	










r  c           !   
   C   s  | j d}| j d}| j d}tjj|dd}g }|r1|j|d}tjj|ddd	}g }| D ]}g }|j|j	|j
|jj|jjg d
}	|j D ]}
|
j|
j|
j|
j|
j|
j|
jd}ztjj||
dddd\}}|jdkr|
j|j |
jj|j |
jjj|j |
jjj|j ntj|
j|jj gdd |j!dkr|
j"dkrt#jj|
jdj$dd tjj|
jjdj$dd t%jj|
jjj dj$dd tjj|
jjj dj$dd nt#jj|
jdj$dd t#jj|
jddd}|stjj|
jjdj$dd n6d}|D ]!}tjj|dd& }|r5tjj|
jjdj$dd d} nq|sGtjj|
jjdj$dd t#jj|
jjddd}|sdtjj|
jjj dj$dd n8d}|D ]"}tjj|dd& }|rtjj|
jjj dj$dd d} nqh|stjj|
jjj dj$dd t#jj|
jjddd}|st%jj|
jjj dj$dd nAd}|D ]"}tjj|dd& }|rt%jj|
jjj dj$dd d} nq|st%jj|
jjj dj$dd nt#jj|
jdj$dd t#jj|
jddd}|stjj|
jjdj$dd n6d}|D ]!}tjj|dd& }|r?tjj|
jjdj$dd d} nq|sQtjj|
jjdj$dd t#jj|
jjddd}|sntjj|
jjj dj$dd n8d}|D ]"}tjj|dd& }|rtjj|
jjj dj$dd d} nqr|stjj|
jjj dj$dd t#jj|
jjddd}|st%jj|
jjj dj$dd n8d}|D ]"}tjj|dd& }|rt%jj|
jjj dj$dd d} nq|st%jj|
jjj dj$dd |j!|d< |j|d< |'|
 W qQ   d|d< d|d< |'| Y qQ||	d< |'|	 q7t(|d}|)d }t*d!d"|i}| j+j,d#krHt*d$d"|i} nt*d%d"|i} |r^t-t.|| |d&dd'S t-g | |d&dd'S )(z
    get the list of subcategories from a category
    on selecting the category from drop down a list of sub category loaded on drop down subcategory
    could be more efficient with processing limit to the page

    :param request:
    :return:
    r  r  ri  rj  r  rk  r  r   r  rl  rm  r_  r  r  r   r   r  r  Tr  r  Frn  rS  ra  rT  r|   ra  rb  r^  rR  r   ro  re  rL  rp  rq  )subcategoriesrs  rt  ru  )/rV  r   rO   r   r   r4  rN   r1  r   r  r  r  r  r  r  r3  r  r   r!  r"  r#  r$  rd  rP   r*  ra  r`  r+  r  r   r   r  r   r|   r)  rV   r,  rM   rc  re  r   rW  rv   r   r   r   r0  )!r   r  r  ri  rw  r  rx  r;  r4  re  ry  r-  r.  r   rz  r{  rr}  rW  rZ  nr[  r\  r]  lr^  gowr[  r  rt  rs  r   r   r   load_subcategories  s  	










r  c                 C   sB   | j d}|rtjj|ddd}tt|ddS tg ddS )Nr  r  r   r  Fru  )rV  r   rN   r   r   r1  r   r0  )r   r  r  r   r   r   load_subcategories_product	  s
   r  c              	   C   rP  )NrQ  rR  rS  r   r+  re  r  product_list.html)rO   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   r  r[  rS  r<  r   r   r   master_product_list	  s   

r  Fc              	      s\  | j dd}| j dd}| j dd}tjjddd}|r<tjt|dt|d	B t|d
B t|dB d}|rJt|d}|j|d}|rR|j|d}t	|d}| j d}z|
|}	W n ttfyt   |
d}	Y n ty   |
|j}	Y nw td | j  dv rd=  fdd|	jjD }
|	||||
dd}t| d|S )z
    In the product section in side bar. if we click on the master product.
    We will get the list of master products.
    :param request:
    :return:
    searchr   product_dateproduct_statusMaster Productrj  r  item_name__icontainsitem_code__icontains'item_category__category_name__icontains/item_sub_category__sub_category_name__icontains%Y-%m-%dr?  r  rR  rS  r   rc  c                    .   g | ]}|  d j dd d| dqS ?r  ru  z&page=)page_numberurl	urlencoder   r  base_urlquery_paramsr   r   r2   
      z'master_product_list.<locals>.<listcomp>r+  )re  search_queryr   r  pagination_linksr  r  )rV  r   rO   r   r   r4  rt   r   strptimer   rW  r   rX  r   rY  rq   copyr[  
page_ranger   )r   product_splitr  r  r  rw  formatted_dater[  rS  r  r  r<  r   r  r   r  	  sZ   


)rJ  rK  rL  r  c           .         s  | j dd}tjj|dd}| jjdkr&tj	dj| jj
d j
}tj d}tt|d	}|j}|j}|r[tjj|dt|d
t|dB t|dB t|dB d}| j d}	|	ri|j|	d}| j d}
|
rw|j|
d}g }|D ]}|j|j|j|jj|jjg d}g }|j D ]}z|j|j|j|j|j|j|jd}t jj!||dddd\}}|j"dkr|j#$| |j%j#$| |j%jj#$| |j%jj#$| nt&j'|j(|j#j
gdd |j)dkrI|j*dkr?t+jj|j(d }d|_,|-  tjj|j%j(d }d|_,|-  tjj|j%jj
d	 }d|_,|-  t.jj|j%jj
d	 }d|_,|-  nt+jj|j(dj/dd t+jj|j%ddd}|sftjj|j%j(dj/dd n6d}|D ]!}t jj|dd }|rtjj|j%j(dj/dd d} nqj|stjj|j%j(dj/dd t+jj|j%jddd }|st.jj|j%jj
d	j/dd n8d}|D ]"}t jj|dd }|rt.jj|j%jj
d	j/dd d} nq|st.jj|j%jj
d	j/dd t+jj|j%jddd!}|stjj|j%jj
d	j/dd nAd} |D ]"}!t jj|!dd }"|"r4tjj|j%jj
d	j/dd d}  nq| sGtjj|j%jj
d	j/dd nt+jj|j(dj/dd t+jj|j%ddd}|sptjj|j%j(dj/dd n6d}|D ]!}#t jj|#dd }|rtjj|j%j(dj/dd d} nqt|stjj|j%j(dj/dd t+jj|j%jddd }|st.jj|j%jj
d	j/dd n8d}|D ]"}$t jj|$dd }|rt.jj|j%jj
d	j/dd d} nq|st.jj|j%jj
d	j/dd t+jj|j%jddd!}|stjj|j%jj
d	j/dd n8d} |D ]"}%t jj|%dd }"|"r=tjj|j%jj
d	j/dd d}  nq| sPtjj|j%jj
d	j/dd |j)|d"< |j"|d#< |0| W q t1y }& zt23d$|&  d%|d"< d%|d#< |0| W Y d&}&~&qd&}&~&ww ||d'< |0| q{t4|d(}'| j d)}(z|'5|(})W n  t6t7fy   |'5d*})Y n t8y   |'5|'j9})Y nw t:d+|d,d-d. | j ; d)v r܈d)=  fd/d0|)j<j=D }*|)||*|d1||||d2	}+| jjdkr | j>d3d4krt?d5|)|*d6},t?d7d8|)i}-t@|,|-d9S tA| d:|+S | j>d3d4kr?t?d;|)|*d6},t?d7d8|)i}-t@|,|-d9S tA| d<|+S )=z
    get the product list on shop admin product listing.
    we can view the product menu on the side bar.
    :param request:
    :param shop_id:
    :param product_type:
    :return:
    r  r   rj  r  rL  r  r	  rQ  r  r  r  r  r  category_pagerk  subcategory_pager  rl  rm  r_  r  r  r   r   r  r  TFr  rn  rS  ra  rT  r|   ra  z'SKU Status Issue(display as disabled): rb  Nr^  rR  rS  r   list_shop_productr  )r  ri  kwargsc                    r  r  r  r  r  r   r   r2  4  r  z product_list.<locals>.<listcomp>r+  )	re  r  r  ri  rU  r  	shop_nameshop_locationr  x-requested-withXMLHttpRequestrp  )re  r  ro  re  rr  zproduct_list_shop_admin.htmlrq  zproduct_list_shop.html)BrV  r   rO   r   r   r4  r   r   rJ   r-  r   rc  rM   r  r   rj  rl  rt   r   r  r  r  r  r  r  r3  r   r!  r"  r#  r$  rd  rP   r*  ra  r`  r+  r  r   r   r  r|   r)  rV   r  r  rN   r,  re  r   r   r   r   rW  r   rX  r   rY  rq   r  r[  r  headersrv   r   r   ).r   r  ri  r  rw  r  shop_detailsr  r  r  sub_category_pagerx  r;  re  r4  ry  r-  r.  r   
sku_updateproduct_updateproduct_category_updateproduct_sub_category_updaterz  r{  r  r}  rW  rZ  vr[  r\  r]  r  r^  stqr   r[  rS  r  r  r<  rs  rt  r   r  r   rw  
  sP  










	rw  c                 C   sF  | j dkr!| jd}| jd}| jd}z
tjj||d}W n tjy3   tddd Y S w z||_||_|	  |d	kr|j
jd
krtjj|djdd tjj|j
jjdjdd tjj|j
jjjdjdd tjj|j
jjjdjdd ntjj|djdd tjj|j
jd	d
d}|stjj|j
jjdjdd n5d}|D ] }tjj|d	d }|rtjj|j
jjdjdd d} nq|stjj|j
jjdjdd tjj|j
jjd	d
d}	|	stjj|j
jjjdjdd n:d}
|	D ]#}tjj|d	d }|r#tjj|j
jjjdjdd d}
 nq|
s7tjj|j
jjjdjdd tjj|j
jjd	d
d}|sVtjj|j
jjjdjdd n:d}|D ]#}tjj|d	d }|r|tjj|j
jjjdjdd d} nqZ|stjj|j
jjjdjdd |d	kr|j
j|j |j
jj|j |j
jjj|j |j
jjj|j n|j
j|j tj|j
j|jjgdd z:| j}d}d|jj d|jj d}d|j
j  d|j
j! d| d|j
jj" d|j
jj# d}t$jj%||||d W n   Y tdd iW S    td!d"i Y S d#S )$ac  
    it is the status which can be changed with the admin dashboard not shop admin
    we have the status details in the sales product selection table
    in that table we have a field status
    with this view we can change that status
    with this change of status shop admin status also change to the new status.

    :param request:
    :return:
    r   r%  r  r|   r`  r^  FOperation not allowed.r  r   r_  r  r  Tr  r  rn  rS  ra  rT  r   r   r  z Product Status Updated for Shop r  r   r  r   updated its status to rE  r  r   r  r  r   Invalid request.N)&r   r   r   rP   r   r=  r   r|   ra  r  r^  r)  rV   r   r,  rO   r  r  rM   r  r   rN   r  rc  r`  r+  rA  r   r   r   rj  rk  r   r!  r  r  r   r  )r   r%  r  
new_statussales_unit_productrz  r{  r;  r}  rW  rZ  r[  r\  r]  r^  r  r  r   r   r   r   r   update_product_sales_status^  s   





8
r  c           <   
   C   s	  | j d}| j d}| j d}tjj|d }tjd}|r)|j|d}|r1|j|d}tjj|d}g }g }	|D ]}
|
j	|
j
|
j|
jj|
jjg d	}g }|
j D ]}|j	|j|j|j|j|j|jd
}|jdkr|j|d }||_||_||d< ||d< || || |dkr|j| |jdkrd|_n<d|_n8|j| d|_n.|j|d }|j|d< |d dkr|jdkrd|_nd|_|j|d< || || |   qW||d< |	| q>tj!|ddg |rt"jj|dd}|jdd}|st#jj|dj$dd n2d}|D ]}tjj|dd }|r8t#jj|dj$dd d} nq|sHt#jj|dj$dd |s^t#jj|d }|r]|j| nd}|D ]}tjj||d }|r{|jdkr{d} nqb|rt#jj|d }|r|j| nt#jj|d }|r|j| nt#j D ]}t"jj|dd}|jdd}|sd|_|   n*d}|D ]}tjj|dd }|rd|_|   d} nq|sd|_|   |s|j| qd}|D ]}tjj||d }|r|jdkrd} nq|r%|j| q|j| q|rt"jj|dd}|jdd}|sMt%jj|dj$dd n2d}|D ]}tjj|dd } | rot%jj|dj$dd d} nqQ|st%jj|dj$dd |st%jj|d }|r|j| ncd}!|D ]}"tjj|"|d }#|#r|#jdkrd}! nq|!rt%jj|d }|r|j| n-t%jj|d }|r|j| n|rrt%jj|dD ]}$t"jj|$dd}|jdd}|sd|$_|$   n/d}|D ] }%tjj|%d } | r(| jdkr(d|$_|$   d} nq	|s4d|$_|$   |s?|$j| qd}!|D ]}&tjj|&|d }#|#r\|#jdkr\d}! nqC|!ri|$j| q|$j| qnt%j D ]}$t"jj|$dd}|jdd}|sd|$_|$   n*d}|D ]}'tjj|'dd } | rd|$_|$   d} nq|sd|$_|$   |s|$j| qwd}!|D ]}(tjj|(|d }#|#r|#jdkrd}! nq|!r|$j| qw|$j| qwg })g }*|D ]}+t"jj|+dd},|,jdd}-|-sd|+_|+   n*d}.|-D ]}/tjj|/dd } | r8d|+_|+   d}. nq|.sDd|+_|+   |,sO|+j| qd}0|,D ]}1tjj|1|d }2|2rl|2jdkrld}0 nqS|0ry|+j| q|+j| qz| j&}3d}4d}5d}6t'jj(|3|4|5|6d  W n   Y t)|	d!}7|7*d"}8| j+d#d$krt,d%d&|8i}9t,d'd&|8i}:t-|9|:d(S t.d)|d*d+d,d-};t/|;S ).z;

    :param request:
    :param shop_id:
    :return:
    r  subcategorystatus_category_selectr  r  item_category__uuid)item_sub_category__uuidsales_unit__uuidrl  rm  r_  r]  r|   ra  r  TFr^  )product__item_category__uuidrd  )rV  r  rS  r@  )rU  rd  ) product__item_sub_category__uuidrd  )category__uuid)rb  rd  )r  rd  r  z3Product Status Updated for Category or Sub CategoryzBulk Category updater  rR  r   r  r  rq  re  ro  rr  r  r  r+  )r  ri  rU  r  )0r   r   rJ   r   r   rc  rO   r4  rP   r   r  r  r  r  r  r  r3  r  r   r!  r"  r#  r$  rd  r|   ra  re  r`  r+  r)  r  rA  r  bulk_updaterV   rM   r,  rN   r   r   r  r   rW  r  rv   r   rq   r   )<r   r  r  r  r  rf  rw  product_selectionupdate_list	show_listr;  re  r4  ry  r-  checksku_category_datar\  r]  zr^  update_statuscategory_remove_flagr  sku_category_salescatyr  sku_sub_category_datarW  rZ  ur[  sub_category_remove_flagr  sku_sub_category_salessub_catr  hr  psku_category_listsku_sub_category_listr  sku_productsrz  r{  r   product_remove_flagsku_productsku_product_salesr  r  r   r   r[  r  rs  rt  r  r   r   r   category_based_update_status  sl  	










-





r  c           	      C   s  | j dkr| jd}| jd}| jd}z
tjj||d}W n tjy2   tddd Y S w z||_|  |d	kre|j	j
|j
 |j	jj
|j
 |j	jjj
|j
 |j	jjj
|j
 n|j	j
|j
 tj|j	j|j
jgd
d z:| j}d}d|j
j d|j
j d}d|j	j d|j	j d| d|j	jj d|j	jj d}tjj||||d W n   Y tddiW S    tddi Y S d S )Nr   r%  r  r|   r  Fr  r  r_  r   r   r  z+Product Shop Admin Status Updated for Shop r  r   r  r  r  rE  r  r   r  r  r   r  )r   r   r   rP   r   r=  r   ra  r  r^  r`  r+  r  r  r  rA  r   r   r  r   r   rj  rk  r   r!  r  r  r   r  )	r   r%  r  r  r  r  r  r   r   r   r   r   #update_product_product_admin_statusi  sB   
8
r  rL  c              
   C   sr  | j d}| j d}| j d}tjj|d }tjd}|r)|j|d}|r1|j|d}g }tjj|d}g }g }	|D ]}
|
j	|
j
|
j|
jj|
jjg d	}g }|
j D ]c}|j	|j|j|j|j|j|jd
}|jdkr|j|d }|jdkr||_|j|d< ||d< n
|j|d< |j|d< || || qY|j|d }|j|d< |j|d< || || qY||d< |	| q@tj|dg tj|j|gdd tj|j||gdd t j|j||gdd t!|	d}|"d}z| j#}d}d}d}t$jj%||||d W n   Y | j&ddkr1t'dd|i}t'dd|i}t(||dS t)d}t*|S )Nr  r  r  r  r  r  r  r  rl  rm  r_  r]  r|   ra  r^  r   r   rR  r   r  z<Product Status Updated for Category or Sub Category For ShopzBulk Category update for Shopr  r  r  rp  re  ro  rr  list_product_shop_admin)+r   r   rJ   r   r   rc  rO   r4  rP   r   r  r  r  r  r  r  r3  r  r   r!  r"  r#  r$  rd  r|   ra  re  r  r   r   r   r   r   r   rW  r   r   r  r  rv   r   rq   r   )r   r  r  r  r  rf  rw  r  r  r  r;  re  r4  ry  r-  r  r[  r  r  r  r   r   rs  rt  r  r   r   r   'category_based_shop_admin_update_status  s|   












r  c                 C   s  | j dd}| j dd}| j dd}tddd	d
d}tddddd}tddddd}g }| jddkrtt|d}	t| j }
tjj	|	d}|

 r|
jdd}|	|_|  z'| j}d}d}d|j d|j d|jj d|jj }tjj||||d W n   Y tdd |i}t|g d!||||||d"	S i }|
j D ]
\}}|d# ||< q||
j |rtdd |i}t||d||||||d"	S tg |d||||||d"	S tg d$d%id||||||d"	S )&Nimage_link_1/media/icons/icon.jpgimage_link_2)/media/standard_images/standard_image.jpgimage_link_3/media/banners/banner.jpgimage_field.htmluploadimage_reset_button1r  
class_nameimage_reset_button
field_nameupload2image_reset_button2r  upload3image_reset_button3r  r  r  r  r  Fr  r  r  r  r  r  r  r  zsub_category_delete_list.htmlr  T)	r>  r}  r  image_field1image_field2image_field3link_1link_2link_3r   r  zSomething went wrong try again)r   r   rv   r  r   rM   r6   rN   r   r   r  r  r  r   r  r  r  r  r   r  r   r}  itemsre  )r   r  r
  r  r  r  r  r	  r}  r  r  r  r  r  r  r   r   sub_categoriesr   r;  r>  r   r   r   sub_category_creation  s   
(


	r  c                 C   sd   | j dd }|rtjj|d}ntj }g }|D ]}||j|j|j	|j
jd qtd|iS )Nr  r  )r  r   pricer  r  )rV  r   rO   r   r   r  re  r  r   r  r  r   )r   r  r  rw  r  r   r   r   filter_products6  s   

r  c                 C   z   | j dd}| j dd}| j dd}tddd	d
d}tdddd|d}tdddd|d}t||||||dS )Nr  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  linkr  r  r  r  r  r	  r
  r  r  rV  r   rv   r   r   r
  r  r  r  r  r	  r   r   r   image_field_rendering  .   r  c              	   C   rP  )z
    In the product section on the side bar. We have
    the product tags. We list the products based on the tags
    on app.
    :param request:
    :return:
    rQ     rS  r   r+  )tag_datar  ztag_list.html)rS   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   tagsr[  rS  r<  r   r   r   tag_list     	

r  c                 C   r  )zq
    delete a tag listed in product section on side bar.
    :param request:
    :param tag_id:
    :return:
    r  r  )rS   r   r   r  r   )r   tag_idr  r   r   r   
tag_delete  s   
r   c                 C   s   g }t t|d}| jdkr4t| j| j|d}| r-| jddr%d|j_	|
  tdS ||j nt|d}|dd	}t| d
|S )z
    If we click on the edit button on tag listing we are
    redirecting to edit form. We can change the tag details
    with the form.
    :param request:
    :param tag_id:
    :return:
    r  r   r  r  r   Nr  r+  )tag_formr  ztag_edit.html)r   rS   r   r8   r   r  r  r   r  r  r  r   re  r}  r   )r   r  r}  r  r  r<  r   r   r   tag_edit  s   


r"  c                 C   v   g }| j dkr,t| j| j}| r| }tdS ||j ||dd}t	| d|S t }||dd}t	| d|S )z
    In the tag list window we have a button add tag. on clicking
    the add tag button. we can add a new tag. fields are
    tag_name, icon, products.
    :param request:
    :return:
    r   r  r+  )r}  r!  r  ztag_add.html)
r   r8   r   r  r  r  r   re  r}  r   )r   r}  r!  r  r<  r   r   r   tag_add  s$   

r$  c              	   C   rP  )zIn app just like tags we have easy filters also. One of them is dynamic
    filter. for eg: black forest. if we select the black forest we will return the
    products associated with the filter.
    admin listing of dynamic filter.
    rQ  r  rS  r   r+  )filter_datar  zdynamic_filter_list.html)rT   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   filter_listr[  rS  r<  r   r   r   dynamic_filter_list	  s   

r'  c                 C   r  )a=  
    Dynamic filter also listed on the product section on side bar.
    for each dynamic filter, There is a delete button on the listing.
    on clicking the delete button. a confirmation dialog box will appear.
    click yes and we could delete the filter.
    :param request:
    :param filter_id:
    :return:
    r  r&  )rT   r   r   r  r   )r   	filter_idr&  r   r   r   dynamic_filter_delete     r)  c                 C   sn   g }t t|d}| jdkr't| j|d}| r |  tdS ||j	 nt|d}|dd}t
| d|S )a  
    Editing a dynamic filter. for each dynamic filter listed on
    dynamic filter list, we have a edit button. clicking on
    the edit button will open a form. fields are tag name and products.

    :param request:
    :param filter_id:
    :return:
    r  r   r  r&  r+  )filter_formr  zdynamic_filter_edit.html)r   rT   r   r9   r   r  r  r   re  r}  r   )r   r(  r}  r&  r  r<  r   r   r   dynamic_filter_edit0  s   


r,  c                 C   r   g }| j dkr*t| j}| r| }tdS ||j ||dd}t| d|S t }||dd}t| d|S )aJ  
    Adding a dynamic filter.
    On dynamic filter listing page which we can open by clicking
    on the product section on side bar have a add dynamic filter button.
    clicking will open the form enter the products and tag name on the form.
    on submission create a new dynamic filter.

    :param request:
    :return:
    r   r&  r+  )r}  r+  r  zdynamic_filter_add.html)	r   r9   r   r  r  r   re  r}  r   )r   r}  r+  r   r<  r   r   r   dynamic_filter_addL  s$   

r.  c              	   C   rP  )z
    special list showed on the app. for eg:  Rolls - Chicken Spring Roll,
    special list will list when we click on the special list on product section
    on side bar.
    :param request:
    :return:
    rQ  r  rS  r   r+  )special_datar  zspecial_list.html)rU   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   special_product_listr[  rS  r<  r   r   r   special_lists  r  r1  c                 C   r  )a#  
    Deleting a special list. for each special list listed on
    special listing, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the special list.
    :param request:
    :param special_list_id:
    :return:
    r  r1  )rU   r   r   r  r   )r   special_list_idr0  r   r   r   special_list_delete  r*  r3  c                 C   s   g }t t|d}| jdkrJt| j| j|d}| rC| jddr%d|j_	| jddr0d|j_
| jddr;d|j_|  td	S ||j nt|d}|d
d}t| d|S )a0  

    Editing a special list. for each special list listed on
    special listing, we have a edit button. clicking on
    the edit button will open a form. fields are special name and icon,
    standard image, banner image, and products.

    :param request:
    :param special_list_id:
    :return:
    r  r   r  r  r   Nr  r  r1  r+  )special_formr  zspecial_list_edit.html)r   rU   r   r:   r   r  r  r   r  r  r  r  r  r   re  r}  r   )r   r2  r}  r0  r  r<  r   r   r   special_list_edit  s"   


r5  c                 C   r#  )a`  
    Adding a special list.
    On special listing page which we can open by clicking
    on the product section on side bar have a add special list button.
    clicking will open the form enter the special name and icon,
    standard image, banner image, and products.
    on submission create a new special list.
    :param request:
    :return:
    r   r1  r+  )r}  r4  r  zspecial_list_add.html)
r   r:   r   r  r  r  r   re  r}  r   )r   r}  r  specialr<  r   r   r   special_list_add  s$   
r7  c                   @      e Zd ZdZdd ZdS )SendVerificationEmailViewz7
    View to send delivery boy verification email.
    c              
   C   sz  |j }tt|d}|jdkrddd}t|d|S tjt|jt	
 tdd d	tjd
d}ttdd}| td|gd }d}	ztjd| |jgdd dddd}W n tyk }
 zd}	W Y d}
~
nd}
~
ww d}ztdd| tj|jgdd W n ty }
 zd}W Y d}
~
nd}
~
ww |	r|rdddd}n|	rdddd}n|rdddd}ndddd}t|d|S ) a  
        On orders on side bar, we have a menu delivery details. if we click on
        delivery details we have the delivery boys listed. we can add new delivery
        boys. for assigning a order, adding alone not help. for that we have to
        verify the delivery boy. we will send the verification sms to the phone number using this view.

        :param request:
        :param delivery_boy:
        :return:
        r  verifiedzPhone  alreay verified .r   )r   r|   phone_verification.html   )hours)delivery_boyexpHS256)	algorithmDOMAINzhttp://localhost/zverify-phonerG  Tzclick here to verify: r   r   ,Verification link sent to your phone number.r   r   r|   r  FNzVerify your emailz%Click the link to verify your email: r  zDVerification link sent to your phone number and email. Please check.z:Verification link sent to your phone number. Please check.z3Verification link sent to your email. Please check.zInvalid Phone number or email!)r   r   r]   r|   r   jwtencoder  r   r   rN  ro   r   
SECRET_KEYgetattrrq   r   r   r  r   rm   DEFAULT_FROM_EMAILr   )selfr   r>  r   delivery_boysr<  tokendomainverification_linksms_flagr   
email_flagr   r   r   post  sN   

 
zSendVerificationEmailView.postN)__name__
__module____qualname____doc__rQ  r   r   r   r   r9        r9  c                   @   r8  )VerifyEmailViewz
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    this view is to verify with the link.
    we send a link to verify the delivery boy on clicking that link, VerifyEmailView will
    verify.
    c              
   C   s   z$t j|tjdgd}tt|d d}d|_|  t|dddd	d
W S  t j	y8   t|dd	dd	d
 Y S  t j
yK   t|dd	dd	d
 Y S w )Nr@  )
algorithmsr>  r  r:  r;  r   z#Phone number verified successfully.r   )r|   r   r  zVerification link has expired.zInvalid verification link.)rE  decoder   rG  r   r]   r|   r  r   ExpiredSignatureErrorInvalidTokenError)rJ  r   rL  payloadrK  r   r   r   r   >  s.   
zVerifyEmailView.getN)rR  rS  rT  rU  r   r   r   r   r   rW  7  rV  rW  rM  c              	   C   s   t jdj| jjd}|r|d }nd}tjj|dd}t|d}| j	
d}z||}W n ttfyA   |d	}Y n tyO   ||j}Y nw |d
d}t| d|S )z
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    It is only available for shop admin.
    each shop have their own delivery boys.
    :param request:
    :return:
    r  r	  r   r   r  r  r  rS  r   r+  )delivery_boy_dataorders_activezdelivery_boy_list.html)rJ   r   r-  r   r   r   r]   r4  r   rV  r   rW  r   rX  r   rY  r   )r   r5  r  rK  r[  rS  r<  r   r   r   delivery_boy_listZ  s$   


r_  c                 C   r  )a8  
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    Deleting a delivery boy. for each delivery boy listed on
    delivery boy list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the boy.
    r  r_  )r]   r   r   r  r   )r   delivery_boy_idr>  r   r   r   delivery_boy_delete}     ra  c                 C   s   g }t jj|d }| jdkr1t| j|d}| r*| }d|_	|  t
dS ||j nt|d}|dd}t| d|S )	a  
    Delivery details menu on Orders section in the side bar will list the delivery boys.
    Editing a delivery boy. for each delivery boy listed, we have a edit button. clicking on
    the edit button will open a form. fields are team member name and phone number,
    email.

    r  r   r  znot verifiedr_  r+  )delivery_boy_formr^  zdelivery_boy_edit.html)r]   r   r   rc  r   r;   r   r  r  r|   r   re  r}  r   )r   r`  r}  r>  r  r<  r   r   r   delivery_boy_edit  s   



rd  c                 C   s   g }| j dkrFtjdj| jjd}|r|d }nd}t| j}|	 r4|
 }||_|
  tdS ||j ||dd}t| d	|S t }||dd}t| d	|S )
a4  
    Adding a delivery boy.
    On delivery boy listing page which we can open by clicking
    on the order section on side bar have a add delivery boy button.
    clicking will open the form enter the fields are team member name and phone number,
    email.
    on submission create a new delivery boy.
    r   r  r	  r   r   r_  r+  )r}  rc  r^  zdelivery_boy_add.html)r   rJ   r   r-  r   r   r   r;   r   r  r  rf  r   re  r}  r   )r   r}  r5  r  r  r>  r<  r   r   r   delivery_boy_add  s4   


re  c           &         s`  z| j d}| j dd}tjj| jjd}|r|d nd}g }| jd}| jd}| jdkrt	jj|d	
 }	tjj|d

 }
t| j|	d}| r|jdd}|
|_d|_|  |
jdkrd|
_|
  d}d}t|
jj|
|||
j t|||
jjgd d}|
jjdkrtj|
jj|
j|
jj|gdd td||
jdW S ||j  td|j dW S | j dd}| j dd}| j dd}| j dd}|s|}| j dd}|s|}t!j"d#t$dt%j& d }|rtjj|d!d"gd#j'd$d%}ntjjd&d'j'd$d%}|"d(d)d*#t$d+|d (d,}|d-kr*|jd.d%}|rM|d/kr@|t)d0d1t)d2d1B }n|t)d3d1t)d4d1B }| j*d5d6kr|rs|t)|d7t)|d8B t)|d9B t)|d:B (d,}|rzt+,|d;}|j|d<}W n
 t-y   Y nw |r|j|d=}|r|r|d>kr|d(kr|(|d? }n|(|}n|d(kr|(d@| d? }n|(d@| }t.|dA}| j dB}z|/|}W n  t0t1fy   |/dC}Y n t2y   |/|j3}Y nw t4dD | j 5 dBv rdB=  fdEdF|j6j7D }|D ]x}
g }|
j8& D ]g}dG}z|j9: r9|j9& d ndG}W n t;yH   dG}Y nw |j<t=|ddGr^t=|j>dHdGr^|j>j?ndt=|j>dIdt=|j>dJd|j@|jA|ru|jBnd|rt=|dKdGr|jCjDndGdL} ||  q'tE|
dM| qt }z
tFjjdN|dO}!W n t;y   g }!Y nw dPdF |D }"|||||||!dQ||r|nd|"dR}#| j*d5d6kr|d-krdSdF |D }"tGdT|||!|||"dU}$ntGdV|||!|||"dU}$tGdWd+|i}%t|$|%dXW S |d-krtH| dY|#W S tH| dZ|#W S  tIy/   g dQd[}#tH| dZ|# Y S w )\a  
    List orders for shop admin / PU admin with product data and custom images (if present).
    Attaches `order.items` to each order in page results. Each item is a dict:
      {
        "product_name": ...,
        "sku_name": ...,
        "sku_quantity": ...,
        "sku_unit": ...,
        "quantity": ...,
        "price": ...,
        "custom_note": ...,
        "custom_image_url": ...  # None if no image
      }
    platformrB  r   r	  r   r  
sort_orderr   r   r  r  Fr  Courier Deliveryr  
DespatchedzOrder Despatchedz"Your Order Despatched Successfullyout_for_delivery_generalTr   r   r  r|   order_idOrderIDfailr&  r  r  r  r^  customizations)querysetr@  Pick Up)r   order_type__inr   r  rE  rF  r   drop_addressr   
order_datar  z
new ordersr  webWebrf  OtherIOSAndriodr  r  order_ID__icontainsorder_type__icontainsorder_status__icontains user_uuid__first_name__icontainsr  r?  r  asc__first_name-r  rS  r   
order_listc                    r  r  r  r  r  r   r   r2  |  r  zorder_list.<locals>.<listcomp>Nr   r"  r#  custom_image)product_namer   r"  r#  quantityr  custom_notecustom_image_urlr  r:  r|   rf  c                 S   s    g | ]}|j d krd|jiqS )r  r   )r  r   r   r   r   r   r   r2    s     r+  )courier_formrv  r  r   r  r  ongoing_ordersr^  rf  rB  r,  c                 S   s   g | ]}d |j iqS r   r  r  r   r   r   r2    s    znew_order_list_page.html)rv  r  r  rf  rB  r,  zorder_list_page.htmlorder_list_pagination.htmlrr  znew_orders.htmlzorder_list.htmlrv  r^  )JrV  r   rJ   r   r   r   r   COOKIESr   ra   rc  r`   r=   r   r  r  r   delivery_typer  r   r   r   r   r   opt_inr   r   r  r   r  r   re  r}  r_   select_relatedr-  ru   r^   r  r  r4  rt   r  r   r  
ValueErrorr   rW  r   rX  r   rY  rq   r  r[  r  rv  rp  existsr   r  rH  r^  r   r  r  r  r  r  setattrr]   rv   r   
IndexError)&r   rm  rf  rB  shop_qsr  r}  r  rg  order_deliveryr   r  delivery_datar   r   	temp_namer  
order_dater  sort_order_datafield_name_dataorderproducts_qsbase_qsr  r  r[  rS  ordersr  r  opcustomizationitemr  r,  r<  rs  rt  r   r  r   r    sp  

 










"(


	
r  c           	      C   s   | j d}g }d}tjj|d }|r#t|d}|jjdkr"d}nt	jj|d }tjj
d|d	}t|d}|jdkr@d}td
|||d}td|dS )Ncsrfmiddlewaretokenfalserh  r  r  truer  ri  )r  r   zcourier_edit_form.html)r  rm  button_statusTr  r  )r   r   ra   r   r   rc  r=   r   r  r`   r  rv   r   )	r   rm  csrfr}  r  r  r  r   r  r   r   r   courier_details_edit  s$   



r  c                   @   s   e Zd Zdd Zdd ZdS )ShipperAutocompletec                 C   s   |j  S r   )team_member_name)rJ  r  r   r   r   get_result_label  s   z$ShipperAutocomplete.get_result_labelc                 C   s(   t jjddd}| jr|j| jd}|S )Nr:  not assigned)r|   delivery_boy_status)team_member_name__istartswith)r]   r   r   r  )rJ  r  r   r   r   get_queryset   s   z ShipperAutocomplete.get_querysetN)rR  rS  rT  r  r  r   r   r   r   r    s    r  c              
   C   s   z:|r8| j j}| j j}| jj}| jj}d| d| d| d| }|||| jd}	tj|j|j|	gdd}	|	W S W d S  t	yU }
 zt
d|
  W Y d }
~
d S d }
~
ww )Nz https://www.google.com/maps/dir/,r  )pick_up_addressru  google_place_linkr   r   r   r   )ru  rp  rq  r   r   r   r   r  r  r   r   r   )r   r  ru  r>  	START_LAT	START_LNGDEST_LATDEST_LNGr  r>  r   r   r   r   WhatsappMessageSend  s$   r  c                 C   s0  z|g }t jj| dd}|D ]}z|jj}|jj}	|||	f W q   Y qdd |D }
d|
}d|j d}t	|j
j}t	|j
j}	d}| d	| d
|	 d| d
|	 d| d}d|j d| d| d| d	}tj||tj|jgdgdd}|W S  ty } ztd|  W Y d }~d S d }~ww )NDelivery Assigned)r>  r  c                 S   s"   g | ]}|d   d|d  qS )r   r  r   r   r   r;  r   r   r   r2  .  s   " z$ShipperEmailSend.<locals>.<listcomp>|zOrder z Assinged to youz&https://www.google.com/maps/dir/?api=1z&origin=r  z&destination=z&waypoints=z&travelmode=drivingz The Order no: z5 assigned to you by NavyaBakeShop.
Pickup Location : z 
Delivery Location : z%

Please click link to track order : z!

Navya Bakers
                  Fr   r   z2Failed to Send Email While Assigning Delivery Boy:)r`   r   r   ru  rp  rq  re  r   r   r  r   r   r   r   rn   r   r   r   r   )shipper_uuidr   r  ru  r>  location_listr  r;  rp  rq  map_linkmap_link_joinr   basemap_link_to_sendr   r   r   r   r   r   ShipperEmailSend!  s2   
( r  c              
   C   sX  | j dkr| jd}| jd}| jd}| jd}zbtjj|d }| jd}tjj|d }|jd	v rEt	d
ddW S |jdkrRt	d
ddW S |jdkr_t	d
ddW S |jdkrlt	d
ddW S d|_t
 |_||_ztjj||d|jid}W n   tjj||d  tjj||d|jid Y d|_|  |  tjj|jd }|rx|jrt|jjd t|jj d t|jj d t|jj d t|jj d t|jj d t|jj d t|jj nd}	|jr5t|jj d t|jj! d t|jj" d t|jj d t|jj d t|jj d t|jj nd}
t#|||
|	|}t$||
|	|}d}d}t%|j&j'||||j& t(|||j&j)gd
 d }|j&j*d!krxt+j,|j&j-|j.|j&j/|gd"d# t	d$|||d%W S  t0y } zt12d&|  t	d'|||d%W  Y d }~S d }~ww t	d(d'iS ))Nr   r  rS  r  r  r  
order_uuidr  )r  r   FzOrder not confirmed)r  r   r  zOrder already deliveredr   zOrder Failedr  zOrder already Assignedr  )r>  r   r  )r>  r   assignedr  z, r   r   r  z), r  zOrder AssignedzFYour Order Assigned Successfully, Delivery Boy will reach you shortly!delivery_assigned_generalTr   r   True)r  rS  r  r  z$Failed While Assigning Delivery Boy:Falser  )3r   r   r   r]   r   r   rc  r`   r  r   r   rN  order_assigntimer>  ra   r*  rB  r  r  r  r  ru  r  r   house_number_or_namerr  	land_markrs  ru  rt  rv  r   rj  rk  rl  r  r  r   r   r   r   r   r  r   r   r  r   r  r   r   r   )r   r  rS  r  r  shipperr  r   r>  ru  r  	email_msgwhatsapp_msgr   r   r  r   r   r   r   save_shipperJ  s   












 r  c              
   C   s  | j dkr| jd}| jd}| jd}z| jd}| jd }tjj|d }||_|	  |dkrz
t
|j|j}W n   t
 }Y | t
   }| }	t|	d	d
 }
|
d
k rud|_t
 |_|	  n!|
dk r|
d
krd|_t
 |_|	  nd|_t
 |_|	  |dkrd|_t
 |_|	  z|j}tjj|d }|jdkrd|_|	  W n   Y |dv rDd|_t
 |_|	  |dkrDzd|j}tjj|d }|jdkrd|_|	  tj }|j|jkr>|j|jkr>tj }dttdd }tjj|||j d}d}d| d}t!|j j"||||j  t#|||j j$gd W n   Y |	  zt%  W n   Y |dkr\d}d}n|d krfd!}d"}n|dkrpd#}d$}n	|dkryd%}d&}|}t!|j j"||||j  t#|||j j$gd |j j&d'krt'j(|j j)|j*|j j+|gd(d) t,d*|||d+W S  t-y } zt,d,t||||d-W  Y d }~S d }~ww d S ).Nr   rS  r  r  r  selected_valuer  r    r   Redr   OrangeWhiter   Dark Redr  r  r  )zDelivery Assingedrj  r  r  	NavyaCoupi  i )coupon_codecouponr   zEligible for Coupon!zYou are eligible for Coupon (z).Forder_packed_generalzJYour Order Packed Successfully, Delivery Boy will pick your order shortly!r  r  zPYour Order Assigned Successfully, your order will reach your foot steps shortly!delivered_generalz^Your Order Delivered Successfully, Please give your valuable feed for better future endevours!deliveryfailedcase_generalzSorry! Your Order Failed!Tr   r   r  r|   rS  r  r  ro  r|   r   rS  r  r  ).r   r   r   stripr`   r   r   rc  r  r  r   combinedelivery_slot_datedelivery_slot_timerN  
astimezonetotal_secondsdivmodcolor_statuscolor_status_updation_timer>  r]   r  rk   r   lower_limit_pricehigher_limit_pricer  randomrandintrl   r  r   r   r   r   r   rp   r  r   r   r  r   r  r   r   )r   rS  r  r  r  r|   r   combined_datetimetime_dif_with_del_slotduration_in_s_data	hours_delr  r  custom_coupon_checkcustom_coupon_settingscoupon_code_generationcustom_couponnotification_titlenotification_descriptionr  r   r   r   r   r   r   save_order_status  s   


















 r  c              
   C   s   | j dkr[z:| jd}| jd}tjj|d }|s%tdddW S |jdkr2tdd	dW S ||_|	  td
diW S  t
yZ } ztdt|dW  Y d }~S d }~ww d S )Nr   r  r  r  ro  Order not foundr   	Contactedz;Order status cannot be changed after being set to Contactedr|   r  )r   r   r   r`   r   r   rc  r   r  r  r   r  )r   r  r|   r   r   r   r   r   save_customorder_status  s&   

r  c                 C   s  | j dkrtdddS | jd}tjj|d }|s$tddd	S | jd
}| jd}| jd}d }d |_d |_	|dkrRd}| jd}|rQt
||_n6|dkrd}| jd}| jd}	|rkt
||_|	rztjj|	d}
|
j|_	W n tjy   d |_	Y nw |  tjj|d\}}||_||_||_|  t| j|d}| r|  tdddS td|jdS )Nr   r   r'  r(  r   r  ro  r  r   r  quantity_unitr  pickuprr  pickup_datehome_deliveryHome Deliverydelivery_date	time_slotr  rm  r  r  zOrder updated successfullyr&  )r   r   r   r   r`   r   r   rc  r  r  r}   rc   
start_timer=  r  r\   r*  r  r  r  rD   r  r}  )r   r  r   r  r  r  del_typer  r  slot_idslotcustom_trackcreatedr  r   r   r   save_customorder_data*  sT   



r  c           
   
   C   s"  | j dkr| jd}| jd}| jd}zYtjj|d }|jdkrgd|_|  d}d	}d
}t	|j
j||||j
 t|||j
jgd |j
jdkr]tj|j
j||j
j|gdd td|||dW S td|||dW S  ty }	 ztdt|	|||dW  Y d }	~	S d }	~	ww d S )Nr   rS  r  r  r  r  r  zOrder Viewedz:Your Order Viewed by the Merchant and will be packed soon!custom_order_requestFTr   r   r  r  ro  r  )r   r   r   r`   r   r   rc  r  r  r   r   r   r   r   r  r   r   r  r  r   r   r  )
r   r   rS  r  r  r   r   r   r  r   r   r   r   order_status_update_to_viewedn  s2   

	r  c              	   C   r  )a?  
    Coupons  menu on Promotions section in the side bar will list the coupons.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete coupons.
    this defined coupons will list on apps.
    if they are eligible they can use the coupons.
    :param request:
    :return:
    rQ  r  rS  r   r+  )coupon_datapromotion_activezcoupon_list.html)rb   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   r  r[  rS  coupon_list_datar<  r   r   r   coupon_list  s   

r  c                 C   sL   t jj|d}|r"tjj|d}|rt| tjd tdS |  tdS )aW  
     Coupons  menu on Promotions section in the side bar will list the coupons.
    Deleting a coupon. for each coupon listed on
    coupon list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the coupon.
    :param request:
    :param coupon_id:
    :return:
    r  )Couponz/Your coupon used in ads! couldn't delete couponlist_coupons)	rb   r   r   re   r   r  rd  r  r   )r   	coupon_idr  rf  r   r   r   coupon_delete  s   
r  c              
   C   sv  g }t jj|d }| jdkr$| j }d|ddv r1|dd}|r+|d |	d| d|ddv rL|dd}|rF|d |	d| d|ddv rg|dd}|ra|d |	d| d|ddv r|dd}|r||d |	d| |
d	d}	z"|	d
kr|d= |d= |d= n|	dkr|d= |d= n|	dkr|d= W n   Y t|| j|d}
|
 r| j
ddrd|
j_| j
ddkrd|
j_d|
j_nd|
j_z|
  W n- ty } z |j}|
t| d |jd  td|
jdW  Y d}~S d}~ww tddiS ||
j td|
jdS t|d}
||
|jd|j|jd}t | d|S )a  
    Coupons  menu on Promotions section in the side bar will list the coupons.
    Editing a coupon. for each coupon listed, we have a edit button. clicking on
    the edit button will open a form. fields are coupon_name and coupon_code,
    coupon_description, icon, coupon_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku, minimum_total_bill_amount, coupon_type,
    discount_amount, no_of_users, validity_start_date, validity_end_date, terms_and_conditions.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on coupon_on field.
    coupon maybe amount based or percentage based. the field used to indicate is
    coupon_type.

    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.

    :param request:
    :param coupon_id:
    :return:
    r  r   r   ApplicableCategoryNApplicableSubCategoryApplicableProductApplicableSkuCouponOnCategorySubCategoryr`  r  r  
CouponTypeamountr   Fr  r  Tr+  )r}  coupon_formr  r  r  coupon_typezcoupon_edit.html)!rb   r   r   rc  r   r   r  r  rA  setlistr   r>   r  r  r  IconDiscountPercentageMaxDiscountAmountForPercentageDiscountAmountr  r   message_dict	add_errorr0  r~  r   r   r}  re  r   r  r  r   )r   r  r}  r  	post_datar  r  rw  r4  	coupon_onr  r   
error_datar<  r   r   r   coupon_edit  s   







r-  c              
   C   sX  g }| j dkr| j }d|ddv r(|dd}|r"|d |d| d|ddv rC|dd}|r=|d |d| d|ddv r^|dd}|rX|d |d| d|ddv ry|dd}|rs|d |d| |dd}z"|d	kr|d= |d= |d= n|d
kr|d= |d= n|dkr|d= W n   Y t|| j}|	 r| jddrd|j
_| jddkrd|j
_d|j
_nd|j
_z|  W n- ty }	 z |	j}
|t|
 d |	jd  td|jdW  Y d}	~	S d}	~	ww tddiS ||j td|jdS t }g }||dd	dd}t| d|S )as  
    Coupons  menu on Promotions section in the side bar will list the coupons.
    adding a coupon.We have a add coupon button. clicking on
    the add button will open a form. fields are coupon_name and coupon_code,
    coupon_description, icon, coupon_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku, minimum_total_bill_amount, coupon_type,
    discount_amount, no_of_users, validity_start_date, validity_end_date, terms_and_conditions.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on coupon_on field.
    coupon maybe amount based or percentage based. the field used to indicate is
    coupon_type.

    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    :param request:
    :return:
    r   r   r  Nr  r  r  r  r  r  r`  r  r  r   r   Fr  r  Tr+  )r}  r!  r  r  r"  zcoupon_add.html)r   r   r  r  rA  r#  r   r>   r  r  r  r$  r%  r&  r'  r  r   r(  r)  r0  r~  r   r   r}  re  r   )r   r}  r*  r  r  rw  r4  r+  r!  r   r,  r<  r   r   r   
coupon_add6  s   






r.  )rJ  rK  rM  c              	   C   r  )z
    In NBC admin dashboard, On the orders section we have order slot menu. clicking on the
    menu will open the slot list. we can add, edit and view the slots from here.
    :param request:
    :return:
    rQ  r  rS  r   r+  )delivery_slot_datar^  zdelivery_slot_list.html)rc   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   delivery_slotsr[  rS  delivery_slotr<  r   r   r   delivery_slot_list  r  r2  c                 C   r  )aD  
    In NBC admin dashboard, On the orders section we have order slot menu.
    on the slots listed, for each slot we have a delete button. Clicking on
    the delete button will open a dialog box. clicking on the confirm will
    delete the delivery slot.

    :param request:
    :param delivery_slot_id:
    :return:
    r  r2  )rc   r   r   r  r   )r   delivery_slot_idr1  r   r   r   delivery_slot_delete     r4  c                 C   st   g }t jj|d }| jdkr*t| j|d}| r#| }t	dS |
|j nt|d}|dd}t| d|S )a  
    In NBC admin dashboard, On the orders section we have order slot menu.
    on the slots listed, for each slot we have a edit button. Clicking on
    the edit button will open a edit form. fields of the form are  start_time,
    end_time, is_available, status. based on the availability status, it is
    showing on the app(select Delivery slots).

    :param request:
    :param delivery_slot_id:
    :return:
    r  r   r  r2  r+  )delivery_slot_formr^  zdelivery_slot_edit.html)rc   r   r   rc  r   r?   r   r  r  r   re  r}  r   )r   r3  r}  r1  r  del_slotr<  r   r   r   delivery_slot_edit  s   


r8  c                 C   r-  )a  
        In NBC admin dashboard, On the orders section we have order slot menu.
    Clicking on the Add Delivery Slot button will open a add delivery slot form.
    fields of the form are  start_time, end_time, is_available, status.

    :param request:
    :return:
    r   r2  r+  )r}  r6  r^  zdelivery_slot_add.html)	r   r?   r   r  r  r   re  r}  r   )r   r}  r  r7  r<  r   r   r   delivery_slot_add  s$   

r9  c              	   C   r  )a  
    Discount  menu on Promotions section in the side bar will list the discounts.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete discounts.
    this defined discount will shown on product listing with a strike.
    rQ  r  rS  r   r+  )discount_datar  zdiscount_list.html)rd   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   discountr[  rS  discount_list_datar<  r   r   r   discount_list  r  r=  c                 C   sH   t jj|d}|rtjj|d}t| tjd tdS |  tdS )ac  
    Discount  menu on Promotions section in the side bar will list the discounts.
    Deleting a discount. for each discount listed on
    discount list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the discount.
    :param request:
    :param discount_id:
    :return:
    r  )rd   z3Your Discount used in ads! couldn't delete discountlist_discounts)	rd   r   r   re   r   r  rd  r  r   )r   discount_idr;  rf  r   r   r   discount_delete(  s   
r@  c                 C   s  g }t jj|d }| jdkr| j }d|ddv r0|dd}|r*|d |	d| d|ddv rK|dd}|rE|d |	d| d|ddv rf|dd}|r`|d |	d| d|ddv r|dd}|r{|d |	d| |
d	d}	z"|	d
kr|d= |d= |d= n|	dkr|d= |d= n|	dkr|d= W n   Y t|| j|d}
|
 r| j
ddrd|
j_| j
ddrd|
j_|
  tddiS ||
j td|
jdS t|d}
||
|jd|jd}t| d|S )a  
    Discount  menu on Promotions section in the side bar will list the discounts.
    Editing a discount. for each discount listed, we have a edit button. clicking on
    the edit button will open a form. fields are discount_name and discount_code,
    discount_description, discount_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku,
    discount_percentage.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on discount_on field.
    discount is percentage based.
     Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    :param request:
    :param discount_id:
    :return:
    r  r   r   r  Nr  r  r  
DiscountOnr  r  r`  r  r  r  r  TFr  r+  )r}  discount_formr?  r  r:  zdiscount_edit.html)rd   r   r   rc  r   r   r  r  rA  r#  r   r@   r  r  r  StandardImageBannerImager  r   re  r}  r   rA  r   )r   r?  r}  r;  r*  r  r  rw  r4  discount_onr  r<  r   r   r   discount_editA  sp   






rF  c           
      C   s  g }| j dkr| j }d|ddv r'|dd}|r!|d |d| d|ddv rB|dd}|r<|d |d| d|ddv r]|dd}|rW|d |d| d|ddv rx|dd}|rr|d |d| |dd}z"|d	kr|d= |d= |d= n|d
kr|d= |d= n|dkr|d= W n   Y t|| j}|	 r| jddrd|j
_| jddrd|j
_|  tddiS ||j td|jdS t }g }||dd	d}	t| d|	S )a  
    Discount  menu on Promotions section in the side bar will list the discounts.
    adding a discount.We have a add coupon discount. clicking on
    the add button will open a form. fields are discount_name and discount_code,
    discount_description, discount_on, applicable_category, applicable_subcategory,
    applicable_product, applicable_sku,
    discount_percentage.
    validations listed on the forms. applicable_category, applicable_subcategory,
    applicable_product, applicable_sku are dynamic fields based on discount_on field.
    discount is percentage based.
     Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    r   r   r  Nr  r  r  rA  r  r  r`  r  r  r  TFr  r+  )r}  rB  r  r:  zdiscount_add.html)r   r   r  r  rA  r#  r   r@   r  r  r  rC  rD  r  r   re  r}  r   )
r   r}  r*  r  r  rw  r4  rE  rB  r<  r   r   r   discount_add  sn   





rG  c                 C   r  )Nr  r  r  r  r  r  r  r  r  r$  r  r  r  rC  r  r  r  rD  r  r  r  r   r   r   image_field_style  r  rH  c              	   C   r  )aW  
    Ads menu on Promotions section in the side bar will list the ads.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete ads.
    These ads are used in carousel in the app and also the bottom banner.
    We are defining the placement of ads while creating.

    :param request:
    :return:
    rQ  r  rS  r   r+  )ad_datar  zad_list.html)re   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   adr[  rS  ad_list_datar<  r   r   r   ad_list     

rL  c                 C   r  )a<  
    Ads  menu on Promotions section in the side bar will list the ads.
    Deleting a ad. for each ads listed on
    ads list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the ad.
    :param request:
    :param ad_id:
    :return:
    r  list_ad)re   r   r   r  r   )r   ad_idrJ  r   r   r   	ad_delete&  r*  rP  c                 C   st  g }t jj|d }| jdkrt| j| j|d}| jdd}| jdd}| jdd}|	 r|j
dd	}|jdkrQztjj|d }||_W n6   Y n1|jdkrjztjj|d }	|	|_W n   Y n|jdkrztjj|d }
|
|_W n   Y | jd
drd|j_| jddrd|j_|
  tdS ||j nt|d}||d|jd}t| d|S )a  
    Ads menu on Promotions section in the side bar will list the ads.
    Editing a ad. for each ad listed, we have a edit button. clicking on
    the edit button will open a form. fields are placement_choices and AdTitle,
    AdDescription, AdPlacement, AdType, StandardImage,
    BannerImage, Discount,
    Coupon, Product.
    validations listed on the forms. Discount, Coupon, Product are dynamic fields based on AdType field.
    On submitting we can edit a ad already existing.
    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.
    :param request:
    :param ad_id:
    :return:
    r  r   r  r  r   rd   r`  Fr  r  Nr  rN  r+  r}  ad_formr  rI  zad_edit.html)re   r   r   rc  r   rA   r   r  r   r  r  AdTyperb   r  rd   rO   r`  r  rC  rD  r   re  r}  r   )r   rO  r}  rJ  r  r  r;  r  coupon_instancediscount_instanceproduct_instancer<  r   r   r   ad_edit8  sR   







rW  c                 C   sn  g }| j dkrt| j| j}| jdd}| jdd}| jdd}| r|jdd}|jdkrFztj	j
|d }||_W n6   Y n1|jdkr_ztj	j
|d }||_W n   Y n|jdkrwztj	j
|d }	|	|_W n   Y | jd	drd
|j_| jddrd
|j_|  tdS ||j ||d|jjd}
nt }g }||ddd}
t| d|
S )a  
    Ad  menu on Promotions section in the side bar will list the ads.
    adding a ad.We have a add ad button. clicking on
    the add button will open a form. fields are placement_choices and AdTitle,
    AdDescription, AdPlacement, AdType, StandardImage,
    BannerImage, Discount,
    Coupon, Product.
    validations listed on the forms. Discount, Coupon, Product are dynamic fields based on AdType field.
    Imp: image refresh and showing logic is handled via javascript.It is field wise and based on id.

    r   r  r   rd   r`  Fr  r  r  Nr  rN  r+  rQ  zad_add.html)r   rA   r   r  r   r  r  rS  rb   r   r   rc  r  rd   rO   r`  r  rC  rD  r   re  r}  r   )r   r}  r  r  r;  r  rJ  rT  rU  rV  r<  r   r   r   ad_add  s\   






rX  c                 C   s   g }| j dkrjt| j| j}| rc| }| jd}|D ]
}tjj	||d q| jd}|D ]
}t
jj	||d q1z| j}d}	d}
d|j d	|j d
}t||	|
| W tdS    Y tdS ||j nt }||dd}t| d|S )a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    adding a Custom Product.We have a Add Custom Product button. clicking on
    the add button will open a form. fields are item_name and item_code,
    item_description, veg_or_non_veg_status, min_size, max_size,
    size_unit, availability.
    Imp: Images are stored separately in CustomProductVideo and CustomProductImage.
    multiple image upload supported
    for validations please refer CustomProductForm


    r   r  custom_productr  r  rZ  r  r  zCustom Product AddedzCustom Product r  z added custom_product_listingr+  )r}  custom_product_formr  zcustom_product_add.html)r   rB   r   r  r  r  r  rX   r   r  rY   r   r  r  r  r   re  r}  r   )r   r}  r]  rZ  r  r  r  r  r  r  r   r   r<  r   r   r   custom_product_add  s8   
r^  c              	   C   r  )a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete custom products.
    custom product means, customer can define a product by directly contact
    with navya bakers.
    There is a section in the app to create a custom order. The admin can define the customization
    available products in the custom products section.
    :param request:
    :return:
    r  r  rS  r   r+  r  zcustom_product_list.html)rW   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   re  r[  rS  product_list_datar<  r   r   r   r\       

r\  c           	   
   C   s   t t|d}|}| jdkrM|  z| j}d}d}d|j d|j d}t|||| W td
S  tyL } zt	
d|  W Y d	}~td
S d	}~ww t| dd|iS )a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    Deleting a custom product. for each custom product listed on
    custom product list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the custom product.
    :param request:
    :param product_id:
    :return:
    r  r   r  zCustom Product deletedCustom product r  z	 deleted z)Error on Custom Product Delete(logging): Nr\  zdelete_confirmation.html)r   rW   r   r  r   r  r  r  r   r   r   r   r   )	r   r   r  re  r  r  r   r   r   r   r   r   delete_custom_product/  s$   

rb  c                 C   s  g }t t|d}t| jpd| jpd|d}| jdkru| ro| }| jd}|D ]
}t	j
j||d q*| jd}|D ]
}tj
j||d q=z| j}	d	}
d
}d|j d|j d}t|	|
|| W tdS    Y tdS ||j |||d}t| d|S )aR  
    Custom Products  menu on Products section in the side bar will list the custom products.
    Editing a Custom Product. for each custom product listed, we have a edit button. clicking on
    the edit button will open a form. fields are item_name and item_code,
    item_description, veg_or_non_veg_status, min_size, max_size,
    size_unit, availability.
        Imp: Images are stored separately in CustomProductVideo and CustomProductImage.
    multiple image upload supported
    for validations please refer CustomProductForm
    :param request:
    :param product_id:
    :return:
    r  Nr  r   r  rY  r  r[  r  zCustom Product Updatedr  r  z	 updated r\  )r}  r]  rZ  zcustom_product_edit.html)r   rW   rB   r   r  r   r  r  r  rX   r   r  rY   r   r  r  r  r   re  r}  r   )r   r   r}  rZ  r]  r  r  r  r  r  r  r   r   r<  r   r   r   custom_product_editT  s8   
rc  c           	      C   sr   t t|d}|jj}|  z | j}d}d}d|jj d|jj d}tj	j
||||d W n   Y td|d	S )
a  
        Custom Products  menu on Products section in the side bar will list the custom products.
        custom product have its associated images and videos which is not listed. when we try to
        add or edit a custom product it will be on form.
        These images and videos are stored in separate table and managed separately.
        access via id.
        Table: CustomProductImage

        We can remove the image with this api.
    :param request:
    :param product_id:
    :param image_id:
    :return:
    r  r  zCustom Product Image deletedra  r  z deleted its imager  rc  r0  )r   rX   rZ  r  r  r   r  r  r   r   r  r   )	r   r   rO  r  custom_product_idr  r  r   r   r   r   r   delete_custom_image  s   re  c           
   
   C   s   t t|d}|jj}|  z| j}d}d}d|jj d|jj d}t|||| W n t	yE }	 zt
d|	  W Y d}	~	nd}	~	ww td	|d
S )a  
    Custom Products  menu on Products section in the side bar will list the custom products.
    custom product have its associated images and videos which is not listed. when we try to
    add or edit a custom product it will be on form.
    These images and videos are stored in separate table and managed separately.
    access via id.
    Table: CustomProductVideo
    We can remove the video with this api.

    :param request:
    :param product_id:
    :param video_id:
    :return:
    r  r  zCustom Product Video deletedra  r  z deleted its videoz&Custom Product Video Delete(logging): Nrc  r0  )r   rY   rZ  r  r  r   r  r  r  r   r   r   r   )
r   r   rQ  r  rd  r  r  r   r   r   r   r   r   delete_custom_video  s   rf  c              	   C   rP  )at  
    Production unit means where products are manufacturing. Currently only one
    production unit available.
    production units  menu on Sales Unit section in the side bar will list the production unit.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete production unit. Only one production unit could be
    created now. creating more than one production unit is restricted. If no production unit created,
    a add button is available to create one else we hide that button.
    edit and delete button available for the production unit.

    :param request:
    :return:
    rQ  rR  rS  r   r+  )production_unit_datarU  zproduction_unit_list.html)rL   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   production_unitr[  rS  r<  r   r   r   rh    r`  rh  c                 C   sx   g }| j dkr+t| j}| r|  tdS ||j ||ddd}t| d|S t }g }||dd}t| d|S )a`  
    production units  menu on Sales Unit section in the side bar will list the production unit.
    adding a Production unit.We have a Add Production Unit button. clicking on
    the add button will open a form. fields are pu_name, pu_code, pu_location, street, city
    district, state_or_province, pin_code, latitude, longitude, gst, contact_no, email, status

    *NOt implemented yet (Only one production unit, not checking the status)
    We can change the status of pu by open, close or currently not accepting any order. it is by
    changing the status field.


    :param request:
    :return:
    r   production_unit_listr  r+  )r}  production_unit_formr  rU  zproduction_unit_add.html)r}  rj  rU  )	r   rC   r   r  r  r   re  r}  r   )r   r}  rj  r<  r   r   r   production_unit_add  s(   

rk  c                 C   r  )a  
    production units  menu on Sales Unit section in the side bar will list the production unit.
    Deleting a production unit. for each production unit listed on
    production unit list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the production unit.
    :param request:
    :param pu_id:
    :return:
    r  ri  )rL   r   r   r  r   )r   pu_idpur   r   r   production_unit_delete  r5  rn  c                 C   sn   t t|d}g }| jdkr't| j|d}| r |  tdS ||j	 nt|d}|dd}t
| d|S )a  
    production units  menu on Sales Unit section in the side bar will list the production unit.
    Editing a production unit. for each production unit listed, we have a edit button. clicking on
    the edit button will open a form. fields are pu_name, pu_code, pu_location, street, city
    district, state_or_province, pin_code, latitude, longitude, gst, contact_no, email, status
    :param request:
    :param pu_id:
    :return:
    r  r   r  ri  r+  )pu_formrU  zproduction_unit_edit.html)r   rL   r   rC   r   r  r  r   re  r}  r   )r   rl  rm  r}  ro  r<  r   r   r   production_unit_edit*  s   


rp  c                 C   s   t j }t| dd|iS )a  
    Faq's are the possible questions raised by the user.
    Faq  menu on Settings in the side bar will list the Faq.
    It is only available for super admin and nbc admin.
    nbc admin can create, edit and delete faq.
    add button is available to create.
    edit and delete button available for each faq listed.
    This created faq will render on the faq section on app.

    :param request:
    :return:
    zfaq.htmlfaqs)r!   r   r  r   )r   rq  r   r   r   faqE  s   
rr  c                 C   sP   | j dkrt| j}| r|  tdS t| d nt }t| dd|iS )z
    Faq  menu on Settings in the side bar will list the Faq.
    adding a Faq.We have a Add Faq button. clicking on
    the add button will open a form. fields are category, question,
    answer
    :param request:
    :return:
    r   rr  -There was an error with your form submission.zadd_faq.htmlr  )	r   r+   r   r  r  r   r   r   r   )r   r  r   r   r   add_faqX  s   

rt  c                 C   sf   t t|d}| jdkr%t| j|d}| r|  tdS t	| d nt|d}t
| d||dS )a  
    Faq  menu on Settings in the side bar will list the Faq.
    editing a Faq.We have a edit Faq button for each faq listed. clicking on
    the edit button will open a form. fields are category, question,
    answer
    :param request:
    :param faq_id:
    :return:
    r  r   r  rr  rs  zedit_faq.html)r  rr  )r   r!   r   r+   r   r  r  r   r   r   r   )r   faq_idrr  r  r   r   r   edit_faqp  s   

rv  c                 C   s   t t|d}|  tdS )a  
    Faq  menu on Settings in the side bar will list the Faq.
    deleting a Faq.We have a delete Faq button for each faq listed. clicking on
    the delete button will open a form. fields are category, question,
    answer
    :param request:
    :param faq_id:
    :return:
    r  rr  )r   r!   r  r   )r   ru  rr  r   r   r   
delete_faq  s   rw  c                 C   s  t j }tj }tj }tj }tj }tj }| jdkrd| j	v r=t
| j	| j|d}| r9|  td	S d| j	v rUt| j	|d}| rQ|  td	S d| j	v rmt| j	|d}	|	 ri|	  td	S d| j	v rt| j	|d}
|
 r|
  td	S d| j	v rt| j	|d}| r|  td	S d| j	v rt| j	|d}| r|  td	S t
|d}t|d}t|d}	t|d}
t|d}t|d}t| d
||||||||	|
|||dS )a+  
    About us describe about the Navya backers,
    Where there shops, contact details, app url,
    privacy polices, terms and conditions and what is their motto etc......

    each of this details are handled separately.
    we can edit each of this details.

    :param request:
    :return:
    r   edit_about_usr  edit_contact_usedit_app_urledit_privacy_policyedit_terms_conditionsedit_refund_policyabout_uszabout_us.html)r~  
contact_usapp_urlprivacy_policyterms_and_conditionsabout_us_formcontact_us_formapp_url_formprivacy_policy_formterms_conditions_formrefund_policyrefund_policy_form)r"   r   rc  r$   r#   r%   r'   r&   r   r   r(   r  r  r  r*   r)   r,   r0   r-   r   r   )r   r~  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r~    sv   




















r~  c                 C   P   t j }t| jp
d|d}| jdkr| r|  tdS t	| d||dS )aD  
    In about us section we have the privacy policy view, in addition to that
    we have a separate menu on the settings, Privacy Policy. on clicking
    it will list current privacy policy, we have edit button on the list.
    by clicking edit we can edit the current privacy policy.

    :param request:
    :return:
    Nr  r   r  zprivacy_policy.html)r  policy)
r%   r   rc  r,   r   r   r  r  r   r   )r   r  r  r   r   r   privacy_policy_view     
r  c                 C   r  )a\  
        In about us section we have the Terms and condition view, in addition to that
    we have a separate menu on the settings, Terms and condition. on clicking
    it will list current terms and condition, we have edit button on the list.
    by clicking edit we can edit the current terms and condition.

    :param request:
    :return:
    Nr  r   r  zterms_conditions.html)r  terms)
r'   r   rc  r0   r   r   r  r  r   r   )r   r  r  r   r   r   terms_conditions_view  r  r  c                 C   
   t | dS )zH
    * Not implemented yet........
    :param request:
    :return:
    delivery_settings.htmlr   r  r   r   r   delivery_settings  s   
r  c              
      s2  z| j d}tjdj| jjd}|d }g }| jd}| jd}| j dd}| j d	d}	| j d
d}
| j dd}|sF|}| j dd}|sQ|}t	jj|ddj
ddddddd}|r|dkr||tddtddB }n|tddtddB }| jddkr|r|t|dt|dB t|dB t|dB d}|	rt|	d }|j|d!}|
r|j|
d"}|r|r|d#kr|dkr||d$ }n||}n|dkr|d%| d$ }n|d%| }t|d&}| j d'}z||}W n  ttfy   |d(}Y n ty$   ||j}Y nw td) | j  d'v r6d'=  fd*d+|jjD }t }tjjd,|d-}||||	|
|d.||d/	}| jddkr{td0||||d1}td2d3|i}t||d4W S t| d5|}|W S  t y   g d.d6}t| d5| Y S w )7Nrf  r  r	  r   r  rg  r  r   r  r  Custom Orders)r   rB  r   rt  rZ  r   ru  custom_orderr  rw  rx  ry  rz  r{  r|  r  r  r}  r  r  r  r  r?  r  r  r  r  r  rS  r   custom_ordersc                    r  r  r  r  r  r   r   r2  p  r  z!custom_orders.<locals>.<listcomp>r:  r  r+  )	
order_formrv  r  r   r  r  r^  r  rf  zcustom_order_list_page.html)rv  r  r  rf  r  rv  rr  zcustom_orders.htmlr  )!rV  r   rJ   r   r-  r   r   r   r  r`   r  r  r4  rt   r  r   r  r   rW  r   rX  r   rY  rq   r  r[  r  rD   r]   rv   r   r   r  )r   rm  rf  r5  r  r}  r  rg  r  r  r  r  r  r  r  r[  rS  r  r  r  r  r<  rs  rt  responser   r  r   r  "  s   
	


r  c                 C   sN   | j d}|stdddddS tjjddd	}d
d |D }td|dS )Nr   ro  zDate not providedr(    r{   Tr+  is_availabler|   c                 S   s2   g | ]}|j |jd  d|jd  dqS )z%I:%M %pz - )r  label)r  r  strftimeend_time)r   r
  r   r   r   r2    s    z0get_available_delivery_slots.<locals>.<listcomp>r  )r|   slots)rV  r   r   rc   r   r   )r   selected_dater  	slot_datar   r   r   get_available_delivery_slots  s   r  c                 C   s  | j dkr(| jd}tjj|d }d}|s"tddddd	S t| j|d
}|	 s5td|j
dS |  | jd}| jd}| jd}d }d }	d }
|dkrq| jd}d }	d }
d|_d}|rjt|nd |_d |_nM|dkr| jd}	| jd}
d }d|_d}|	rt|	nd |_|
rztjj|
d}|j|_W n tjy   d |_Y nw d |_nd }d }	d }
d |_d |_tjj|d\}}||_||_||_|  d|_|  d}d| }t|jj||||j t|||jjgd d}td|jj  d|jj!  |jj!dkrt"j#|jj$||jj%|gd d! td"|j |j&d#S | j d$kr| j'd%}tjj|d& }|rt|d
}z	tjj|d}W n tjy[   d }Y nw d}|jdkrgd}nd}d }d }	|dkrx|j}d }	n
|dkr|j}	d }d }|dkr|jrztjj|jd' }|r|j(}W n   Y nnt }d }d}d }d }	d }tjjdd(d))d*}t*d+|||r|jnd|||||	|d,	}td|d-S tdd.dS )/Nr   r   r  r   ro  r  r(  i  r{   r  r&  r  r  r  r   r  r  rr  r  r  r  r  r  r  Bill Createdz#Bill Created for your custom order Fcustom_bill_createdzOpt-in status for user z: Tr   r   r  rl  rV  rm  r  )r  r+  r  r  zorder_status_update_form.html)	r  rm  r  r0  custom_trackingexisting_delivery_typer  r  selected_slot_idr  r'  )+r   r   r   r`   r   r   rc  r   rD   r  r}  r  rB  r}   r  r  rc   r  r=  r\   r*  r  r  r  r  r   r   r   r   r   r   r   r   r  r   r   r  r  r   rV  r  r4  rv   )r   r   r  r  r  r  r  r  r  r  time_slot_idr
  r  r  r   r   r  rm  r  r  r  available_slots	form_datar   r   r   custom_order_edit  s   




r  c                 C   r  )Nzsystem_settings.htmlr  r  r   r   r   system_settingsL  s   
r  c           	   
      s$  | j dkrzd|  I d H }| jd}| jd}tt	 |	 t
j }t||rat|}|d dd}|rXtjj|d }d|_|  tjj||d	 d
d tddiddW S tdddddW S  ty } ztddt|dddW  Y d }~S d }~ww tdddddS )Nr   zutf-8zX-Razorpay-Signaturenotesr   r   r  r  rm  paid)r   razorpay_order_idpayment_statusr|   r     r{   r   zPayment verification failedr(  r  zSomething went wrong!)r|   r   r   ro  zMethod not allowedr&  i  )r   jsonbodyrY  r  r   hmacnewRAZORPAY_WEBHOOK_SECRETrF  hashlibsha256	hexdigestcompare_digestloadsr`   r   r   rc  r  r  rf   r  r   r   r  )	r   r>  r  received_signaturegenerated_signature
event_datar   r  r   r   r   r   custom_order_paymentR  sL   



r  c                 C   sf  | j d}|dkr?dd tj D }z|r#tt||dddW S tg |dddW S  ty>   tg |ddd Y S w |dkrxd	d tj D }z|r\tt||dddW S tg |dddW S  tyw   tg |ddd Y S w |d
krdd t	j D }z|rtt||dddW S tg |dddW S  ty   tg |ddd Y S w d S )Nr   rd   c                 S      g | ]	}|j |jd qS )r  r   )r  DiscountNamer  r   r   r   r2        zchoice_list.<locals>.<listcomp>)tablestypeFru  r  c                 S   r  r  )r  
CouponNamer  r   r   r   r2    r  r`  c                 S   r  r  )r  r  r  r   r   r   r2    r  )
rV  r   rd   r   r  r   r0  StopIterationrb   rO   )r   r   discount_choicecoupon_choiceproduct_choicer   r   r   choice_list}  sF   r  c           	      C   s  t  }tjjg dd}g d}|D ]}z|jr7|jr7t|j|j}|	 t
 	  }| }|d }nd}|jdksC|jdkr|dk r]|jd	kr]|j|vr]d
|_t
 |_|  |dkrr|j|vrrd|_t
 |_|  |dk r|dkr|j|vrd|_t
 |_|  |dkrd|_t
 |_|  |jd	kr|dkr|j|vrd|_t
 |_|  |dk r|dkr|j|vrd|_t
 |_|  |dkrd|_t
 |_|  |jdkrd|_t
 |_|  W q   |jdkrd|_t
 |_|  Y qtddiS )N)r  r  r  r   r   )r  rj  r  r  r  r   r  r   r  r  r   r  Yellowr  r   r  r  r   r  StatusT)r   r  r`   r   r   r  r   r  r  r  rN  r  r  r  r  r  r   )	r   r  r  status_excludedr;  r  r  r  r  r   r   r   my_polling_task  st   















r  c                 C   sP   t jdddd}t|d}| jd}||}|dd}t	| d	|S )
Nr   r   message__adsrQ  r  rS  r+  )msg_datar  zcommunication_list.html)
rg   r   r  r-  r4  r   rV  r   rW  r   )r   r   r[  rS  msg_list_datar<  r   r   r   list_communication  s   


r  c                 C   sL  | j dkrt| j}g }| r| }tjjdd}|D ]}|jr|j	r|j
dkr{z2tjjddd }tjd|j d	|j d
|jj dgdd dddd}tjj||d W q tyz } ztd|  W Y d }~qd }~ww |j
dkrzBtjjddd }	|	jj}
d|j d|	j d
|	jj }|jg}tj}tj|t|	jj }t!|
||| tjj||d W q ty } ztd|  W Y d }~qd }~ww zt"d|jj|j|jj tjj||d W q ty } ztd|  W Y d }~qd }~ww qt#dS |$|j% nt }t&| dd|iS )Nr   r  r   r   r   )message_typerQ  zDear u+   ,
Surpricing offer for you from Navya🎉 

z+919961073407r   r   rC  r   rD  )r   r   zSMS Send Error: z,
we are providing
z#Email Send Error on Communication: z+917012951573z&Whatsapp Send Error on Communication: communication_listzsend_message.htmlr  )'r   rE   r   r  r  r  r   r   r   r  r  rh   r4  rc  r   r   r  r   rf  AdDescriptionrg   r  r   r   r   AdTitler   
MEDIA_ROOTospathr   r  rD  send_email_attachmentr   r   re  r}  r   )r   r  r}  msg_form	customerscustomersmsmsgr<  r   emailmsgr   
email_bodyto_email
media_rootfilepathr   r   r   send_message  sj   



r  c                 C   s(   t | |tj|d}|| |  d S )N)r   r  
from_emailto)r~   r   rn   attach_filer  )r   r  r  r  mailr   r   r   r  J  s   
r  r   c              
   C   s  z,| j dd }| j dd }| j dd }| j dd }| j dd }d|v r.|d d|v r7|d d|v r@|d d|v rI|d |dkrtjdd}|D ]}t|d |v red	|d
< qVd|d
< qVtjj|ddd}|D ]}	t|	d |v rd	|	d
< qwd|	d
< qwt	jj|ddd}
|
D ]}t|d |v rd	|d
< qd|d
< qt
jj|ddd}|D ]}t|d |v rd	|d
< qd|d
< qtt|t|t|
t||dddW S |dkrtjdd}|D ]}t|d |v rd	|d
< qd|d
< qtjj|ddd}|D ]}	t|	d |v r"d	|	d
< qd|	d
< qt	jj|ddd}
|
D ]}t|d |v rFd	|d
< q5d|d
< q5t
jj|ddd}|D ]}t|d |v rjd	|d
< qYd|d
< qYtt|t|t|
t||dddW S |dkr+tjdd}|D ]}t|d |v rd	|d
< qd|d
< qtjj|ddd}|D ]}	t|	d |v rd	|	d
< qd|	d
< qt	jj|ddd}
|
D ]}t|d |v rd	|d
< qd|d
< qt
jj|ddd}|D ]}t|d |v rd	|d
< qd|d
< qtt|t|t|
t||dddW S W d S  tyK } ztdt|dddW  Y d }~S d }~ww )Nr  r  r  r^  triggerr   r   r  r  checkedr  )category_id__inr  )item_category__inr  r  )product__item_category__inr   )
categoriesr  r  r^  r  Fru  )item_sub_category__in)product__item_sub_category__in)product_id__in)r|   r   )r>  r   rA  rM   r   r1  r  rN   r   rO   rV   r   r0  r   )r   category_idssub_category_idsproduct_idssku_idsr  r  r;  r  ry  r  kr^  r  r   r   r   r   coupon_subcategory_listU  s   















#"r  c              
   C   sj  | j dkrztjj|d }|jdkr1|dkrd|_n|dkr$d|_ntdddW S |  n"|jdkr>tdd	dW S |jdkrKtd
ddW S tdddW S d}d| d}t|j	j
||||j	 t|||j	jgd d}|j	jdkrtj|j	j||j	j|gdd tdd| dW S  ty } ztddt|dW  Y d }~S d }~ww tdddS )Nr   r  r  r  r   r   zStatus change not allowedr   z Status already updated to Failedr  z#Status already updated to DeliveredzOrder DeliveredzYour Order z Delivered!Fr  Tr   r   r   z Status of your order changes to zsomething went wrong)r|   r   r   zmethod not allowed)r   r`   r   r   rc  r  r   r  r   r   r   r   r   r  r   r   r  r  r   r  )r   r   r|   r   r   r   r  r   r   r   r    order_status_update_to_delivered  s8   




 r  c              	   C   r  )aq  
    Liked List menu on Product section in the side bar will list the You may also like list.
    It is only available for NBC admin.
    from Liked list we can add a product and we can map product related to that product.
    if one person orders a jam, liked products will contain bread it will be defined in
    the liked list

    :param request:
    :return:

    rQ  r  rS  r   r+  )	like_listr  zpeople_also_like_list.html)rZ   r   r4  r   rV  r   rW  r   rX  r   rY  r   )r   
liked_datar[  rS  r>  r<  r   r   r   you_may_also_like_list  rM  r  c                 C   r  )a8  
    Liked List menu on Product section in the side bar will list the You may also like list.
    Deleting a Liked Data. for each liked data listed on
    liked list, we have a delete button. clicking on
    the delete button will open a confirm dialog box. if you confirm
    it will delete the liked data.
    r  you-may-also-like)rZ   r   r   r  r   )r   r  you_may_also_like_datar   r   r   you_may_also_like_delete  rb  r   c                 C   s   g }t t|d}| j }d|dg v r(|dg }|r"|d |d| | jdkrFt||d}|	 r?|
  tdS ||j nt|d}|dd}t| d	|S )
a	  
    Liked List menu on Product section in the side bar will list the You may also like list.
    Editing a Liked data. for each liked data listed, we have a edit button. clicking on
    the edit button will open a form. fields are product and liked products.

    r  r   liked_productsr   r  r  r+  )	like_formr  zpeople_also_like_edit.html)r   rZ   r   r  r  rA  r#  r   rF   r  r  r   re  r}  r   )r   r  r}  r  request_postliked_product_listr  r<  r   r   r   you_may_also_like_edit&  s"   	




r  c                 C   s   g }| j  }d|dg v r"|dg }|r|d |d| | jdkrIt|}| r7|  t	dS |
|j ||dd}t| d|S t }||dd}t| d|S )a  
    Adding a liked product.
    Liked List menu on Product section in the side bar will list the You may also like list.
    clicking on add Liked list will open the form. fields are product and liked products.
    on submission create a liked product data.
    r   r  r   r  r+  )r}  r  r  zpeople_also_like_add.html)r   r  r  rA  r#  r   rF   r  r  r   re  r}  r   )r   r}  r  r  r  r<  r   r   r   you_may_also_like_addG  s0   	


r  c                 C   s   t j }|st j }tj d}| jdkr`d| jv r1t	| j|d}|
 r1|  tdS d| jv rGt| j}|
 rG|  tdS d| jv r_| jd}tjj|d	  tdS nt	|d}t }|||d
}t| d|S )a  
    delivery settings.
    delivery settings menu on Settings section in the side bar will list Delivery setting.
    delivery fee configuration, current delivery fee discount, add delivery discount option are
    listed on the delivery settings.
    :param request:
    :return:
    min_order_valuer   save_settingsr  r  add_discountdelete_discountr?  r  )settings_formrB  delivery_discountsr  )rj   r   rc  r  ri   r  r4  r   r   rG   r  r  r   rH   r   r   r  r   )r   r   	discountsr  rB  r?  r<  r   r   r   delivery_settings_viewt  s6   








r  c                 C   sh   t j }|st j }| jdkr%t| j|d}| r$|  t	dS nt|d}d|i}t
| d|S )a  
    custom coupon settings.
    Custom Coupon settings menu on Promotions section in the side bar will list Custom coupon list.
    if a customer buys a product between a price range and there is a
    coupon for that price range. A coupon code will send through the email, whatsapp or sms.
    settings view for entering the price range is here

    :param request:
    :return:
    r   r  zcoupon-settingsr!  zcustom_coupon_setting.html)rk   r   rc  r  r   rI   r   r  r  r   r   )r   coupon_settingsr  r<  r   r   r   coupon_settings_view  s   



r  )r   r   )F)Nr  (M  django.shortcutsr   r   django.contrib.authr   r   r   r   django.contrib.auth.decoratorsr   r   rest_framework.decoratorsr	   django.views.decorators.httpr
   django.forms.modelsr   django.db.modelsr   r   r   django.core.exceptionsr   r   razorpayr  r  r  twiliodjango.views.decorators.csrfr   r   r   r   signals.custom_signalsr   requestsr  	itertoolsr   r   django.confr   django.httpr   r   django.contribr   django.utilsdjango.core.paginatorr   r   r   django.appsr    
cms.modelsr!   r"   r#   r$   r%   r&   r'   formsr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   shops.modelsrJ   rK   rL   products.modelsrM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   accounts.modelsorders.modelsr\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   django.core.mailrm   NavyaBackers.settingsrn   r   r   ro   accounts.viewsrp   django.urlsrq   django.contrib.auth.modelsrr   rs   rt   ru   django.template.loaderrv   dalrw   rest_framework.permissionsrx   rE  rest_framework.viewsry   rest_framework.responserz   rest_frameworkr|   r   django.utils.dateparser}   r~   r  celeryr   adminportal.tasksr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r=  rD  rI  rO  r\  ri  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r/  r5  r9  r?  rC  rD  rF  rL  rP  rR  r_  rg  r  r  r  r  rw  r  r  r  r  r  r  r  r  r   r"  r$  r'  r)  r,  r.  r1  r3  r5  r7  r9  rW  r_  ra  rd  re  r  r  Select2QuerySetViewr  r  r  r  r  r  r  r  r  r  r-  r.  r2  r4  r8  r9  r=  r@  rF  rG  rH  rL  rP  rW  rX  r^  r\  rb  rc  re  rf  rh  rk  rn  rp  rr  rt  rv  rw  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r   r   r   r   <module>   s6   $@LH	n  B	
6:.
 *+BNX*:*)W

.2 c `	D  I    (W
KT
"
%$*I#"< x


)
Zl
C$ml#\WIP:#7#)G	u !*)NK

~&+4