
    d i@                       d dl mZ d dlmZ d dlmZ d dl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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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#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 d dl2Z3d dl4m5Z5 d dl6 ddl7m8Z8 d dlmZ d dl9Z9d dl m:Z: d dl;m<Z< d dl=m>Z> d dlmZ d dl?m@Z@ d dlAZAd dlBmCZC d dlDmEZEm	Z	 d dlDmEZEmFZFmGZG d dlHZHddlImJZJ d d lmKZK d d!lLmMZMmNZN d d"lOmPZP d d#lQmRZR d dlSZSd dlTZTd dlUZUd d$lVmWZWmXZX d d%lYmZZZ d d&lVm[Z[ d d'lm\Z\ d d(l]m^Z^ d d)l_m`Z` d dl m!Z! d d*lambZb ej                  Zdej                  Zfej                  Zh eHj                  ehedef+      Zjd,Zkd-Zld.Zmd/Znd0Zod1 Zpd2 Zqd3 Zr G d4 d5e      Zsd6 Zt G d7 d8e      Zu G d9 d:e      Zv G d; d<e      Zw G d= d>e      Zx G d? d@e      Zy G dA dBe      Zz G dC dDe      Z{ G dE dFe      Z| G dG dHe      Z} G dI dJe5      Z~ G dK dLe      ZdM Z G dN dOe      Z G dP dQe      Z G dR dSe      Z G dT dUe      Z G dV dWe      Z G dX dYe      Z G dZ d[e      Z G d\ d]e      Z G d^ d_e      Z G d` dae      Zej                  eA_         G db dce      Z G dd dee      Z G df dge      Z G dh die      Z G dj dke      Z G dl dme      Z G dn doe      Z G dp dqe      Z G dr dse      Z G dt due      Z G dv dwe      Z G dx dye      Z G dz d{e      Z G d| d}e      Z G d~ de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )APIView)ResponsestatusN)datetimedate	timedelta)settings)django_pbkdf2_sha256)get_authorization_header)RefreshToken)TokenBackend)
exceptions)renderredirectHttpResponse)Max)Q)	send_mail)r   r	      )ProductSampleEndUserShootsUserActivityInReviewProductDataReviewDataCommentsNotificationsCredSettingsCollectionsProductAddCollectionProductWatchers)ProductSampleSerializerEndUserSerializerProductSampleSerializerClient)PageNumberPagination)*)authenticated)EmailMultiAlternatives)render_to_string)
strip_tags)r   )timezone)BaseCommand)nowr	   )r-   
make_awareis_naive)send_comment_notification_email)F)CreateFolderError
SharedLink)ApiError)!CreateSharedLinkWithSettingsError)ExtractMonthExtractYear)defaultdict)
TruncMonth)Count)	Paginator)make_password)mailoauth2_refresh_tokenapp_key
app_secretz100.71.115.36   	lionsheadzScr@tch1424z!/volume1/Projects_In_Progress_LH2c                 B    | j                  dd      j                         S )N_ )replacetitle)
field_names    2/var/www/html/lionshead-backend/panel_api/views.pyhumanize_field_namerK   M   s    3$**,,    c                    |sg S g d}t        j                  dt        j                  |       dt         j                        }g }| D ]:  t	        fd|D              s|j                        s*|j                         < |S )z
	Filter image files that match the pattern: {tm_article_number}_XX.ext
	Example: If tm_article_number is 'L12101', matches 'L12101_01.png', 'L12101_02.png', etc.
	z.jpgz.jpegz.pngz.webp^z_\d+\.(jpg|jpeg|png|webp)$c              3   \   K   | ]#  }j                         j                  |       % y wNlowerendswith.0extfs     rJ   	<genexpr>z2filter_images_by_article_number.<locals>.<genexpr>^   s%     =,<S		C	 ,<   ),)recompileescape
IGNORECASEanymatchappend)filestm_article_numberimage_extensionspatternfiltered_filesrX   s        @rJ   filter_images_by_article_numberrg   P   s    
 		)6::299%6788RSUWUbUbc
Q=,<==mmA! 
 	rL   c                 ^   	 t        j                  t        t        t              }t        |       }|j                  d|      }g }|j                  D ]  }t        |t         j                  j                        s(	 |j                  |j                        }|j                  }t+        |d       |s_|j-                  d	d
      }|j/                  |j0                  |d        |S # t        $ r}t        |j                  t         j                   j"                        r`|j                  j%                         rF|j'                  |j                  d      }	|	j(                  r|	j(                  d   j                  }nd }n|Y d }~d }~ww xY w# t2        $ r}dt5        |      icY d }~S d }~ww xY w)Nr>   )url )pathshared_linkT)rk   direct_onlyr   z -------------file_urlfile_urlz?dl=0z?raw=1)nameri   error)dropboxDropboxREFRESH_TOKENAPP_KEY
APP_SECRETr3   files_list_folderentries
isinstancerb   FileMetadata(sharing_create_shared_link_with_settings
path_lowerri   r4   ro   sharingr5   is_shared_link_already_existssharing_list_shared_linkslinksprintrG   ra   rn   	Exceptionstr)

shared_urldbxrl   resultrb   entrylink_metadatafile_urleexisting_linkss
             rJ   get_dropbox_folder_contentsr   f   sr   '%	# z*+  bk B&
%~~e223AA%BRBRS]!!X 
(34  (3X
\\jj ) 2 
,'  
177GOOMMNSTSZSZSxSxSz44%:J:JX\4]n			 &&q)--xxg
* 	 
3q6	sO   A8F <'C&#F 23F &	F	/BF?F F		F 	F,F'!F,'F,c                       e Zd Zd Zy)UploadImagesc                 :   	 |j                   }|j                  d      }g }|D ]  }t        |      }|j                  |         t	        t
        j                  d|d      S # t        $ r0}t	        dt        |      it
        j                        cY d }~S d }~ww xY w)NimagesSuccessstatus_codestatus_messagedatamessager   )
r   getlistuploadTheProfilera   r   r   HTTP_200_OKr   r   HTTP_500_INTERNAL_SERVER_ERROR)selfrequestr   r   
image_urlsimage
image_pathr   s           rJ   postzUploadImages.post   s    	V
,,4LL"6:u!%(Jj!  &"4"4iWab
cc	 V
Is1v&v/T/T
UUVs   AA! !	B*%BBBN__name__
__module____qualname__r    rL   rJ   r   r          
VrL   r   c                     t        j                         j                  } d}t        j                  j                  d|        }g }|D ]S  }t        j                  | d|  |j                        }|s+|j                  t        |j                  d                   U |rt        |      dz   nd}t        |      j                  d      }| d| d|  S )NSDR-)job_number__endswithz-(\d+)-r      )r   r-   yearr   objectsfilterr[   r`   
job_numberra   intgroupmaxr   zfill)current_yearprefixexisting	sequencesobjr`   next_seqsequence_strs           rJ   generate_job_numberr      s    ##
 !!((,>P(Q S
((vhgl^4cnn
E%
CA'( 
 #,C	NQH##A&(!L><.11rL   c                       e Zd Zd Zy)UserRegisterc                 .   	 |j                   }|j                  d      }|j                  d      j                         j                  dd      j	                         }|j                  d      }|st        ddit        j                        S |st        dd	it        j                        S |st        dd
it        j                        S t        j                  |      }t        j                  j                  |      j                         }|rt        ddit        j                        S t        j                  j                  |||      }t        ddi      S # t         $ r0}	t        dt#        |	      it        j$                        cY d }	~	S d }	~	ww xY w)Nrn   emailrF   rj   passwordr   email is requiredr   name is requiredpassword is requiredr   zThe email is already registered)rn   r   r   z.You have been successfully registered with us.)r   getstriprG   rS   r   r   HTTP_400_BAD_REQUESThandlerhashr   r   r   firstHTTP_409_CONFLICTcreater   r   r   )
r   r   r   rn   r   r   new_passwordcheck_emailuser_objr   s
             rJ   r   zUserRegister.post   sR   T
,,4
((6
488G""$,,S"5;;=5hhz"8
Y236;V;VWW
Y12&:U:UVV
Y56f>Y>YZZ,,x(<''e'4::<;Y@AIaIabb%%4e\%RH
INO
PP	 T
Ic!f%V-R-R
SSTs7   B	E E +E 
A!E ,.E 	F$%F	FFNr   r   rL   rJ   r   r          TrL   r   c                       e Zd Zd Zy)	LoginUserc                 h   	 |j                   }|j                  d      j                         j                         }|j                  d      }|st	        ddit
        j                        S |st	        ddit
        j                        S t        j                  j                  |d      j                         }|d	k(  rt	        dd
it
        j                        S t        j                  j                  |      j                         }t        j                  j                  |d      j                         }|j                  }t        j                  ||      }	|	rt!        j"                  |      }
|j$                  |j&                  |j(                  |j*                  |j,                  d}t/        |
      t/        |
j0                        d}t	        d||d      S t	        ddit
        j2                        S # t4        $ r0}t	        dt/        |      it
        j6                        cY d }~S d }~ww xY w)Nr   r   r   r   r   r   Fr   	is_clientr   :This email does not exist in our database, please registerr   r   end_date__isnull)idrn   r   is_profile_completedis_password_resetrefreshaccessLogin Successfullyr   r   tokensInvalid password)r   r   r   rS   r   r   r   r   r   r   countHTTP_404_NOT_FOUNDr   r   r   verifyr   for_userr   rn   r   r   r   r   access_tokenHTTP_401_UNAUTHORIZEDr   r   r   r   r   r   r   useruserObjuserObj_deletecheck_passwordcheckrefresh_tokenallDatar   r   s                 rJ   r   zLoginUser.post   s   #T
,,488G""$**,5hhz"8
Y236;V;VWW
Y56f>Y>YZZ??!!%!@FFH4
aiY[\djd}d}~~oo$$5$17797__++%5+QWWY>$$>>>(>25 ))'2M**LL]]#88!33		G M"=--.F 37FSTTY12&:V:VWW	 T
Ic!f%V-R-R
SSTs8   A(G8 +G8 
AG8 C?G8 G8 8	H1%H,&H1,H1Nr   r   rL   rJ   r   r      s    $TrL   r   c                       e Zd Zd Zy)	ResendOTPc                    	 |j                   }|j                  dd      j                         j                         }|st	        ddit
        j                        S t        j                  j                  |      j                         }|st	        ddit
        j                        S t        j                  dd	d
      }||_        |j                          d}t!        dd|i      }t"        j$                  }t'        j(                  ||||g|       t	        d| |dt
        j*                        S # t,        $ r0}	t	        dt/        |	      it
        j0                        cY d }	~	S d }	~	ww xY w)Nr   rj   r   Email is requiredr   r   z*This email does not exist in our database.  '     zResend OTP - Forget Passwordforget_password_otp.htmlotphtml_messagezOTP resent successfully to r   r   )r   r   r   rS   r   r   r   r   r   r   r   r   random	randrangeforgot_password_otpsaver)   r
   EMAIL_HOST_USERr=   r   r   r   r   r   )
r   r   r   r   r   new_otpsubjectr   
from_emailr   s
             rJ   r   zResendOTP.post   sS   !V
,,488GR &&(..05
Y 34V=X=XYYoo$$5$17798
=>%%  dD!,7")8==?+7"#=w?OP<((:>>
G ,UG4 ! !
 
 V
Is1v&v/T/T
UUVs,   AD2 AD2 (B	D2 2	E+;%E& E+&E+Nr   r   rL   rJ   r   r      s    "VrL   r   c                       e Zd Zd Zy)userForgetPasswordc                 L   	 |j                   }|j                  d      j                         j                         }|st	        ddi      S t
        j                  j                  |      j                         }|dk(  rt	        ddit        j                        S t
        j                  j                  |      j                         }|rt        j                  dd	d
      }||_        |j                          |j                   }d}t#        dd|i      }	|	}
t$        j&                  }t)        j*                  ||
||g|	       t	        d| |d      S t	        ddi      S # t,        $ r;}t/        |       t	        dt1        |      it        j2                        cY d }~S d }~ww xY w)Nr   r   r   r   r   zPThis email does not exist in our database, please enter the valid email address.r   r   r   r   zForget Password OTPr   r   r   zOTP Send Successfully to r   zEmail not exists)r   r   r   rS   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   theotpto_emailr  r   plain_messager  r   s                rJ   r   zuserForgetPassword.post  s    U
,,4
((7

!
!
#
)
)
+5
Y2344			e		,	2	2	44
aiYqr  {A  {T  {T  U  Uoo$$E$288:8D$*F!'H MMO~~H#G#$>PL M))JNN7M:zP\]+E73   Y1233	 U	!HYs1v&f.S.STTUs2   AE 
AE B7E E 	F#(0FF#F#Nr   r   rL   rJ   r  r    s    !UrL   r  c                       e Zd Zd Zy)verifyForgetPasswordOTPc                    	 |j                   j                  d      j                         j                         }|s,t	        t
        j                  ddt
        j                        S |j                   j                  d      }|s,t	        t
        j                  ddt
        j                        S t        j                  j                  |      j                         }|s,t	        t
        j                  ddt
        j                        S t        j                  j                  ||	      j                         }|rt	        t
        j                  d
d      S t	        t
        j                  ddt
        j                        S # t        $ r?}t	        t
        j                  t        |      dt
        j                        cY d }~S d }~ww xY w)Nr   r   r   r   r   r   zotp is Requiredr   zEmail not exist)r   r   zSuccessfully VerifiedzInvalid otp)r   r   r   rS   r   r   r   r   r   r   existsr   r   r   r   r   )r   r   r   r   r   r   r   s          rJ   r   zverifyForgetPasswordOTP.post>  s   P<<G$**,2245
