
    iI                        d dl mZmZ d dlZd dlZd dlmZ d dlmZmZ d dlmZ d dl	Z	d dl
Z
d dlZd dl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Zd dlZd dlZd dlZd dlZd dlZd dl Z!d dl"Z"d dl
Z
d dlZd dlZd dlZ#d dl$Z$d dl%Z%d dl$Z$d dl&Z&d dl'Z'd dl(Z(d dl)Z)d dl*Z*d dl+Z+d dl*m,Z,m-Z-m.Z. d dl)m/Z/ d dlmZ d dlm0Z0 d d	lm1Z1 d d
l2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD d dlEmFZF d dlGZGd dlHZHd dlIZId dlJmKZK d dlL dZMdeMz   dz   e	j                  d<   g dZOg dZPd ZQd(dZRd ZSd  ZTd! ZUd" ZVd# ZWd$ ZXd% ZYd& ZZd' Z[y))    )HTMLCSSN)datetime)r   	timedelta)EmailMessage)SMTP_SSLSMTPSMTPAuthenticationError)create_default_context)date)	randrange)encoders)MIMEBase)MIMEMultipart)MIMEText)MIMEApplication)urlretrieve)default_backend)hashes)
PBKDF2HMAC)Fernet)relativedelta)Image)*z#/var/www/reporter.gc-track.com/app/ /temp/images/MPLCONFIGDIR)LunesMartesu
   MiércolesJuevesViernesu   SábadoDomingo)enerofebreromarzoabrilmayojuniojulioagosto
septiembreoctubre	noviembre	diciembrec                     t        |j                         t        d      D ]  }t        j                  |||   |       }  | S )NT)keyreverse)sortedkeyslenresub)string
dictionaryitems      //var/www/reporter.gc-track.com/app/lib/utils.pyfind_replacer;   E   s=    z(c4@ 8j.78M    c                 j    t         j                  dj                  fdt        |       D              S )Nr   c              3   H   K   | ]  }t        j                          y wN)randomchoice).0iletterss     r:   	<genexpr>zrandomString.<locals>.<genexpr>L   s     Ga6==)Gs   ")r7   ascii_lowercasejoinrange)stringLengthrD   s    @r:   randomStringrJ   J   s(    $$G77G53FGGGr<   c                  h   dt         z   dz   } t        j                  |       D ]  }t        j                  j	                  | |      }	 t        j                  j                  |      st        j                  j                  |      rt        j                  |       n4t        j                  j                  |      rt        j                  |        dt         z   dz   }t        j                  |      }|D cg c]  }|j!                  d      s| }}|D ]7  }t        j                  j	                  ||      }t        j"                  |       9 y # t        $ rT}t        t        j                        dk7  r-t        t        j                        dk7  rt        d|d|       Y d }~d }~ww xY wc c}w )	Nr   r   createrevisionlesszFailed to delete z
. Reason: /temp/z.html)dirpathoslistdirpathrG   isfileislinkunlinkisdirshutilrmtree	Exceptionstrargs	executionprintendswithremove)	folderfilename	file_pathe	directoryfiles_in_directoryfilefiltered_filespath_to_files	            r:   deleteTempFilesri   N   s@   Z'FJJv& 	JGGLL2		Jww~~i(BGGNN9,E		)$y)i(	J 7
8#II.'9TtT]]7=SdTNT  ww||It4
		,   	J4>>"h.3t~~3F.3X9aHI	J Us&   BE5F/F/	F,A	F''F,c                     d}	 t        j                  | d         }|d   D ]  }|dk(  s	|dz   } |S # t        $ r |cY S w xY w)Nr   jockey_activationsactivations   )pd	DataFrameKeyError)datajockeyCountdfys       r:   getJockeyZeroesru   b   sa    K\\$345M" 	*AAv'M	*  s   %3 3 A Ac                     	 t        | t        dz          t        dz   }t        j                  |      }|j	                  d       |j                  t        dz          y# t        $ r Y yw xY w)Nz!/temp/images/logo_client_temp.png   K   z(/temp/images/logo_temp_client_png_re.pngr   z'temp/images/logo_temp_client_png_re.pngr   rO   r   open	thumbnailsaverY   logoUrlconverted_pngimages      r:   convertClientLogor   m   sg    GG$GGH CC

=)	"

7EEF 5     AA 	A)(A)c                     	 t        | t        dz          t        dz   }t        j                  |      }|j	                  d       |j                  t        dz          y# t        $ r Y yw xY w)Nz"/temp/images/logo_company_temp.pngrw   z)/temp/images/logo_temp_company_png_re.pngr   z(temp/images/logo_temp_company_png_re.pngrz   r~   s      r:   convertCompanyLogor   x   sg    GG$HHI DD

=)	"

7FFG 6  r   c                     g }t        j                  |      D ]'  \  }}}|D ]  }t        j                  ||       s|} ' ) |S r?   )rP   walkfnmatch)patternrR   resultrootdirsfilesnames          r:   	find_filer      sQ    FWWT] dE 	DtW-	
 Mr<   c                    t        j                  |       }	 t        |j                             d|j                   dt
        |j                  dz
      d|j                   d|j                  d       	}|S # t        $ r d}Y |S w xY w)Nz, z de rm   z del z, a las z%H:%M:%S-)

dateparserparseweekdaysweekdaydaymonthsmonthyearstrftimeAttributeError)	timestamp
start_timeformatted_dates      r:   formatDateAlertsr      s    %%i0
	!
**,-.b0@VJL\L\_`L`EaDbbghrhwhwgx y((457    	! N	!s   AA5 5BBc                     dt         z   dz   | z   }t        j                  j                  t         d|      }t	        |      j                  |d       y )Nr   rN   tempzpdf/a-3u)pdf_variant)rO   rP   rR   rG   r   	write_pdf)ra   pdfnamerf   output_paths       r:   generatePDFr      sH    w;x(D'',,w8KJ  r<   c                     	 t        j                  |        y # t        $ r Y y t        $ r}t	        d|        Y d }~y d }~ww xY w)Nz!ERROR EN CREACION DE DIRECTORIO: )rP   makedirsFileExistsErrorrY   r]   )DirNamerc   s     r:   createReportDirr      sA    7
G  71!5667s    	AA>Ac                 \   t        j                  t        |      d      }t        |j                  |j                  d      }|j                  dk(  rt        |j                  dz   dd      }n$t        |j                  |j                  dz   d      }|t        d      z
  }t        | ||      d   S )Nz%Y-%mrm      )secondsrecords)r   strptimerZ   r   r   r   getNewPumpInfo)redboxIdr   selectedDate	startDatenext_month_startendDates         r:   getStateRecordsr      s    $$SY8L**L,>,>BIR#L$5$5$91a@#L$5$5|7I7IA7MqQ1!55G(Iw7	BBr<   )
   )\
weasyprintr   r   time
subprocessr   r   email.messager   rP   jsonpandasrn   matplotlib.patchespatchesmpatchesnumpynppylabpltrequestsmatplotlib.pyplotpyplotmatplotlib.datesdatesmdate
matplotlibmplr@   r7   sysr5   urllib.requesturllibunicodedatadtlocalepdfkitargparserW   r   sslsmtplibgcr   r	   r
   r   r   r   emailr   email.mime.baser   email.mime.multipartr   email.mime.textr   email.mime.applicationr   r   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   )cryptography.hazmat.primitives.kdf.pbkdf2r   cryptography.fernetr   dateutil.relativedeltar   cairosvg	tracebackr   PILr   lib.apicallsrO   environr   r   r;   rJ   ri   ru   r   r   r   r   r   r   r    r<   r:   <module>r      s        ( & 	   %          
  	     	        
  	 ; ; & &    $ . $ 2 & 8 1 @ & 0     
/Z7

> W


H (		5	6
7
Cr<   