6#>#>Pcdlr  mH  mH  I  I			%	 3
V%@%@Rcdms  nI  nI  J  J'''6==?;
6#>#>Pabjp  kF  kF  G  Goo$$E$LRRT8F$6$6H_`aa6#>#>P]^fl  gB  gB  C  C	 P6#H#HZ]^_Z`aio  jO  jO  P  PPs9   A$F 'AF 0AF AF +F 	G4GGGNr   r   rL   rJ   r
  r
  =  s    PrL   r
  c                       e Zd Zd Zy)changeForgetPasswordc                 `   	 |j                   j                  d      j                         j                         }|st	        ddit
        j                        S t        j                  j                  |      j                         }|st	        ddit
        j                        S |j                   j                  d      }|st	        ddit
        j                        S |j                   j                  d	      }|st	        dd
it
        j                        S ||k7  rt	        ddit
        j                        S t        j                  |      }||_        |j!                          t	        ddi      S # t"        $ r0}t	        dt%        |      it
        j&                        cY d }~S d }~ww xY w)Nr   r   r   r   r   user not foundr   znew password is requiredconfirm_passwordzconfirm password is requiredz+Password and confirm password are not same.zChanged Password Successfully)r   r   r   rS   r   r   r   r   r   r   r   r   HTTP_403_FORBIDDENr   r   r   r   r   r   r   )r   r   r   r   r   r  encrypt_passwordr   s           rJ   r   zchangeForgetPassword.postT  sk   T<<G$**,2245
Y 34V=V=VWWoo$$5$17798
Y 01&:S:STT,,"">2<
Y9:&B]B]^^ll&&'9:
Y=>fFaFabb&&YLMU[UnUnooll#34'8==?
I=>
??	 T
Ic!f%V-R-R
SSTs=   AE4 AE4 %9E4 9E4 !E4 ;8E4 4	F-=%F("F-(F-Nr   r   rL   rJ   r  r  S  r   rL   r  c                       e Zd Zd Zy)LoginThroughProxyc                    	 |j                   }|j                  d      }|j                  d      }|r|st        ddit        j                        S ||d}d}t        j                  ||      }|j                  d	k(  r)t        |j                         t        j                        S t        |j                         |j                        S # t        $ r0}t        dt        |      it        j                        cY d }~S d }~ww xY w)
Nr   r   r   z Email and Password are required.r   )r   r   z/http://95.111.228.198:9006/api-panel/user-login)json   )r   r   r   r   r   requestsr   r   r  r   r   r   r   )	r   r   r   r   r   
login_data	login_urlresponser   s	            rJ   r   zLoginThroughProxy.postl  s    V
,,488G5hhz"8
xY BCFLgLghh : A9mmIJ78 c!HMMOF,>,>??HMMOH,@,@AA	 V
Is1v&v/T/T
UUVs+   AC AC '$C 	D%D :D DNr   r   rL   rJ   r  r  k      VrL   r  c                       e Zd Zd Zy)ResetPasswordc                 8   	 |j                   }|j                  dd      j                         j                         }|j                  d      }|j                  d      }|j                  d      }|st	        ddit
        j                        S t        j                  j                  |	      j                         }|st	        dd
it
        j                        S |st	        ddit
        j                        S t        j                  ||j                        st	        ddit
        j                        S |st	        ddit
        j                        S |st	        ddit
        j                        S ||k7  rt	        ddit
        j                        S t        j                  |      }||_        |j!                          t	        ddit
        j"                        S # t$        $ r0}	t	        dt'        |	      it
        j(                        cY d }	~	S d }	~	ww xY w)Nr   rj   old_passwordr   r  r   r   r   r   User not foundzOld password is requiredzOld password is incorrectzNew password is requiredzConfirm password is requiredz&New and confirm passwords do not matchzPassword updated successfully)r   r   r   rS   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   hashed_passwordr   s
             rJ   r   zResetPassword.post  s   %V
,,488GR &&(..05((>*<((>*<hh12
Y 34V=X=XYYoo$$5$17798
Y 01&:S:STT
Y :;FD_D_``
..x'8'8
9Y ;<VE`E`aa
Y :;FD_D_``
Y >?HcHcdd&&Y HIRXRmRmnn\\,/?&8==?
I>?HZHZ
[[	 V
Is1v&v/T/T
UUVsJ   BG  AG  G  :<G  7G  G  5!G  AG   	H)%HHHNr   r   rL   rJ   r   r     s    &VrL   r   c                       e Zd Zd Zd Zy)UpdateUserProfilec           	         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  |j                  |j                  |j                  |j                  |j                   |j"                  |j$                  d}t        t        j&                  d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j(                        cY d }~S d }~ww xY w)Nr   r   r   r  )r   rn   r   phone_numberr   r   r   is_adminFetched Successfullyr   )r'   r   r   r   r   r   r   r   r   r   r   r   rn   r   r)  r   r   r   r*  r   r   r   r   uidr   r   all_datas         rJ   r   zUpdateUserProfile.get  s   TL

 C oo$$$,2248
Y 01&:S:STT	==NN((NN 22""  8 &"4"4F\dlm
nn%  LYs1v&f.J.JKKL& 
 T
Ic!f%V-R-R
SSTsN   C AD A7D 	D%DDD DD 	E%E=EEc           	         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        t        j                  dd      S |j                  j                  d      }|st        t        j                  d	d      S t        j                  j                  |
      j                  |j                        }|rt        t        j                  dd      S |j                  j                  d      }|st        t        j                  dd      S t        j                  j                  |      j                  |j                        }	|	rt        t        j                  dd      S |j                  j                  d      }
||_        ||_        ||_        |
|_        |j)                          |j                  |j                   |j"                  |j$                  |j&                  |j*                  |j,                  |j.                  d}t        t        j0                  d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j2                        cY d }~S d }~ww xY w)Nr   r   r(  r  rn   r   r  r   r   r   3This email is already associated with some account.r)  phone_number is requiredr)  :This phone number is already associated with some account.r   )r   rn   r   r)  r   r   r   r*  Profile updated successfullyr   )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   excluder   rn   r   r)  r   r   r   r   r*  r   r   r   r   r-  r   r   rn   r   r   r)  check_numberr   r.  s               rJ   r   zUpdateUserProfile.post  s   4TL

 C oo$$$,2248
Y 01&:S:STT
,,

6
"4
6#>#>Pbcdd<<G$5
6#>#>Pcdee''u'5===M;6#>#>  QF  G  H  H,,"">2<
6#>#>Pjkll //(((EMMQYQ\Q\M]<6#>#>  QM  N  O  O<<G$58=8>'88>==?	==NN((NN""!33!!8 &"4"4Fdltu
vv_  LYs1v&f.J.JKKL` 
 T
Ic!f%V-R-R
SSTsn   I5 AJ1 8J1 8J1 AJ1 %8J1 AJ1 6B>J1 5	J.>%J)#J.$J1 )J..J1 1	K*:%K%K*%K*Nr   r   r   r   r   r   rL   rJ   r&  r&    s    T65TrL   r&  c                   $    e Zd ZdZdZdZd Zd Zy)CustomProductPagination
   	page_sized   c                     |j                   j                  | j                  | j                        }	 t	        |      }|| j                  kD  r| j                  }|S # t
        $ r | j                  }Y 4w xY wrQ   )query_paramsr   page_size_query_paramr<  r   
ValueErrormax_page_size)r   r   r<  s      rJ   get_page_sizez%CustomProductPagination.get_page_size  sm    ""&&t'A'A4>>R)9~9
 ###!!9	 
 ~~9s   A A21A2c                    t        dd| j                  j                  j                  | j                  j                  j                  | j                  j
                  | j                         | j                         |d      S )Nr  zProducts fetched successfully)r   r   total_itemstotal_pagescurrent_pagenextpreviousresults)r   page	paginatorr   	num_pagesnumberget_next_linkget_previous_link)r   r   s     rJ   get_paginated_responsez.CustomProductPagination.get_paginated_response  sh    	4))%%++))%%//99##%%'	 	
 	rL   N)r   r   r   r<  r@  rB  rC  rQ  r   rL   rJ   r:  r:    s    $
rL   r:  c                       e Zd Zd Zy)GetProductListingc                 T   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  dd       }|j                  j                  d      }|r0t        j                  j                  d      j                  d	      }n/t        j                  j                  d
      j                  d	      }|j                  j                  d      }|r/t        j                  j                  d      j                  d	      }|j                  j                  dd       }	|	r |j                  |	      j                         }|r|j                  t!        |      t!        |      z  t!        |      z  t!        |      z  t!        |      z  t!        |      z  t!        |      z  t!        |      z  t!        |      z        }|j                  j                  dd       }
|j                  j                  dd      }|
r+|dk(  r|j                  |
      }n|j                  d|
       }t#               }|j%                  ||      }t'        |d      }|
||	|j(                  d}|j+                  |      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j,                        cY d }~S d }~ww xY w)Nr   r   r(  r  search
is_archiveT)rV  -idFis_drop)
is_droppedcollection_name)<addcollectionproduct__collection__collection_name__icontainsproduct_line__icontainssubfamily__icontainstm_article_number__icontainsstyle_name__icontainsog_style_name__icontainscolor_name__icontainsseason__icontainsjob_number__icontainslocation__icontainssort_by
sort_orderascr   many)rn  ro  rZ  rJ  )r'   r   r   r   r   r   r   r   r   r   r   r?  r   r   order_bydistinctr   r:  paginate_querysetr"   r   rQ  r   )r   r   r-  r   r   rU  rV  samplesrX  rZ  rn  ro  rL  result_page
serializerresponse_datas                   rJ   r   zGetProductListing.get,  s   >TL

 C oo$$$,2248
Y 01&:S:STT  $$Xt46$$((6:##***=FFuMG##***>GGNG!!%%i07##***=FFuMG ))--.?F?nnBQ  hj  nnv&F#$F+, V$% '	(
 V$%  ! V$% 6"#
G !!%%i67$$((u=: U(W!G9.W&(9,,Wg>;'$?: &	= 
*
*=
99q  LYs1v&f.J.JKKLt 
 T
Ic!f%V-R-R
SSTsN   J2 AK. IK. 2	K+;%K& K+!K. &K++K. .	L'7%L"L'"L'Nr   r   r   r   r   rL   rJ   rS  rS  +  s    ?TrL   rS  c                 x    	 d}d}t        j                  | |      }|j                  |      S # t        $ r Y y w xY w)Nz%m/%d/%yz%Y-%m-%d)r   strptimestrftimerA  )date_strcurrent_formatdesired_formatdate_objs       rJ   convert_dater  n  sH    
.. x8(			>	** 	s   *- 	99c                       e Zd ZddZy)UploadProductSampleNc           
      V   |j                   j                  d      }|st        ddit        j                        S |j
                  j                  d      }d }|rQt        j                  j                  |      j                         }|s!t        dd| dit        j                        S 	 |j                  j                  d	      rt        j                  |      }nN|j                  j                  d
      rt        j                  |      }nt        ddit        j                        S |j#                         D ]_  \  }}	|	j                  d      }
|
s|	j                  d      }|rt%        |      }nd }t'               }i d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|d|	j                  d      xs d d|	j                  d      d |	j                  d!      d"|	j                  d#      d$|	j                  d%      d&|	j                  d'      d(|	j                  d)      d*|	j                  d+      d,|	j                  d-      d.|	j                  d/      |	j                  d0      |	j                  d1      |	j                  d2      d3|d4}t(        j                  j+                  |
|5      \  }}|s	 t,        j                  j                  |6      j                         }|rB|j.                  }|j0                  |j0                  k7  r>t3        d7|
 d8|j4                   d9       n!t,        j                  j7                  ||:       b t        d=d>it        j8                        S # t        $ r3}t        ddt!        |       it        j                        cY d }~S d }~ww xY w# t        $ r%}t3        d;|
 d<t!        |              Y d }~d }~ww xY w)?Nfilero   zNo file uploadedr   collection_idr(  zCollection with id 
 not foundz.csvz.xlsxzUnsupported file formatzError reading file: zTM ARTICLE NUMBERzPAR Dateproduct_linezPRODUCT LINE	subfamily	SUBFAMILY
style_namez
STYLE NAMEog_style_namezOG STYLE NAME
color_namezCOLOR  NAMEseasonSEASONpar_datenumber_of_samples_receivedzNumber of Samples Receivedmarketing_sample_estimated_datezMarketing Sample Estimated Daterep_sms_photoshop_commentszRep SMS/Photoshop CommentslocationLOCATIONsample_pass_off_datezSample Pass off Datelionshead_received_datezLionshead Received Dateflatlay_shoot_datezFlatlay Shoot Dateflatlay_shootzFlatlays Shot (Y or N)on_model_shoot_datezOn Model Shoot Dateon_models_shotzOn Models Shot (Y or N)zSample Return DatezLast Updated DateTimez
Updated ByF)sample_return_datelast_updated_datetime
updated_byis_product_shotr   )rc   defaultsproductzWarning: Product z is already in collection z , skipping collection assignment
collectionr  zError adding product z to collection: r   z.File uploaded and data processed successfully.)FILESr   r   r   r   r   r   r   r   r   rn   rT   pdread_csv
read_excelr   r   iterrowsr  r   r   update_or_creater    r  r   r   rZ  r   HTTP_201_CREATED)r   r   formatr  r  collection_objdfr   rE   rowrc   r  r   r  product_sampleexisting_collection_productexisting_collections                    rJ   r   zUploadProductSample.post}  s;   			6	"$	
G/09T9T
UU ,,""?3-.&..555GMMO>
W 3M?*MNW]WrWrssc
ii 	T	B		7#	t	BW78A\A\]]
 faww23
 ggj!8H%HH#%:CGGN+% #'',' SWW_-	
 #''-( cggh  !#''*F"G"O4 &sww/P'Q !#''*F"G 
# CGG$:; sww'@A #''"67  SWW56!" 377#89#$ cgg78%& ''"67 WW%<='','/84 %,,==' > >1 P#7#?#?#F#F~#F#^#d#d#f #7BB			>#4#4	4 !2 33MNaNqNqMr  sS  T  U ""))^^)\s ~ 
9NOX^XoXo	ppG 
 c
G3CF8<=fFaFa
bbc|  P
"#4"55Ec!fXNOOPs8   ,A>N; BO:;	O7(O2,O72O7:	P(P##P(rQ   r   r   rL   rJ   r  r  |  s	    XqrL   r  c                       e Zd Zd Zy)GetProductDetailc           	         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S 	 t        j                  j                  |      }t!        |      }g }|j"                  rt%        |j"                        }g }	t&        j                  j                  |
      j                         }
|
r|
j(                  r|
j(                  j*                  }t-        |d       t/        j0                         }|j3                  t/        j4                                	 |j7                  t8        t:        t<        t>        d       |jA                         }tB         d| d}t-        |d       	 |jE                  |      }t-        |d       tI        ||jJ                        }t-        |d       tL        jN                  jQ                  tR        jT                  d|jV                  xs |      }tM        jX                  |d       t-        |d       |D ]  }| d| }tL        jN                  jQ                  ||      }tL        jN                  j[                  |      s|j                  ||       tR        j\                   d|jV                  xs | d| }|	j_                  |        t-        |	d       	 	 |jc                          |jc                          td        j                  j                  |d      }g }|D ]Z  }|jf                  }|s|j_                  |jh                  |jj                  |jl                  |jn                  |jp                  d       \ tr        j                  j                  |
      }g }|D ]-  }s|j_                  |jh                  |jl                  d       / tt        j                  j                  |      }g }|rG|D ]B  }|jh                  |jv                  |jx                  |jz                  d}|j_                  |       D |j                  } || d<   |	| d <   || d!<   || d"<   || d#<   t        | t        j|                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        j                  $ r  t        dd	it        j                        cY S w xY w# tF        $ r g }Y Sw xY w# t        $ r[}t        ddt        |       it        j`                        cY d }~	 jc                          |jc                          S #  Y S xY wd }~ww xY w#  Y qxY w# 	 jc                          |jc                          w #  Y w xY wxY w# t        $ r0}t        dt        |      it        j`                        cY d }~S d }~ww xY w)$Nr   r   r(  r  article_numberro   article_number is requiredrc   Product not foundr  z ------------collection_nameFportusernamer   look_for_keys//Client Originalsz -----------nas_folder_pathz ----------filesz --------------image_files
nas_imagesTexist_okz ----------local_dirnas_images/z ------------nas_filesFailed to retrieve NAS files: )r  
is_watcher)r   rn   r   r)  r   )r   r   r   )r   r   r   
start_datedropbox_files	nas_fileswatchershistorymain_watchers)?r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   DoesNotExistr"   dropbox_urlr   r    r  rZ  r   paramiko	SSHClientset_missing_host_key_policyAutoAddPolicyconnecthostnamer  r  r   	open_sftpremote_base_pathlistdirFileNotFoundErrorrg   rc   osrk   joinr
   
MEDIA_ROOTnas_folder_namemakedirsr  	MEDIA_URLra   r   closer   r   r   rn   r   r)  r   r!   r   r   r   r  r   )!r   r   r-  r   r   r  r  rx  r  r  collection_productrZ  sshsftpnas_folder_pathrb   image_files	local_dirfilenameremote_path
local_path	media_urlr  watcher_userswatcherr   r  main_watcher_usersr  history_datahisr.  ry  s!                                    rJ   r   zGetProductDetail.posti  sX   }VN

 C oo$$$,2248
Y 01&:S:STTLL$$%56>
W:;FD_D_``V##''.'IG (0: =/0C0CDM 9,44;;G;LRRT/::(33CCO	/89



C##H$:$:$<=&[[x(Z_[`MMOT ++1_,==NO_
?89ll?+e 5#$25':S:ST[
;34h11<AXAXAk\klY[[T*
9+, &'q
3k77<<	84j WW^^J'xxZ( ''(G4K4K4^3__`ai`jkiy! ! 9-.
jjl	iik nn##G#E8=w<<DGGiizz''zz   ##**7*;=wJJ}}     !!((n(E7<66>>>>	X "  ??=$1=! )=,=*=$6=! =););
<<m  NYA'0L0LMMN 
$
$ VW126;T;TUUV<  e,  yg!?AxHIRXRwRwxx
jjl	iik
y
	
jjl	iik
^ 
 V
Is1v&v/T/T
UUVs$  R1 AW 9W  S- 6CW >AT5 T# D0T5  V 06W 'A4W CW 1	S*:%S%S* W %S**W -0T W T  W #T2.T5 1T22T5 5	V>(V&V'V$ , VVW W VV$ V!W $W& WWW	WW 	X
%X?X
X
Nr   r   rL   rJ   r  r  h  s	    ~VrL   r  c                       e Zd Zd Zy)ChangeProductStatusc           
      L   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }t        st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |	      j                         }|st        dd
it        j                        S ||_        |j                          |dk(  rNt        j                  j                  d      }d}	||d|j                   d}
t!        d|
      }t#        |      }n|dk(  rg }t$        j                  j                  |      j                         }|r|j&                  r|j&                  j(                  }t+        j,                         }|j/                  t+        j0                                	 |j3                  t4        t6        t8        t:        d       |j=                         }t>         d| d}	 |jA                  |      }tC        d|       tG        ||j                        }tC        d|       d|jH                   d}	 |jK                  |       |D ]p  }| d| }| d| }	 |jM                  |d      5 }|jO                         }d d d        |jM                  |d      5 }|jQ                         d d d        tR        jT                  }tV        jX                  jZ                  j]                  d |i       |jM                  |d      5 }d|jH                   d!| }d|jH                   d}tV        j_                  |jO                         |d"       tV        ja                  |      jb                  } | |_2        |j                          d d d        tR        jf                   d#|jH                   d| }!|ji                  |!       s 	 	 |jm                          |jm                          |jn                  }"t        j                  j                  dd(      }tp        j                  j                  |")      js                  d*      }#tt        j                  j                  |")      js                  d*      }$tC        tp        jv                  jy                         D cg c]  }|jz                   c}       tC        tt        jv                  jy                         D cg c]  }|jz                   c}       i }%|D ]  }&|&jz                  |%|&j|                  <    |#D ]4  }'|'j~                  }&|&j                  s|&jz                  |%|&j|                  <   6 |$D ]4  }(|(j~                  }&|&j                  s|&jz                  |%|&j|                  <   6 tC        d+|%       d,}	||d-}
t!        d.|
      }t#        |      }nK|jn                  }"t        j                  j                  dd(      }tp        j                  j                  |")      js                  d*      }#tt        j                  j                  |")      js                  d*      }$i }%|D ]  }&|&jz                  |%|&j|                  <    |#D ]4  }'|'j~                  }&|&j                  s|&jz                  |%|&j|                  <   6 |$D ]4  }(|(j~                  }&|&j                  s|&jz                  |%|&j|                  <   6 d/}	||d|j                   d}
t!        d0|
      }t#        |      }%j                         D ]Q  \  })}*tC        d+|)       t        |	|tR        j                  |)g1      })|)j                  |d2       |)j                          S t        t        j                  d3d4      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# tD        $ r g }tC        d       Y w xY w# t        $ r}tC        d|        Y d }~d }~ww xY w# 1 sw Y   txY w# 1 sw Y   UxY w# 1 sw Y   xY w# t        $ r%}tC        d$| d%t        |              Y d }~d }~ww xY w# t        $ r[}t        d&d't        |       it        jj                        cY d }~	 jm                          |jm                          S #  Y S xY wd }~ww xY w#  Y xY w# 	 jm                          |jm                          w #  Y w xY wxY wc c}w c c}w # t        $ r0}t        dt        |      it        jj                        cY d }~S d }~ww xY w)5Nr   r   r(  r  r   status is requiredr   zjob number is requiredr  zno product foundRetouching DoneTr*  u2   Retouching process completed – Lionshead Studios+https://lionshead.tidera.ai/admin/products/)r   product_statusri   zretouch_complete_email.htmlReview Successr  Fr  r  r  z!Files in Client Originals folder:z!Client Originals folder not foundz'Filtered image files by article number:/Projects_In_Progress_LH2//Final FileszError creating final folder: rbwbDropbox-API-Select-User/Final Files/muter  zError processing file : ro   z'Failed to retrieve and move NAS files: )r*  email_verified
product_idr   z---------------------u=   Success:Client Review Process Completed – Lionshead Studios)r   r  review_complete_email.htmlu?   Requested Changes :Client Changes Request – Lionshead Studioszrequest_changes_email.htmlr  bodyr  to	text/htmlStatus updated successfullyr  )Gr'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rc   r)   r*   r    r  rZ  r  r  r  r  r  r  r  r  r   r  r  r  r   r  rg   r  mkdiropenreadwriter
   DROPBOX_USER_IDr   _sessionheadersupdatefiles_uploadry   ri   r  r  ra   r   r  r   r!   prefetch_relatedr   _meta
get_fieldsrn   r   r   r  itemsr(   r   attach_alternativesendr   )+r   r   r-  r   r   r  r   product_objadmin_usersr  contexthtml_contenttext_contentr  r  rZ  r  r  r  rb   r  final_folder_pathr  r  final_remote_pathsource_file	file_datadestination_fileuser_idrX   dropbox_pathdropbox_folder_path
folder_urlr  r  r  shootsemail_user_mapr   r  shootr   rn   s+                                              rJ   r   zChangeProductStatus.post  s   OTL

 C oo$$$,2248
Y 01&:S:STTLL$$X.>
Y 45f>W>WXX  .:
Y 89&B[B[\\&&--:-FLLN;
Y 23F<U<UVV&;)) //(($(7KBG%9+:W:W9XYG $$A7KLl+L**I .55<<[<QWWY0;;)44DD_S$$X%;%;%=>?	kk(8[`ka]]_d ,,Ao->>OPo2||O,u0%8 4E;;X;XYk5{C6{7R7R6SS_`2zz#$ "('((4{/0(<=YY{D)[ %%' * YY($/3C					* 0 **##%>$HIYY($/1K778hZP!";#>#>"?|L				!&&(Lt		<BBCVW[[#-	 				 0  ))*+k6Q6Q5RRST\S]^	#5 "Dzz|
yy{ J//(($t(LK &&*&=NNvVH ^^""j"9JJ6RF	8>>446
76a1666
78	6<<224
54a1664
56 N "&))^DJJ  LLT#'99nTZZ   JJT#'99nTZZ   

!.1MG%G $$@'JLl+L J//(($t(LK &&*&=NNvVH ^^""j"9JJ6RF N "&))^DJJ  LLT#'99nTZZ   JJT#'99nTZZ   PG%9+:W:W9XYG
 $$@'JLl+L %**,{ud	
!%("
((		E 
\;7	JJL - &"4"4Fcd
eeU  LYs1v&f.J.JKKLd  2u012  2,QC0112 *) 0/ 0/  =&xj3q6(;<<=  Cw"I#a& RS\b  ]B  ]B  C  Czz|
yy{C	zz|
yy{ 8
5n 
 T
Ic!f%V-R-R
SSTs  _. Ae* =e* 9e* Ae*  C=e* Ac !`* >2c 1a c b)a,:ba9&Ab;A?b:;b5c : d+ B0e* 
e ,e* 	e%Ae* #8e* De* .8e* 'Ce* .	`'7%`"`'e* "`''e* *a c ac 	a)a$c $a))c ,a6	1b9b	>bb	b	cb<6c <cc 	d((d#5d(6d3 ; dd e*  e* #d((d3 +d0-e* 3e5 eeeee* *	f#3%ff#f#Nr   r   rL   rJ   r  r    s	    PTrL   r  c                       e Zd Zd Zy)	GetShootsc           
         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  dd       }t        j                  j                  d      j                  d      }|rv|j                  t        |	      t        |
      z  t        |      z  t        |      z  t        |      z  t        |      z  t        |      z  t        |      z        }|j                  d      j                  d      }|j                  d      j                  d      }|j                  d      j                  d      }	|j                  d      j                  d      }
|j                  d      j                  d      }t!        |d      j"                  }t!        |d      j"                  }t!        |	d      j"                  }t!        |
d      j"                  }t!        |d      j"                  }t        dd|||||ddt        j$                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j&                        cY d }~S d }~ww xY w)Nr   r   r(  r#  rU  F)status__isnullrW  r\  r^  r`  rb  rd  rf  rh  rl  	scheduled)status__iexactorder_indexin_progress
retouching	revisionsfinalTrq  r  Fetched successfully)r/  r2  r3  r4  r5  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   rU  rv  scheduled_samplesin_progress_samplesretouching_samplesrevisions_samplesfinal_samplesscheduled_serializedin_progress_serializedretouching_serializedrevisions_serializedfinal_serializeds                    rJ   r   zGetShoots.get  s   ;VN

 C oo$$$,2248
Y 01&:S:STT  $$Xt46 "")) * Xe_ 
 nnv&F#$F+, V$% '	(
 V$%  ! 6"#	G ~~[~AJJ=Y }ENN}]lCLL][~~[~AJJ=Y>>>9BB=Q= 22C$OTT34GdSXX23EDQVV12C$OTT-m$GLL
,&*(&
 
! 
!U  NYA'0L0LMMNl 
 V
Is1v&v/T/T
UUVsN   I AJ G?J 	J$%J	J
J JJ 	K %KKKNrz  r   rL   rJ   r,  r,    s    <VrL   r,  c                       e Zd Zd Zy)GetAssignUsersc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  t        |j                        t        d      z  t        d      z        }t        |d      }t        d	|j                  it        j                         S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j"                        cY d }~S d }~ww xY w)
Nr   r   r(  r  Tr   r  rq  users)r'   r   r   r   r   r   r   r   r   r   r   r5  r   r   r#   r   r   r   )r   r   r-  r   r   other_usersserialized_userss          rJ   r   zGetAssignUsers.get  s   TL

 C oo$$$,2248
Y 01&:S:STT((hkk):1t;L)LQX\M])]^;'$?
G-223F<N<N
OO  LYs1v&f.J.JKKL 
 T
Ic!f%V-R-R
SSTsN   C AD B D 	D%%D
DD DD 	E!%EEENrz  r   rL   rJ   rB  rB    s    TrL   rB  c                       e Zd Zd Zy)
AddProductc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  }|j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }|r3t        d|j                  |j                   d	t        j                        S t#               }t        j                  j%                  ||j                  d
      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  dd      |j                  d      |j                  d      |j                  d       |j                  d!      |j                  d"      #      }	|j                  d$      }
|
r	 t&        j                  j                  |	%      j                         }|rL|j(                  }t        d&|j*                   |j                  |j*                  d't        j                        S t,        j                  j                  |
      j                         }|r"t&        j                  j%                  ||	(       nt/        d)|
 d*       t        d,|	j                  d-t        j0                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r!}t/        d+t        |              Y d }~d }~ww xY w# t        $ r0}t        dt        |      it        j2                        cY d }~S d }~ww xY w).Nr   r   r(  r  rc   ztm_article_number is requiredr  z/Product with this article number already exists)r   r  rc   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  Fsourcer   r  r  r  )r   r  r  rc   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rK  r   r  r  r  r  r  z(Product is already added to collection: r   existing_collection_idexisting_collection_namer  zWarning: Collection with id r  z$Error adding product to collection: z!Product sample added successfully)r   r   )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rc   r   r   r    r  rZ  r   r   r  r   )r   r   r-  r   r   r   rc   existing_productr   r  r  r  r  r  s                 rJ   r   zAddProduct.post  s   VVL

 C oo$$$,2248
Y 01&:S:STT
,,4xx 34 Y ?@IdIdee#++22EV2W]]_A#&&*<< ))	+ + $%:!))0088N+%!45,'HH_-,'XXhxx
#!%*F!G&*hh/P&Q!%*F!Gxx
#88$:;"hh'@A"67HH_-((#89XX./"67hh0%8XXhXXh((=) HH%<=,'5 1 >< 88O,=<#7#?#?#F#F~#F#^#d#d#f #7BB<=P=`=`<ab!4!7!7#6#F#F ++	- - )0077=7IOOQ^""))^^)\ *=/DE 2


 $$& &W  LYs1v&f.J.JKKLL  <
1#a&:;;< 
 V
Is1v&v/T/T
UUVs   O AP7 ;P7 A"P7 :GP7 	A;P
 A!P
 &'P7 	P%P<P=P7 PP7 
	P4P/*P7 /P44P7 7	Q0 %Q+%Q0+Q0Nr   r   rL   rJ   rI  rI    s	    WVrL   rI  c                       e Zd Zd Zy)DeleteProductByArticlec                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  |      j                         }|s!t        dd| dit        j                        S |j                  r	 t        j                         }|j                  t        j                                 |j#                  t$        t&        t(        t*        d	       t,         d
|j                   }|j/                  d| d       j3                         r/|j3                         j5                         r|j7                          	 |j8                  r%	 d
|j                   }	t:        j=                  |	       |j?                          t        dd| ddt        j@                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r}t1        d|        Y d }~d }~ww xY w# j3                         r0|j3                         j5                         r|j7                          w w w xY w# t        $ r}t1        d|        Y d }~d }~ww xY w# t        $ r0}t        dt        |      it        jB                        cY d }~S d }~ww xY w)Nr   r   r(  r#  r  zProduct with article number r  Fr  r  zrm -rf ""zFailed to delete NAS folder: z!Failed to delete Dropbox folder: r  z deleted successfully.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  exec_commandr   get_transport	is_activer  r  r   files_delete_v2deleter   r   )
r   r   rc   r-  r   r   r  r  folder_pathr&  s
             rJ   rY  zDeleteProductByArticle.deletes  s   +VN

 C oo$$$,2248
Y 01&:S:STT""))<M)NTTV7
Y">?P>QQ[ \]((* * 
S$$X%;%;%=>[[x(Z_[`&'q)@)@(AB[Q/0 	 1 1 3 = = ?	iik4w6678,- >>
-.?-@@VW ! !E  NYA'0L0LMMN&  0
*1#.//0 	 1 1 3 = = ?	iik !@  4
.qc2334 
 V
Is1v&v/T/T
UUVs   G" AJ+ AJ+ ,J+ 9BH  AJ+ $J 01J+ "	H+%HHJ+ HJ+ 	H?'H:5I :H??I AJJ+ 	J(J#J+ #J((J+ +	K$4%KK$K$N)r   r   r   rY  r   rL   rJ   rQ  rQ  r  s    ,VrL   rQ  c                       e Zd Zd Zy)
AssignUserc           
         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        |t              r|g}n-t        |t              st        ddit        j                        S |j                  j                  dg       }t        |t         t        f      rt!        |      g}n-t        |t              st        dd	it        j                        S t        t#        |D cg c]  }t!        |       c}            }t        j                  j                  |
      }t#        |j%                  dd            }	|	st        ddit        j                        S t&        j                  j                  |      }
|
j)                         st        ddit        j                        S |j                  j                  d      }g }|
D ]?  }|r||_        |D ]  }t,        j                  j/                  ||ddi      \  }}|j1                  |j2                         d|_        |j7                  dg       	 |j8                  |j:                  |j<                  xs dt?        j@                         jC                  d      d}d}tE        d|      }tG        |      }tI        ||tJ        jL                  |jN                  g      }|jQ                  |d       |jS                           d|_        |j7                  |rd dgnd g       B t        d!t        |	      ||d"t        jV                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY wc c}w # t        $ r&}tU        d|j2                   d|        Y d }~d }~ww xY w# t        $ r0}t        dt        |      it        jX                        cY d }~S d }~ww xY w)#Nr   r   r(  r#  r   job_number is requiredz0job_number must be a string or a list of stringsr  z,watchers should be a list or a valid user ID)id__inr   T)flatzNo valid watchers found)job_number__inz2No ProductSample found for the given job_number(s)shoot_date_timer  )r  r   r  is_retoucher)update_fieldsN/Az	%B %d, %Y)	user_namer   r  assigned_dateu=   You're Invited for Retouching a Product – Lionshead Studioszretoucher_email.htmlr  r	  z!Email sending failed for watcher r  Assignedr   z!Retouchers assigned successfully.)r   watchers_added
new_shootsjob_numbers_processed)-r'   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   listr   setvalues_listr   r  rb  r   r  ra   r   rc  r   rn   r   r  r   r-   r}  r)   r*   r(   r
   r   r   r  r  r   r   r   )r   r   r-  r   r   job_numbersr  widr  valid_watcher_idsproduct_samplesrb  created_watchersr  r  r*  createdr  r  r  r  r   email_errors                          rJ   r   zAssignUser.post  s   hVN

 C oo$$$,2248
Y 01&:S:STT !!,/;
Y 89&B]B]^^c"-K{D)Y RS\b\w\wxx llz2.8C:&HHx&Y NOX^XsXstt 3H5HSCH5678 ??))):==44T4EF
Y 9:6C\C\]] #**111M?

 
 
"Y TU^d^w^wxx \\%%&78? )~&5^# !nn55d# 6 ^UG
 UXX& !W\\ 0\1MLL#..%22;e //<	g Pg%&<gFl-l$**==/	e |[9jjlA !L 'Nx1B&C]e\fgY )^ 2,-"(	
 ! !y  NYA'0L0LMMN2 6t  M/

|2k]KLLM  
 V
Is1v&v/T/T
UUVs   N  AP 9P A P A+P P O"A%P AP BP $B-O!AP  	O)%OOP OP !	P*PP PP 	Q%QQQNr   r   rL   rJ   r\  r\    s	    iVrL   r\  c                       e Zd Zd Zy)GetUserActivityc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                         j                  d      }g }|D ]B  }|j                  |j                  |j                   |j"                  d}|j%                  |       D t        d|it        j&                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j(                        cY d }~S d }~ww xY w)Nr   r   r(  r#  rW  )r   r  typer   r   )r'   r   r   r   r   r   r   r   r   r   r   r   allrs  r   r  ry  r   ra   r   r   	r   r   r-  r   r   activity_objall_Dataactivityr.  s	            rJ   r   zGetUserActivity.get  s4   VN

 C oo$$$,2248
Y 01&:S:STT&&**,55e<<8x%%MM%%	H OOH   FH%f.@.@
AA#  NYA'0L0LMMN$ 
 V
Is1v&v/T/T
UUVsN   C. AD* BD* .	D'7%D"D'D* "D''D* *	E#3%EE#E#Nrz  r   rL   rJ   rw  rw    s    VrL   rw  c                       e Zd Zd Zy)UpdateOrderIndexc           
         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  }t        |t              st        ddit        j                        S |D ][  }|j                  d      }|j                  d      }|j                  d      }	|s:t         j                  j                  |	      j                         }
|
skg }|
j"                  |k7  r(|j%                  d
|
j"                   d|        ||
_        |
j                  |	k7  r)|j%                  d|
j                   d|	 d       |	|
_        |s|j&                  |
_        t+        j,                         j/                  d      |
_        |
j3                          |j4                  xs |j&                  }| d|
j6                  xs | ddj9                  |      z   }t:        j                  j=                  ||ddt+        j,                                t>        j                  j                  |
      jA                  d      }|D ]  }|jB                  s|jB                  |k7  s d|
j6                  xs | d| ddj9                  |      z   }t:        j                  j=                  |jB                  |ddt+        j,                                 ^ t        ddit        jD                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        jF                        cY d }~S d }~ww xY w)Nr   r   r(  r#  z%Invalid data format. Expected a list.r   r1  r   r  zorder index from u    → zstatus from '   ' → ''%Y-%m-%d %H:%M:%Sz updated sample 'z': , Fsample_update)r   r   is_readry  end_dater  r   zSample 'z' was updated by r  sample_notificationz,Order index and status updated successfully.)$r'   r   r   r   r   r   r   r   r   r   r   r   rw   rl  r   r   r   r1  ra   r   r  r+   r-   r}  r  r   rn   r  r  r   r   r   select_relatedr   r   r   )r   r   r-  r   r   job_listjobr   r1  status_valuesamplechangeschanger_identityupdate_messagerelated_shootsr*  
notify_msgs                    rJ   r   zUpdateOrderIndex.post/  s'   JVN

 C oo$$$,2248
Y 01&:S:STTll8
Xt
$Y GHQWQlQlmms&J''-(K778$L""))Z)@FFHFWk)nn(););(<E+OP&f%nn}V]]O7<.JK"fm"..f%-\\^%<%<=P%Qf"kkm "8(..
	,V->->-L*,MSQ	7	  !! "  ~~,,V,<KKFSn!%
**x/F%%344EFVEWWY	Z99W
 	 	##jj#,,. 	$ 	
 "W r IMNW]WiWi
jjI  NYA'0L0LMMNL 
 V
Is1v&v/T/T
UUVso   L AM 8M A,M A3M 5DM 7M B
M 	M%M MM MM 	N%N<NNNr   r   rL   rJ   r  r  .  s	    KVrL   r  c                       e Zd ZdZd Zy)SendMonthlyRecapz1Send AI-generated monthly activity summary emailsc           	         t               j                         }|j                  d      }|t        d      z
  }|j                  d      }t        j
                  j                  d      }|D ]`  }t        j
                  j                  ||      j                  d      }|j                         sE|D 	cg c]-  }	d|	j                  j                  d	       d
|	j                   / }
}	dj                  |
      }d|j                  d       d| d}	 t        j                  j!                  ddddd|dgd      }|d   d   d   d   j#                         }t'        d||j                  d      ||d      }d|j                  d       }t)        ||t*        j,                  dg       }|j/                  |d!       |j1                          c t3        dd"it4        j6                  #      S c c}	w # t$        $ r}d|z   }Y d }~d }~ww xY w)$Nr   )day)daysTr  )start_date__date__gtestart_date__date__lter  [z%d %b %H:%Mz] 
zxYou are an assistant summarizing monthly work logs for an email newsletter.
			Here's the activity log for the month of z%B %Yz:

			zn

			Write a friendly, concise 5-6 sentence summary highlighting key activities and achievements of the month.zgpt-4systemzBYou are a helpful assistant that summarizes monthly work activity.)rolecontentr   gffffff?)modelmessagestemperaturechoicesr   r   r  zMWe're unable to generate a monthly summary. Here's a raw list of activities:
zmonthly_recap.html)r   monthsummary
activitiesu   📅 Monthly Recap – zdeveloper@esferasoft.comr  r	  z Monthly recap sent successfully.r   )r-   r   rG   r	   r   r   r   r   rs  r  r  r}  r   r  openaiChatCompletionr   r   r   r)   r(   r
   r   r  r  r   r   r   )r   r   todayfirst_day_of_current_monthlast_month_endlast_month_startrE  r   r  aactivity_linesjoined_activitiespromptr  r  r   r  r  r   s                      rJ   r   zSendMonthlyRecap.get  sL   
%**,%$}}}3-	q0AA.#+++2
//
 
 D
 
1%d$$++*( ,  Xl  


 FPEOam,-R		{;Z   yy0--=-F-Fw-O,P Q mp6s$$++$hi&)  , H y!!$Y/	:@@BG
 ##7&&w/	: < ''7'@'@'I&JK7!	''"#	5 	L+6::<k n 
9@A&J\J\	]][0  s^arrGss   72G/AG44	H=HHN)r   r   r   helpr   r   rL   rJ   r  r    s    ;@^rL   r  c                       e Zd Zd Zy)DashboardSummaryc                    	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                         j                         }t        j                  j                  d      j                         }t        j                  j                  d      j                         }t        j                  j                  d      j                         }t        j                  j                  d      j                         }	t        j                  j                  d	
      j                         }
t        j                  j                         j                         }t        j                  j                  d	      j                         }t        j                  j                  d	
      j                  t!        d            j#                  d      j                  t%        d            j'                  d      }t        j                  j                  d	      j                  t!        d            j#                  d      j                  t%        d            j'                  d      }d } ||      } ||      }t)        t+        |j-                               t+        |j-                               z        }||D cg c]  }|j/                  |d       c}|D cg c]  }|j/                  |d       c}d}t        j                  j                  t!        d            j#                  d      j                  t%        d            j'                  d      }d } ||      }t)        |j-                               }|D cg c]  }|j/                  |d       }}||d}t        d|||||	||
|||dt        j0                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY wc c}w c c}w c c}w )Nr   r   r(  r#  
unscheduler  rh  r  Trc  rD  
created_at)r  r  r   )r   c                 X    | D ci c]  }|d   j                  d      |d    c}S c c}w Nr  z%Y-%mr   r}  querysetitems     rJ   format_dataz)DashboardSummary.get.<locals>.format_data  s2    FN
Ohd4=!!'*DM9h
OO
O   'r   )months
retouchersclientsr  c                 X    | D ci c]  }|d   j                  d      |d    c}S c c}w r  r  r  s     rJ   r  z)DashboardSummary.get.<locals>.format_data   sA      	M7#T']2  r  )r  products_uploadedz)Dashboard summary retrieved successfully.)r   total_product_countunschedule_productsreview_completed_productsassigned_productsretouching_done_productsclient_retoucher_datatotal_retouchertotal_clientstotal_collectionsproduct_graph)r'   r   r   r   r   r   r   r   r   r   r   r   rz  r   r   annotater9   valuesr:   rs  sortedrm  keysr   r   )r   r   r-  r   r   r  r  r  r  r  r  r  r  r  r  r  retoucher_dataclient_data
all_monthsr  r  productsproduct_datar  product_countsproducts_graph_datas                             rJ   r   zDashboardSummary.get  s   M	w	3 __##s#+113(	
I/09R9R
SS%--11399;%--44l4KQQS+33::DT:U[[]#++22J2GMMO*2299CT9U[[]__++4+@FFH/(00446<<>??))d);AAC-
??t,H:l+H,
F7OH5;HHW  ??D)H:l+H,
F7OH5;HHW 
P z*.G$+ c.--/03{7G7G7I3JJK* <FGJ5.$$UA.JG6@AjU{ua(jA H:l+H,
F7OH5;HHW  X&, ,##%&&<BCF5L$$UA.F.C & 
9-- 9)60$ (& 
   M 
 M
Is1v&v/K/K
LLMT HA* Ds/   P :QQ?Q	P?%P:4P?:P?Nrz  r   rL   rJ   r  r    s    U rL   r  c                       e Zd Zd Zy)BulkUpdateProductsc                 ^   	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  dg       }i d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d	|j                  j                  d	      d
|j                  j                  d
      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      d|j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d      |j                  j                  d       t        j                         j                  d!      |j                   d"}g }i }|D ]W  }	t"        j                  j                  |	#      j                         }
|
s5g }|j%                         D ]W  \  }}|	t'        |
|      }t        |      t        |      k7  s-|j)                  t+        |      ||d$       t-        |
||       Y |s|
j/                          |j)                  |	       |||	<   d%j1                  d& |D              }t2        j                  j5                  d'|	 d(|j                    d)| d*d+|
j6                  ,       t8        j                  j5                  d'|	 d(|j                    d)| d*d+|-       Z t        j                  j                  d*.      }|D ]  }t;        |      d/k(  r
d0|d1    d2}nd%j1                  d3 |D              }d4| d2}t<        j>                  }|j@                  }tC        d5|j                   |t        j                         jD                  d6      }tG        |d7||g      }|jI                  |d8       |jK                           t        t;        |       d9|d:t        jL                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w);Nr   r   r(  r#  job_idsr  r  rc   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rK  r   sample_statusrb  style_number
sku_new_costyle_new_cor  )r  r  r  rK  r   r  rb  r  r  r  r  r  r  )fieldfromr  r  c              3   D   K   | ]  }|d     d|d    d|d    d  yw)r  z: 'r  r  r  r  Nr   )rV   changes     rJ   rY   z*BulkUpdateProducts.post.<locals>.<genexpr>f  s9      $)& 
VF^,GF4L>C)s    Job z updated by z. Fields changed: FBulkProductUpdater   r  ry  r   )r   r  ry  r   rD  r   zUpdate: Job #r   u'    Details Modified – Lionshead Studiosc              3   2   K   | ]  }t        |        y wrQ   )r   )rV   r  s     rJ   rY   z*BulkUpdateProducts.post.<locals>.<genexpr>  s     ;ls#c(ls   zUpdate: Job Numbers #zproduct_shot_updated.html)rf  updates_per_jobr   rj   r	  z job(s) updated successfully.)r   updated_jobs)'r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r}  rn   r   r  getattrra   rK   setattrr   r  r   r   rc   r   lenr
   r   r   r)   r   r(   r  r  r   )r   r   r-  r   r   r  rd  r  r  r  pro_objupdated_fields_detailr  valueoriginal_valuefield_changes_textrE  r   r  	jobs_listr  r  r  msgs                           rJ   r   zBulkUpdateProducts.post   s   M	w	3 __##s#+113(	
I/09R9R
SSLLY+'7<<##N3  - (()<= !!,/	
 GLL$$_5 !!,/ W\\h' w||
+  !1!12N!O %gll&6&67X&Y  !1!12N!O w||
+ 7<<++,BC gll../HI ))*>?  GLL$$_5!" ',,**+@A#$ \\%%&67 ))*>?ll&&'89\\h'\\h'LL$$_5ll&&'89<<##N3!!,/<<##N3$LLN334GH;-@ ,/c""))S)9??A7%++-uw.n	^	E
	*##$U+%	 

 wu% . \\^1_S )) $)$ 
   SEhmm_4FGYFZ[,,	 !  !!SEhmm_4FGYFZ[	 " E T //
 
 U
 
3%d	,1l1o..UVG		;l;;I%i[0WXG((:jj8"))'\\^((< 
 Z(	D3,488:+ . 
\"##@A 
   _ 
 M
Is1v&v/K/K
LLMs   W3 3	X,<%X'!X,'X,Nr   r   rL   rJ   r  r    s    u rL   r  c                       e Zd Zd Zy)AssignProductReviewc                    	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|j                  j                  d      }t        |t              r|st        ddit        j                        S t        |t              r|st        ddit        j                        S g }|D ]'  }t         j                  j                  |	      j                         }	|	s|j#                  |d
dd       J|D ]  }
	 t        j                  j                  |
      j                         }d}d }|sdj%                  t'        j(                  t*        j,                  t*        j.                  z   d            }t1        j2                  |      }t        j                  j5                  |
d|      }d}	 d}t6        j8                  }|
g}d|
 d| d}t;        d|
|ddt=        j>                         j@                  d      }tC        ||||      }|jE                  |d       |jG                          tH        j                  j                  ||	       jK                         }|s^tH        j                  j5                  ||	t=        j>                         t=        j>                         !       d|	_&        |	jO                          |j#                  ||
d"|d#        * t        d$|d%t        jP                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r-}|j#                  ||
ddt        |       d       Y d }~jd }~ww xY w# t        $ r*}|j#                  ||
d
t        |      d       Y d }~d }~ww xY w)&Nr   r   r(  r#  r  client_emailsz job_ids must be a non-empty listz&client_emails must be a non-empty listr  failedzInvalid product ID)job_idr   reasonr   Frj   r;  kT)r   r   r   u8   You're Invited to Review a Product – Lionshead Studiosz@
							You've been invited to review a product.

							Email: z
							Password: zF

							Log in at: https://lionshead.tidera.ai/userauth/login
							zinvite_review.htmlz*https://lionshead.tidera.ai/userauth/loginzThe Lionshead Studios)client_emailraw_passwordr  company_namer   r	  partial_successz User created, but email failed: )r  r  r   r  )clientr  )r  r  assign_dater  success)r  r  r   new_userz$Review assignment process completed.)r   rJ  ))r'   r   r   r   r   r   r   r   r   r   r   r   r   rw   rl  r   r   ra   r  r   r  stringascii_lettersdigitsr   r   r   r
   r   r)   r+   r-   r   r(   r  r  r   r  is_client_reviewr   r   )r   r   r-  r   r   r  r  rJ  r  r  r  client_userrt  r   r$  r  r  r  r  r  r  ru  assignment_existss                          rJ   r   zAssignProductReview.post  s   M	w	3 __##s#+113(	
I/09R9R
SS LLY'',,""?3-	GT	"'
IAB6KfKf
gg	M4	(
IGHQWQlQl
mm'f""))V)<BBD7
NNfDXYZ$|L??)))=CCE[W\WWV^^F,@,@6==,PTVWXl\2oOO** + k
 g Kw,,z.x n   	| '';$$A/##>	 
| $G\:xPs
lK8
xxz -44;;;X_;`ggi!!((<<>,,.	 )  "&glln^^"	 A % j 
4 
   Q 
 M
Is1v&v/K/K
LLMH  ~~$#4S5E4FG		 
 8  ^^"A	  sb   L; B2N06A<M72B%N0;	M4%M/)M4/M47	N- "N("N0(N--N00	O#9OO#Nr   r   rL   rJ   r  r    s    o rL   r  c                       e Zd Zd Zy)ClientLoginc                     	 |j                   }|j                  d      j                         j                         }|j                  d      }|st	        ddit
        j                        S |st	        ddit
        j                        S t        j                  j                  |d      j                         }|d	k(  rt	        dd
it
        j                        S t        j                  j                  |      j                         }t        j                  j                  |d      j                         }|j                  }t        j                  ||      }	|	rt!        j"                  |      }
|j$                  |j&                  |j(                  |j*                  d}|j,                  dk(  rHd|_        |j/                          t0        j                  j3                  d|j(                  z   dz   d       t5        |
      t5        |
j6                        d}t	        d||d      S t	        ddit
        j                        S # t8        $ r0}t	        dt5        |      it
        j:                        cY d }~S d }~ww xY w)Nr   r   r   r   r   r   Tr   r   r   r   Fr   r   rn   r   r   zClient with emailz+ has logged into the product review portal.client_login)r   ry  r   r   r   r   )r   r   r   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rn   r   r   is_linked_loginedr   r   r   r   r   r   r   r   s                 rJ   r   zClientLogin.post  s   &T
,,488G""$**,5hhz"8
Y236;V;VWW
Y56f>Y>YZZ??!!$!@FFH4
aiY[\djd}d}~~oo$$5$17797__++%5+QWWY>$$>>>(>25 ))'2M**LL]] 22	G   E)!%W\\^  *=w}}*LO|*|  CQ   RM"=--.F 37FSTTY12&:U:UVV	 T
Ic!f%V-R-R
SSTs8   A(I +I 
AI EI 'I 	I=%I82I=8I=Nr   r   rL   rJ   r  r    s    'TrL   r  c                       e Zd Zd Zy)UpdatePasswordc                    	 t        |      }t        j                  j                  |d      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  |      }||_        d|_        |j#                          t%        j&                  |      }|j(                  |j*                  |j,                  |j                   d}t        |      t        |j.                        d	}	t        d
||	d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w)Nr   r   Tr   r   r#  r   znew_password is requiredr  r   zPassword Updated Successfullyr   )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rn   r   r   )
r   r   r-  r   r   r   gen_passwordr   r   r   s
             rJ   r   zUpdatePassword.post:  sE   M	w	3 __##st#<BBD(	
I/09R9R
SS !!.1,	
I9:6C\C\
]]l+,"(#(
--/''1-MM^^!33' =!-,,-
& 
<GU[\	]]5 
 M
Is1v&v/K/K
LLMs   D< <	E5%E0*E50E5Nr   r   rL   rJ   r  r  9  s    ^rL   r  c                       e Zd Zd Zy)MyAssignedReviewc                    	 t        |      }t        j                  j                  |d      j                         }|st        ddit        j                        S t        j                  j                  |d      j                  t        d      t        d            j                         }|j                   j#                  d	d       }|r9t%               }g d
}|D ]  }	|t%        di |	 d|iz  } |j                  |      }t'        t(              }
|D ]_  }t+        j,                  t        |j.                        d      j1                  d      }| d|j2                   }|
|   j5                  |       a |j                   j#                  dd      }g }t7        |
j9                         d |dk(        }|D ]:  \  }}t;        |d      }|j5                  ||j<                  t?        |      d       < t        |t        j@                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w)Nr   r   Tr  r#  )inreviewproductdata__clientready_for_client_review inreviewproductdata__assign_date)r  r   rU  )	r   r  r  rc   r  r  r  r  r  __icontainsz%mz%BrF   month_sort_orderdescc                 Z    | d   j                         d   | d   j                         d   fS )Nr   r   )split)xs    rJ   <lambda>z&MyAssignedReview.get.<locals>.<lambda>  s%    !A$**,q/1Q4::<?3rL   )keyreverserq  )
month_yearr  r   r   )!r'   r   r   r   r   r   r   r   r   r   r   r   r  r6   r7   rt  r?  r   r   r8   rl  r   r|  r  r}  r   ra   r  r  r$   r   r  r   )r   r   r-  r   r   r  search_querysearch_fieldsfields_to_searchr  products_by_monthr  
month_namer'  r  ry  sorted_monthsrx  s                     rJ   r   zMyAssignedReview.get\  s7   M	w	3 __##sd#;AAC(	
I/09R9R
SS ""))' *  H	8	967   HJ  %%))(D9,3=
 !uQ?UG;/>??M !oom,8 "$'g!!#gmm"4d;DDTJ:,a~.:Z ''0  ))--.@&I -3&- ,j(-hTB:]  , 
-(:(:	;;o 
 M
Is1v&v/K/K
LLMs   H 	I%I=IINrz  r   rL   rJ   r  r  [  s    :<rL   r  c                       e Zd Zd Zy)MyAssignedReviewDetailsc                 "   	 t        |      }t        j                  j                  |d      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S 	 t        j                  j                  |      }g }t        j                  j                  |      j                         }|r|j                   r|j                   j"                  }	t%        j&                         }
|
j)                  t%        j*                                	 |
j-                  t.        t0        t2        t4        d       |
j7                         }t8         d|	 d}	 |j;                  |      }t?        ||j@                        }tB        jD                  jG                  tH        jJ                  d|jL                  xs |	      }tC        jN                  |d       |D ]  }| d| }tB        jD                  jG                  ||      }tB        jD                  jQ                  |      s|j                  ||       tH        jR                   d|jL                  xs |	 d| }|jU                  |        	 	 |jY                          |
jY                          t[        |      }|j\                  }||d<   t        |t        j^                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        j                  $ r  t        d	d
it        j                        cY S w xY w# t<        $ r g }Y w xY w# t        $ r[}t        d	dt        |       it        jV                        cY d }~	 jY                          |
jY                          S #  Y S xY wd }~ww xY w#  Y 'xY w# 	 jY                          |
jY                          w #  Y w xY wxY w)Nr   r   Tr  r#  r  zjob_id not foundr  ro   r  r  Fr  r  r  r  r  r  r  r  )0r'   r   r   r   r   r   r   r   r   r   r   GETr   r   r  r    r  rZ  r  r  r  r  r  r  r  r  r   r  r  r  r  rg   rc   r  rk   r  r
   r  r  r  r  r  ra   r   r  r"   r   r   )r   r   r-  r   r   r  r  r  r  rZ  r  r  r  rb   r  r  r  r  r  r  rx  ry  s                         rJ   r   zMyAssignedReviewDetails.get  su   M	w	3 __##sd#;AAC(	
I/09R9R
SS;;??8$&	
I126;T;T
UUU""&&&&97 )+33::7:KQQS.99'22BB?				3""8#9#9#;<%
KKthY^K_==?D **!O+<<MNO\\/*U
 2%9R9RSK X00,@W@W@j[jkIKK	D)%&az2['',,y(3Z GGNN:&
hh{J' &&'{73J3J3]o2^^_`h_ijYi   
	ZZ\YY[
 'w/*//-(-	-(:(:	;;C 
 M
Is1v&v/K/K
LLM 
	#	# U
G01&:S:S
TTU$  U,  xW >s1vhGHQWQvQvww
	ZZ\YY[
	x
		
	ZZ\YY[
	s   K1  L- AM5 M# D M5  O 1	L*:%L%L*%L*-0M M #M2.M5 1M22M5 5	O>(O&O'O$ , OOOO$ O!$P& PPP	PNrz  r   rL   rJ   r/  r/    s    D<rL   r/  c                       e Zd Zd Zy)
AddCommentc                    	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }|j                  j                  d      }|j                  j                  d	      }|j                  j                  d
      }	|j                  j                  d      }
t        j                  j                  |||||	|
       |
rt         j                  j                  d|j"                   d|j$                   dd|j&                         t        j                  j                  |j$                        j)                  t+        d      dz          t,        j                  j                  d|j"                   d|j$                   dd|j&                  |       nt         j                  j                  d|j"                   d|j$                   dd|j&                         t        j                  j                  |j$                        j)                  t+        d      dz          t,        j                  j                  d|j"                   d|j$                   dd|j&                  |       t/        j0                  |j"                  |j$                  ||       t        ddit        j2                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w)Nr   r   r(  r#  r   r^  r  comment_textcomment_imageis_comment_imagemark_image_data)r  r   r5  r6  r7  r8  zClient z, marked the review on the image for product Fcomment_postedr  notification_countr   )r:  )r   r  ry  r   r   z posted a comment on product zPosted successfully)r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rc   r  r1   r   r0   delayr   )r   r   r-  r   r   r   r  r5  r6  r7  r8  s              rJ   r   zAddComment.post  s>   M	w	3 __##s#+113(	
I/09R9R
SS||-*	
I78AZAZ
[[%%,,*,FLLN+!!.1,,,""?3-\\%%&89LL$$%67/##kWc  tA  Ue  vE#  F hnn%%QR]RhRhQij	..	   ;+A+ABII-.2 J  hnn%%QR]RhRhQij	..    hnn%%B;CYCYBZ[	..	   ;+A+ABII-.2 J  hnn%%B;CYCYBZ[	..    #((nn	 
9236;M;M	NNO 
 M
Is1v&v/K/K
LLMs   M
 
	N%M>8N>NNr   r   rL   rJ   r3  r3    s	    JOrL   r3  c                       e Zd Zd Zd Zy)ClientUpdateProfilec                    	 	 t        |      }t        j                  j                  |d      j                         }|st        ddit        j                        S |j                  |j                  |j                  |j                  |j                  |j                   |j"                  d}t        t        j$                  d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j&                        cY d }~S d }~ww xY w)	Nr   r   Tr  r  )r   rn   r   r)  r   r   r   r+  r   )r'   r   r   r   r   r   r   r   r   r   r   r   rn   r   r)  r   r   r   r   r   r,  s         rJ   r   zClientUpdateProfile.get3	  s   TL

 C oo$$$=CCE8
Y 01&:S:STT	==NN((NN 22""	8 &"4"4F\dlm
nn  LYs1v&f.J.JKKL  
 T
Ic!f%V-R-R
SSTsN   C	 AD A,D 		D%C=7D8D =DD 	D>%D93D>9D>c                    	 	 t        |      }t        j                  j                  |d      j                         }|st        ddit        j                        S |j                  j                  d      }|st        t        j                  dd      S |j                  j                  d	      }|st        t        j                  d
d      S t        j                  j                  |      j                  |j                        }|rt        t        j                  dd      S |j                  j                  d      }|st        t        j                  dd      S t        j                  j                  |      j                  |j                        }	|	rt        t        j                  dd      S |j                  j                  d      }
||_        ||_        ||_        |
|_        |j)                          |j                  |j                   |j"                  |j$                  |j&                  |j*                  |j,                  d}t        t        j.                  d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j0                        cY d }~S d }~ww xY w)Nr   r   Tr  r  rn   r   r  r   r   r   r(  r0  r)  r1  r2  r3  r   )r   rn   r   r)  r   r   r   r4  r   )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r5  r   rn   r   r)  r   r   r   r   r   r   r6  s               rJ   r   zClientUpdateProfile.postK	  s|   /TL

 C oo$$$=CCE8
Y 01&:S:STT
,,

6
"4
6#>#>Pbcdd<<G$5
6#>#>Pcdee''u'5===M;6#>#>  QF  G  H  H,,"">2<
6#>#>Pjkll //(((EMMQYQ\Q\M]<6#>#>  QM  N  O  O<<G$58=8>'88>==?	==NN((NN"" 22	8 &"4"4Fdltu
vvU  LYs1v&f.J.JKKLV 
 T
Ic!f%V-R-R
SSTsn   I+ AJ' 8J' 8J' AJ' &8J' AJ' 7B3J' +	J$4%JJ$J' J$$J' '	K 0%KK K Nr8  r   rL   rJ   r=  r=  2	  s    T00TrL   r=  c                       e Zd Zd Zy)GetCommentsc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      }g }|D ]  }|j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  j*                  |j(                  j,                  |j(                  j.                  |j(                  j0                  |j(                  j2                  |j(                  j4                  |j(                  j6                  dd	}	|j9                  |	        |st        dd
it        j:                        S t        d|it        j:                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j<                        cY d }~S d }~ww xY w)Nr   r   r(  r  r  product_id is requiredr  )rn   r   r)  r   rc  r*  r   )r5  r6  r7  r8  r  r  r   z"No comments found for this productcomments)r'   r   r   r   r   r   r   r   r   r   r   r1  r   r   r5  r6  r7  r8  r  r  r   rn   r   r)  r   rc  r*  r   ra   r   r   )
r   r   r-  r   r   r  rD  comment_datacommentcomment_infos
             rJ   r   zGetComments.get~	  s   +TL

 C oo$$$,2248
Y 01&:S:STT-:
Y 89&B[B[\\ ((//:/F8 <w))++00..%%!!ll||!!ll//||!!\\..&&,,((	L$ %' , Y DEfN`N`aa
J-f6H6H
IIK  LYs1v&f.J.JKKLN 
 T
Ic!f%V-R-R
SSTsZ   G% AH! 9H! D2H! H! %	H.%HHH! HH! !	I*%IIINrz  r   rL   rJ   rA  rA  }	  s    ,TrL   rA  c                       e Zd Zd Zy)GetImageDatac                 v   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                  d	      j                  d
      j                         }|st        dd
it        j                         S |j"                  |j$                  |j&                  d}t        d|it        j                         S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j(                        cY d }~S d }~ww xY w)Nr   r   r(  r  r  rC  r  T)mark_image_data__isnullrj   )mark_image_data__exact
image_data)r8  r  r  )r'   r   r   r   r   r   r   r   r   r   r   r1  r   r   r5  lastr   r8  r  r  r   )r   r   r-  r   r   r  rD  rG  s           rJ   r   zGetImageData.get	  sy   "VN

 C oo$$$,2248
Y 01&:S:STT-:
Y 89&B[B[\\ VzV"WTW*WBW'	TV  \2&v/A/ABB  //%%!!< L,/8J8J
KK9  NYA'0L0LMMN< 
 V
Is1v&v/T/T
UUVs[   E AE? 9E? A,E? A E? 	E<%E71E<2E? 7E<<E? ?	F8%F3-F83F8Nrz  r   rL   rJ   rI  rI  	  s    #VrL   rI  c                       e Zd Zd Zy)ReadCommentNotificationsc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }d|_        |j                          t        t        j                   d	d
      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j"                        cY d }~S d }~ww xY w)Nr   r   r(  r  r   r^  r  r   Read successfullyr  )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r:  r   r   r   )r   r   r-  r   r   r   prod_objs          rJ   r   zReadCommentNotifications.post	  s/   TL

 C oo$$$,2248
Y 01&:S:STT!!,/:
Y 89&B[B[\\##**
*KQQS8!"8==?
&"4"4FYZ
[[  LYs1v&f.J.JKKL 
 T
Ic!f%V-R-R
SSTsT   C6 AD2 9D2 A D2 6	D/?%D*$D/%D2 *D//D2 2	E+;%E& E+&E+Nr   r   rL   rJ   rP  rP  	  s    TrL   rP  c                       e Zd Zd Zy)GetNotificationsc           
      L   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                         j                  d      }g }|D ]  }|j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  j*                  |j(                  j,                  d}|j/                  |        t        d|it        j0                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j2                        cY d }~S d }~ww xY w)Nr   r   r(  r#  rW  r   r   r  ry  r  r   rf  
user_emailr   )r'   r   r   r   r   r   r   r   r   r   r   r   rz  rs  r   r   r  ry  r  r   r   rn   r   ra   r   r   r{  s	            rJ   r   zGetNotifications.get	  s^   VN

 C oo$$$,2248
Y 01&:S:STT''++-66u=<8x
++%%MM%%##--%%H OOH    FH%f.@.@
AA1  NYA'0L0LMMN2 
 V
Is1v&v/T/T
UUVsN   D. AE* CE* .	E'7%E"E'E* "E''E* *	F#3%FF#F#Nrz  r   rL   rJ   rU  rU  	  s    VrL   rU  c                       e Zd Zd Zy)ReadNotificationc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }t        |      D ]G  }t        j                  j                  |      j                         }d|_        |j!                          I t        t        j"                  dd      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j$                        cY d }~S d }~ww xY w)	Nr   r   r(  r#  r   TrR  r  )r'   r   r   r   r   r   r   r   r   r   r   r   r   rl  r   r  r   r   r   )r   r   r-  r   r   r   r  job_objs           rJ   r   zReadNotification.post
  s#   VN

 C oo$$$,2248
Y 01&:S:STT!!,/::s$$+++5;;=GGOLLN 
 &"4"4FYZ
[[  NYA'0L0LMMN 
 V
Is1v&v/T/T
UUVsN   C' AD# BD# '	D 0%DD D# D  D# #	E,%EEENr   r   rL   rJ   rZ  rZ  
  s    VrL   rZ  c                       e Zd Zd Zy)GetNotificationCountc           
         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  d      j                  d      d d }t        j                  j                  d      j                         }g }|D ]  }|j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  j*                  |j(                  j,                  d	}	|j/                  |	        t        t        j0                  d
||d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j2                        cY d }~S d }~ww xY w)Nr   r   r(  r#  F)r  rW  r   rW  rR  )r   r   r   unread_count)r'   r   r   r   r   r   r   r   r   r   r   r   rs  r   r   r   r  ry  r  r   r   rn   r   ra   r   r   )
r   r   r-  r   r   not_objr`  r}  r~  r.  s
             rJ   r   zGetNotificationCount.get'
  s   VN

 C oo$$$,2248
Y 01&:S:STT""))E):CCEJ2AN7''...?EEG<8x
++%%MM%%##--%%	H OOH  &"4"4FYbj  {G  H  I  I-  NYA'0L0LMMN. 
 V
Is1v&v/T/T
UUVsN   E" AF DF "	F+%FFF FF 	G'%GGGNrz  r   rL   rJ   r^  r^  &
  s    VrL   r^  c                       e Zd Zd Zy)ArchiveUnarchiveProductc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        |      D ]T  }t        j                  j                  |	      j                         }|d
k(  rd|_        nd|_        |j!                          V t        t        j"                  dd      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j$                        cY d }~S d }~ww xY w)Nr   r   r(  r#  r  r  archive_statuszarchive_status is requiredr  trueTFr
  r  )r'   r   r   r   r   r   r   r   r   r   r   r   r   rl  r   rV  r   r   r   )	r   r   r-  r   r   r  re  articlerS  s	            rJ   r   zArchiveUnarchiveProduct.postF
  s   VN

 C oo$$$,2248
Y 01&:S:STTLL$$%56>
Y <=fF_F_``LL$$%56>
Y <=fF_F_``~&w$$+++HNNPHX XMMO ' &"4"4Fcd
ee)  NYA'0L0LMMN, 
 V
Is1v&v/T/T
UUVZ   E AF	 9F	 9F	 A=F	 	F%F;F<F	 FF	 		G%F=7G=GNr   r   rL   rJ   rc  rc  E
  r  rL   rc  c                       e Zd Zd Zy)DropUndropProductc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        |      D ]T  }t        j                  j                  |	      j                         }|d
k(  rd|_        nd|_        |j!                          V t        t        j"                  dd      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j$                        cY d }~S d }~ww xY w)Nr   r   r(  r#  r  r  drop_statuszdrop_status is requiredr  rf  TFr
  r  )r'   r   r   r   r   r   r   r   r   r   r   r   r   rl  r   rY  r   r   r   )	r   r   r-  r   r   r  rl  rg  rS  s	            rJ   r   zDropUndropProduct.poste
  s   VN

 C oo$$$,2248
Y 01&:S:STT\\%%&67>
Y <=fF_F_``,,""=1;
Y 9:6C\C\]]~&w$$+++IOOQHfX XMMO ' &"4"4Fcd
ee)  NYA'0L0LMMN, 
 V
Is1v&v/T/T
UUVrh  Nr   r   rL   rJ   rj  rj  d
  r  rL   rj  c                       e Zd Zd Zy)readyClientReviewc                 `   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|j                  j                  d      }|st        ddit        j                        S |st        ddit        j                        S t        |t              s|g}t         j                  j                  |	      j#                  |
       t        t        j$                  dt'        |      d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j(                        cY d }~S d }~ww xY w)Nr   r   r(  r#  r  
key_statuszkey_status is requiredr  )tm_article_number__in)r  r
  )r   r   updated_count)r'   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   rl  r   r  r   r  r   )r   r   r-  r   r   article_numbersrp  s          rJ   r   zreadyClientReview.post
  sx   VN

 C oo$$$,2248
Y 01&:S:STT\\%%&67?  .:
Y 89&B]B]^^
Y <=fFaFabb _d
+&'O oFMMfpMq
%%3)  -  NYA'0L0LMMN6 
 V
Is1v&v/T/T
UUVs[   D8 AE4 AE4 0E4 A(E4 8	E1%E,&E1'E4 ,E11E4 4	F-=%F("F-(F-Nr   r   rL   rJ   rn  rn  
  s     VrL   rn  c                       e Zd Zd Zy)retoucherJobListingc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  dd       }t        j                  j                  |      j                         }|r|j                  t        |      t        |      z  t        |	      z  t        |
      z  t        |      z  t        |      z  t        |      z  t        |      z  t        |      z        }|j                  j                  dd       }|j                  j                  dd      }|r+|dk(  r|j!                  |      }n|j!                  d|       }t#               }	|	j%                  ||      }
t'        |
d      }|||j(                  d}|	j+                  |      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j,                        cY d }~S d }~ww xY w)Nr   r   r(  r  rU  )shoots__userr\  r^  r`  rb  rd  rf  rh  rj  rl  rn  ro  rp  r   Trq  )rn  ro  rJ  )r'   r   r   r   r   r   r   r   r   r   r   r?  r   r   rt  r   rs  r:  ru  r"   r   rQ  r   )r   r   r-  r   r   rU  rv  rn  ro  rL  rw  rx  ry  s                rJ   r   zretoucherJobListing.get
  s:   1TL

 C oo$$$,2248
Y 01&:S:STT  $$Xt46"")) * XZ 
 nnv&F#$F+, V$% '	(
 V$%  ! V$% 6"#
G !!%%i67$$((u=: U(W!G9.W&(9,,Wg>;'$?: = 
*
*=
99W  LYs1v&f.J.JKKLZ 
 T
Ic!f%V-R-R
SSTsN   G AH E=H 	H"%HHH HH 	I%I	I	INrz  r   rL   rJ   ru  ru  
  s    2TrL   ru  c                       e Zd Zd Zy)myCredSettingsc           
         	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                         j                         }|j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  d	}t        t        j,                  d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j.                        cY d }~S d }~ww xY w)Nr   r   r(  r  )	r   dropbox_app_keydropbox_app_secretdropbox_refresh_tokennas_hostnamenas_port_nonas_usernamenas_passwordnas_remote_base_pathrR  r   )r'   r   r   r   r   r   r   r   r   r   r   r   rz  r   r{  r|  r}  r~  r  r  r  r  r   r   )r   r   r-  r   r   cred_objr.  s          rJ   r   zmyCredSettings.get
  sF   TL

 C oo$$$,2248
Y 01&:S:STT##'')//18 
..!44$::((&&((((#888 &"4"4FYbjk
ll)  LYs1v&f.J.JKKL* 
 T
Ic!f%V-R-R
SSTsN   D
 AE B.E 
	E%D>8E9E >EE 	E?%E:4E?:E?Nrz  r   rL   rJ   ry  ry  
  s    TrL   ry  c                       e Zd Zd Zy)
logoutUserc                     	 t        ddit        j                        }|j                  d       |j                  d       |S # t        $ r0}t        dt        |      it        j                        cY d }~S d }~ww xY w)Nr   zLogged out successfullyr   r   r   )r   r   r   delete_cookier   r   r   )r   r   r  r   s       rJ   r   zlogoutUser.post  so    	V	#<=fFXFXY8.)/*
?	 V
Is1v&v/T/T
UUVs   A A 	A<%A71A<7A<Nr   r   rL   rJ   r  r    r   rL   r  c                       e Zd Zd Zd Zy)createCollectionc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                         j                  d      }g }|D ]7  }|j                  |j                  |j                   d}|j#                  |       9 t        t        j$                  d|d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j&                        cY d }~S d }~ww xY w)	Nr   r   r(  r  rW  )r   rZ  r  r6  r   )r'   r   r   r   r   r   r   r   r   r   r   r   rz  rs  r   rZ  r  ra   r   r   )	r   r   r-  r   r   r  r}  colr.  s	            rJ   r   zcreateCollection.get$  s+   VL

 C oo$$$,2248
Y 01&:S:STT&..224==eD>8s
&&**..H OOH  &"4"4F\emn
oo#  LYs1v&f.J.JKKL& 
 V
Is1v&v/T/T
UUVsN   C# AD BD #	D,%DDD DD 	E(%EEEc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }|rt        ddit        j                        S t        j                  j                  |      }t        j                          }|j#                  t        j$                                d }		 |j'                  t(        t*        t,        t.        d	       t0         d
| }
g d}|j3                  d|
 d      \  }}}|j4                  j7                         }|dk7  r2|j9                         j;                         }d| }	t=        d|	        nt=        d|
        |D ]  }|
 d
| }|j3                  d| d      \  }}}|j4                  j7                         }|dk7  r0|j9                         j;                         }t=        d| d|        rt=        d|         	 	 |jG                         r.|jG                         jI                         r|jK                          t        jL                  dd}|	r|	|d<   t        |      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        j>                  $ r&}dt        |       }	t=        d|	        Y d }~d }~wt        j@                  $ r'}dt        |       }	t=        d|	        Y d }~d }~wt        $ r;}dt        |       }	t=        d|	        dd l!}|jE                          Y d }~Nd }~ww xY w#  Y xY w# 	 |jG                         r0|jG                         jI                         r|jK                          w w w #  Y w xY wxY w# t        $ r0}t        dt        |      it        jN                        cY d }~S d }~ww xY w)Nr   r   r(  r  rZ  )rZ  z(Collection with this name already existsFr  r  )z_PSD WorkingzClient OriginalszClipping PathszDigital ProofsFeedbackFinalszTrash laterz
mkdir -p "rS  r   z$Failed to create collection folder: zNAS Error: z(Successfully created collection folder: z$Warning: Failed to create subfolder r  z Successfully created subfolder: zNAS SSH Authentication failed: zNAS SSH connection error: zFailed to create NAS folder: Created successfullyr  nas_warning)(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  rU  channelrecv_exit_statusr  decoder   AuthenticationExceptionSSHException	traceback	print_excrV  rW  r  r   r   )r   r   r-  r   r   rZ  r  r  r  	nas_errorcollection_folder_path
subfoldersstdinstdoutstderrexit_status	error_msg	subfoldersubfolder_pathr  ry  s                        rJ   r   zcreateCollection.post>  s   `VL

 C oo$$$,2248
Y 01&:S:STT\\%%&78?
Y 01&:S:STT+33::?:[aacY JKTZTmTmnn #**11O1T: 
			3""8#9#9#;<96
KKthY^K_ !11?2CDJ  ,,z:P9QQR-STE66..113Ka%%'Y7	{CY
[$%
56L5MNO  	/0)=^ --
>:J!.LMUFF>>224[q++-&&(i29+R	{KL.~.>?@  ,
 1 1 3 = = ?	iik %%,= #,M- 
=
!!u  LYs1v&f.J.JKKLx 
*
* %1#a&:I	K	{
#$$


 %,SVH5I	K	{
#$$
 /Ax8I	K	{
#$	
		
 1 1 3 = = ?	iik !@
	  
 V
Is1v&v/T/T
UUVs   K AP 9P AP "AP <D1L -O />O -$P 	L%L LP LP O!M=O OM:4O :O0N<6O <OO O	P P>PPPPP 	Q"%QQQNr8  r   rL   rJ   r  r  #  s    V4aVrL   r  c                       e Zd Zd Zd Zy)addToCollectionc                 .   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|r	t        j                  j                  |      j                  d      }|j                         st        ddit        j                        S g }|D ]L  }|j                   }	|j#                  |	j$                  |	j&                  |j(                  |j*                  d	       N t,        j                  j                  |      j                         }
t        t        j.                  ||
r|
j0                  nd
|d      S t        ddit        j2                        S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j4                        cY d }~S d }~ww xY w)Nr   r   r(  r  r  )r  r  z$No products found in this collection)r  r  r  r  Unknown)r   r  rZ  r  zcollection_id  is required)r'   r   r   r   r   r   r   r   r   r   r   r?  r   r    r  r  r  ra   rc   r  r  r  r   r   rZ  r   r   )r   r   r-  r   r   r  collection_productsproducts_datar  r  r  s              rJ   r   zaddToCollection.get  s   .VN

 C oo$$$,2248
Y 01&:S:STT''++O<=.66==  > nY  %%'i!GH))+ + M#||W--**OO--	  $ (//66-6HNNPN&&#:H66i	   Y <=**, ,O  NYA'0L0LMMNT 
 V
Is1v&v/T/T
UUVs[   F AG A9G B,G G 	G(%GGG GG 	H$%H	HHc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d	      }|st        dd
it        j                        S t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  |      j                         }	|	r|	j                   }
|
j"                  |j"                  k(  rt        ddit        j$                        S t        d|
j&                   |
j"                  |
j&                  dt        j$                        S t        j                  j)                  ||       t        t        j*                  dd      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j,                        cY d }~S d }~ww xY w)Nr   r   r(  r  r  rC  r  No product foundr  collection_id is requiredzNo collection foundr  z)Product already exists in this collectionz0Product is already added to another collection: rL  r  r  r  )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r  r   r   rZ  r   r   r   )r   r   r-  r   r   r  r  r  r  r  r  s              rJ   r   zaddToCollection.post  sh   'VL

 C oo$$$,2248
Y 01&:S:STT  .:
Y 89&B[B[\\&&--*-MSSU;
Y 23F<U<UVV<<##O4=
Y ;<VE^E^__&..55=5IOOQ>
Y 56v?X?XYY "6!=!=!D!D[!D!Y!_!_!a!5@@!2!22i!LMV\VqVqrrCDWDgDgChi 3 6 6"5"E"E **	, , &&N[&Y
&"4"4F\]
^^E  LYs1v&f.J.JKKLF 
 V
Is1v&v/T/T
UUVst   I AJ 9J AJ "9J AJ )A1J ?J <J 	J!%JJJ JJ 	K%KKKNr8  r   rL   rJ   r  r    s    /Vd(VrL   r  c                       e Zd Zd Zy)deleteCollectionc                 h   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  |      j!                          |j!                          t        t        j"                  d	d
      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j$                        cY d }~S d }~ww xY w)Nr   r   r(  r  r  r  zCollection not found)r  z0Collection and its products deleted successfullyr  )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    rY  r   r   )r   r   r-  r   r   r  r  s          rJ   r   zdeleteCollection.post  sm   VN

 C oo$$$,2248
Y 01&:S:STT<<##O4=
Y ;<VE`E`aa&..555GMMO>
Y 67@Y@YZZ&&.&AHHJ
%%H  '  NYA'0L0LMMN0 
 V
Is1v&v/T/T
UUVs[   D< AE8 9E8 AE8 "AE8 <	E5%E0*E5+E8 0E55E8 8	F1%F,&F1,F1Nr   r   rL   rJ   r  r        VrL   r  c                       e Zd Zd Zy)changeClientViewStatusc                    	 	 t        |      }|j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }d|_        |j                          t        j                  j!                  d|j"                  j$                   dd	d
|j"                  j&                         t        t        j(                  dd      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j*                        cY d }~S d }~ww xY w)Nr   r   	review_idzreview_id id requiredr(  Tr  z has been viewed by the client.FReviewViewedr  r
  r  )r'   r   r   r   r   r   r   r   r   r   r   r   r   	is_viewedr   r   r   r  r   rc   r   r   )r   r   r-  r   r  
review_objs         rJ   r   zchangeClientViewStatus.post%  sE   VN

 C ||,9
Y 78AZAZ[[#++22i2@FFH::??:%%0011PQ	##55	   %%3  #  NYA'0L0LMMN* 
 V
Is1v&v/T/T
UUVsM   C8 9D4 B/D4 8	D1%D,&D1'D4 ,D11D4 4	E-=%E("E-(E-Nr   r   rL   rJ   r  r  $  s    VrL   r  c                       e Zd Zd Zy)addWatchersc                    	 |j                   j                  d      }|st        ddit        j                        S |j                   j                  d      }|st        ddit        j                        S t
        j                  j                  |      j                         }|st        ddit        j                        S |j                  |j                  xs d	d
}t        |      D ]|  }t        j                  j                  ||       d}t        d|      }t!        |      }	t#        ||	t$        j&                  |g      }
|
j)                  |d       |
j+                          ~ t        ddt        j,                  d      S # t.        $ r1}t        dt1        |      dt        j2                        cY d }~S d }~ww xY w)Nemailsr   zemails are requiredr   r  rC  r  r  re  )r   r  )r   r  u>   You're assigned as a watcher for Product – Lionshead Studioszwatcher_email.htmlr  r	  r  z(Watchers added and notified successfully)r   r   r   ro   rT  )r   r   r   r   r   r   r   r   r   r   r   r  rl  r!   r   r)   r*   r(   r
   r   r  r  r   r   r   r   )r   r   r  r  r  r  r   r  r  r  	email_msgr   s               rJ   r   zaddWatchers.postC  s   .4LLX&6
Y 56v?Z?Z[[  .:
Y 89&B]B]^^&&--
-KQQS;
Y 23F<U<UVV ((,,57
 Vu%= OG#$8'BLl+L&
((		I   {;NN! $ 9%%   
 4
1v 224 44s0   9F 9F 6AF CF 	G&F<6G<GNr   r   rL   rJ   r  r  B  s    /4rL   r  c                    g }| j                   syt        j                         }|j                  t        j                                	 |j                  t        t        t        t        d       |j                         }d| j                    d}d| j                    d}	 |j                  |      }g d}|D cg c]  t        fd|D              s }	}	 |j                  |       |	D ]  }
| d	|
 }| d	|
 }|j#                  |d
      5 }|j%                         }ddd       |j#                  |d      5 }|j'                         ddd       t(        j*                  }t,        j.                  j0                  j3                  d|i       d	| j                    d|
 }t,        j5                  |d        d	| j                    d}t,        j7                  |      j8                  }|| _        | j=                          	 j                          |j                          y# t        $ r+ Y 	 |j                          |j                          y#  Y yxY ww xY wc c}w # t         $ r Y w xY w# 1 sw Y   QxY w# 1 sw Y   2xY w# t>        $ r!}tA        dtC        |              Y d}~d}~ww xY w#  Y yxY w# 	 j                          |j                          w #  Y w xY wxY w)z,Handles NAS file copying and Dropbox upload.NFr  r  z	/Raw Datar  rN   c              3   \   K   | ]#  }j                         j                  |       % y wrQ   rR   rU   s     rJ   rY   z)handle_nas_and_dropbox.<locals>.<genexpr>  s%     (]L\S););C)@L\rZ   r  r  r  r  r  Tr  zNAS/Dropbox Error: )"r  r  r  r  r  r  r  r  r  r   r  r  r  r  r_   r  IOErrorr  r  r  r
   r  r   r  r  r  r  ry   ri   r  r   r   r   r   )r  r  r  r  r  r  rb   rd   rX   r  r  r  r   r!  r"  r#  r$  r%  r&  r'  r   s           `            rJ   handle_nas_and_dropboxr  v  s   ##	  !7!7!9:2++hTHxW\+]	$01L1L0MYW/2;3N3N2O|\<<(5 8!^EqS(]L\(]%]E+^::  h#$AhZ0;+,AhZ8		+t$  "I 	% 			#T*.>9% 	+ %%7<< 97CDk112-zJ<I|$7 $ K778E;;<OPTT*&+
::<99;O 
 
H::<99;S _ 
  	%$ 	+*  	 (
c!fX&''(	::<99;s   	AJ  H% 1	J :IIJ I! ,%J I1"J <I>B5J  J8 %	I.J 0 I IIJ !	I.*J -I..J 1I;	6J >J	J 	J5J0+J? 0J55J? 8J<?K) K"!K)"K&$K)c                       e Zd Zd Zy)bulkApprovec                 >   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|j                  j                  d      }|st        ddit        j                        S |rt        |t              st        ddit        j                        S g }g }|D ]}  }	t         j                  j                  |		      j                         }
|
s|j#                  |	       E||
_        |
j%                          |j#                  |	       |d
k(  sst'        |
        |rt        j                  j                  d      }d}d}||d	 d}t)        ||      }t+        |      }|D ]L  }t-        ||t.        j0                  |j2                  g      }|j5                  |d       |j7                          N t        t        j8                  dt;        |       d||d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j<                        cY d }~S d }~ww xY w)Nr   r   r(  r#  r   ro  r  zjob_numbers must be a listr  r  Tr  u6   Success: Client Review Completed – Lionshead Studiosr  r  )ro  r  ri   r  r	  zStatus updated for z product(s))r   r   updated_job_numbersnot_found_job_numbers)r'   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   rl  r   ra   r   r  r)   r*   r(   r
   r   r   r  r  r   r  r   )r   r   r-  r   r   r  ro  r  not_found_jobsr   r  r  r  templater  r  r  adminr   s                      rJ   r   zbulkApprove.post  su   FXN

 C oo$$$,2248
Y 01&:S:STT LL$$X.>!!-0;
Y 45f>Y>YZZ
ZT:Y <=fFaFabb<> z''..*.EKKMK:&'K
# ))K( ! //(($(7KFG+H !%9*FG
 $Hg6Ll+L#))
++	U lK8
ZZ\  %%+C,=+>kJ'+	  u  NYA'0L0LMMNB 
 XiQ(1V1VWWXsb   H' AI# AI# 0.I# A8I# CI# '	I 0%II I# I  I# #	J,%JJJNr   r   rL   rJ   r  r    s	    GXrL   r  c                       e Zd Zd Zy)getAllUsersc                    	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S t        j                  j                  |j                        j                  d      }|j                  j                  d      }|dk(  r|j                  d	      }n/|d
k(  r|j                  d      }n|dk(  r|j                  d      }|j                  j                  dd      }|j                  j                  dd      }t!        ||      }	|	j#                  |      }
g }|
D ]y  }|j                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  |j0                  |j2                  d	}|j5                  |       { t        t        j6                  d|	j8                  |	j:                  |
j<                  t?        |      |d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j@                        cY d }~S d }~ww xY w)Nr   r   r(  r#  rW  ry  r  TrD  	retoucherr  r  r  rK  r   per_pager;  )	r   rn   r   r)  r   
last_loginr   rc  r*  r   )r   r   total_usersrF  rG  r  r   )!r'   r   r   r   r   r   r   r   r   r   r   r5  r   rs  r1  r   r;   get_pagern   r   r)  r   r  r   rc  r*  ra   r   r   rM  rN  r   r   )r   r   r-  r   r   all_user	user_typerK  r  rL  rG  r}  r   r.  s                 rJ   r   zgetAllUsers.get   s+   8VN

 C oo$$$,2248
Y 01&:S:STT oo%%8;;%7@@G8 {{v&98.H[ D1HW-H ++//&!
$4kkooj"-88,9$$T*<8t77YYjj&&jj??..&&
H OOH  %%??&& ''H  S  NYA'0L0LMMNf 
 V
Is1v&v/T/T
UUVsN   H AI F7I 	I%III II 	J%J=JJNrz  r   rL   rJ   r  r    s    9VrL   r  c                       e Zd Zd Zy)
deleteUserc                 l   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  j                  d      }|st        ddit        j                        S t        j                  j                  |      j                         }|st        ddit        j                        S |j                  |k(  rt        ddit        j                        S |j                          t        t        j                   d| dd	      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j"                        cY d }~S d }~ww xY w)
Nr   r   r(  r#  r$  z"You cannot delete your own accountzUser z deleted successfullyr  )r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rY  r   r   )r   r   r-  r   r   r$  delete_users          rJ   r   zdeleteUser.post>  sz   VN

 C oo$$$,2248
Y 01&:S:STT\\i(7
Y 01&:S:STT''7'399;;
Y 01&:S:STTnnY DEfNiNijj
%%gY&;<  '  NYA'0L0LMMN0 
 V
Is1v&v/T/T
UUVs`   D> AE: 9E: AE: "+E: /E: >	E7%E2,E7-E: 2E77E: :	F3%F.(F3.F3Nr   r   rL   rJ   r  r  =  r  rL   r  c                       e Zd Zd Zy)
createUserc                 z   	 	 t        |      }t        j                  j                  |      j                         }|st        ddit        j                        S |j                  st        ddit        j                        S |j                  j                  d      }|j                  j                  d      j                         j!                  dd	      j#                         }|j                  j                  d
      }|j                  j                  dd      }|j                  j                  dd      }	|j                  j                  dd      }
|r|st        ddit        j$                        S t        j                  j                  |      j'                         rt        ddit        j$                        S d	j)                  t+        j,                  t.        j0                  t.        j2                  z   d            }t        j                  j5                  |||||	|
t7        |            }	 t9        dd| d| d| dt:        j<                  |gd       t        t        j@                  d|jB                  d      S # t        $ r0}t        dt        |      it        j
                        cY d }~S d }~ww xY w# t        $ r3}t        ddt        |       it        j>                        cY d }~S d }~ww xY w# t        $ r0}t        dt        |      it        j>                        cY d }~S d }~ww xY w)Nr   r   r(  r#  zPermission deniedrn   r   rF   rj   r)  r   Frc  r*  zName and Email are requiredr   zEmail already registeredr;  r  )rn   r   r)  r   rc  r*  r   zYour Account CredentialszHello z),

Your account has been created.
Email: z
Password: z*

Please change your password after login.)r  r   r  recipient_listfail_silentlyz'User created but failed to send email: z6User created successfully. Credentials sent via email.)r   r   r$  )"r'   r   r   r   r   r   r   r   r   r   r   r*  r  r   r   r   rG   rS   r   r  r  r   r  r  r  r	  r   r<   r   r
   r   r   r  r   )r   r   r-  r   creatorrn   r   r)  r   rc  r*  r   r  mail_errs                 rJ   r   zcreateUser.posta  s   >VN

 C __##s#+1137
Y 01&:S:STT


Y 34V=V=VWW ,,

6
"4<<G$**,44S"=CCE5,,"">2<||U39,,"">59<llz518
eY =>vGbGbcc oo5)002Y :;FD_D_`` ggfnnV%9%9FMM%IRPQ8 oo$$	
8$ % 8	J'dVGwl[cZd  eQ  R((G **N{{  g  NYA'0L0LMMN`  JY"I#h- YZci  dI  dI  J  JJ 
 V
Is1v&v/T/T
UUVs   J AL (L C1L 6A
L A4L 6)K &L 	J?%J:4J?5L :J??L 	K>(K93K>4L 9K>>L 	L:
%L5/L:5L:Nr   r   rL   rJ   r  r  `  s    ?VrL   r  )rest_framework.viewsr   rest_framework.responser   rest_frameworkr   r   r   r   r	   django.confr
   passlib.hashr   r   r  rest_framework.authenticationr   rest_framework_simplejwt.tokensr   !rest_framework_simplejwt.backendsr   r   django.shortcutsr   r   r   mathr  django.db.modelsr   r   django.core.mailr   modelsr   r   r   r   r   r   r   r   r   r    r!   serializersr"   r#   r$   pandasr  rest_framework.paginationr%   panel_api.functionsauthenticationr'   r[   r(   django.template.loaderr)   django.utils.htmlr*   django.utilsr+   r  django.core.management.baser,   django.utils.timezoner-   r.   r/   rp   tasksr0   r1   dropbox.filesr2   r3   dropbox.exceptionsr4   dropbox.sharingr5   r  r  r  django.db.models.functionsr6   r7   collectionsr8   r9   r:   django.core.paginatorr;   django.contrib.auth.hashersr<   django.corer=   DROPBOX_APP_KEYrs   DROPBOX_APP_SECRETrt   DROPBOX_REFRESH_TOKENrr   rq   r   r  r  r  r   r  rK   rg   r   r   r   r   r   r   r  r
  r  r  r   r&  r:  rS  r  r  r  r  r,  rB  rI  rQ  r\  rw  r  OPEN_AI_KEYapi_keyr  r  r  r  r  r  r  r/  r3  r=  rA  rI  rP  rU  rZ  r^  rc  rj  rn  ru  ry  r  r  r  r  r  r  r  r  r  r  r  r   rL   rJ   <module>r     ss   ( , !  , ,   8  B 8 : % : :      & ( q  q  q  q ` `  : ! ) B 	 3 3 (  !  3 0 ; ;  2  7 ' =  	  @ # 1 " + 5 &  
"
"((
.. goo#	 	6 -,(XV7 V2&T7 T2%T %TP#V #VL"U "UHPg P,T7 T0V V<'VG 'VVQT QTn2 @@T @TFYq' YqXVw VHQT' QTh=V =VBTW T(XV XVz-VW -VbjV jV^Vg V8LVw LV` %%C^w C^LV w V rv  v vp ' p h(T' (TV^W ^D;<w ;<zE<g E<XKO KO\IT' ITV-T' -Tb$V7 $VRTw T.Vw VDVw V,V7 V>Vg V>V Vp!V !VH3T' 3TnTW T<V V|Vw |V@[Vg [V~Vw VDVW V<04' 04h<|HX' HXV:V' :V|V VF@V @VrL   