
    sdqfx9                        d dl Z d dlZd dlZd dlZd dl mZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlZd dlZd dlZd dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0 d Z1d Z2e ed                         Z3ed             Z4ed             Z5ddZ6ddZ7dS )    N)BytesIO)TemporaryDirectory)Image	ImageDraw	ImageFont)datetime)messages)login_required)IntegrityErrortransaction)HttpResponse)redirectrender)timezone)csrf_exempt)require_POST)A4letter)ImageReader)canvas)api_view)	CustomersCompanyUserBatchCompany)Categoryc                 n    t           j                            |                                           dk    S )Nqr_coder   )r   objectsfiltercount)new_ids    UD:\MyProjects\RESCUE_SCANNER\qr_onkar_130624\qr_onkar\qr_manager\views\qr_genrator.pyis_id_unique_in_databaser%      s-    ##F#3399;;q@@    c                 p   | d} |d}t           j        t           j        z   t                      }t	          |          |k     red                    fdt          |           D                       }t          |          r|                    |           t	          |          |k     et          |          S )N       c              3   @   K   | ]}t          j                  V  d S )N)secretschoice).0_
characterss     r$   	<genexpr>z&generate_unique_ids.<locals>.<genexpr>.   s-      KK
33KKKKKKr&   )
stringascii_lettersdigitssetlenjoinranger%   addlist)lengthr"   
unique_idsr#   r0   s       @r$   generate_unique_idsr=   #   s    ~}%5JJ
j//E
!
!KKKKU6]]KKKKK#F++ 	#NN6""" j//E
!
!
 
r&   c                    	 	 | j         j        j        dz   }n)# t          $ r}t	          |d           d}Y d }~nd }~ww xY wt          | j                            dd                    }t          | j                            dd                    }| j                            dd           }|r t          j	                            |	          }| j                            d
d          }| j                            dd          }| j                            dd          }	|s&t          j        | dd           t          d          S |s&t          j        | dd           t          d          S |s&t          j        | dd           t          d          S ||k    r&t          j        | dd           t          d          S n6# t          $ r) t          j        | dd           t          d          cY S w xY w	 t          ||          }
nM# t          $ r@}t          j        | dt          |           d           t          d          cY d }~S d }~ww xY w| j         }	 t           j	                            |||||                                           t          j                  t          |j        j                  z  _                                         j        r3|j        xj        j        z  c_        |j                                         t/          j                    5  g }|
D ]}	 ||z   }t3          j        dt2          j        j        dd          }t	          |           |                    |           |                    d           |                    ||          }|                     d           }tC          j"        |          }tG          j$                    }|%                    d!||"          }|j&        |d#         z
  d#z  }|j'        |d$         z
  d%z
  }|(                    ||f|||&           tS                      }|                    |d'(           |*                    d)           |+                    ||,                                f           {# t          $ rP}t	          |           t          j        | d*| d           t          d          cY d }~c cd d d            S d }~ww xY w	 d d d            n# 1 swxY w Y   n# tZ          $ r0}t          j        | d+d           t          d          cY d }~S d }~wt          $ r?}t	          |           t          j        | d,d           t          d          cY d }~S d }~ww xY w	 t\          j	        /                    fd-|D                        |	dk    rta          |          }n|	d.k    rtc          |          }|S # t          $ r?}t	          |           t          j        | d/d           t          d          cY d }~S d }~ww xY w)0N/	exceptionhttp://rescuescanner.in/r;      r"   r)   category)pkcolorblackbgcolortransparentdownload_optionpdfzPlease select a categorydanger
extra_tags/qr_generatorzPlease select a colorz Please select a background colorz-Color and background color cannot be the samezInvalid length or count valuezError generating unique IDs: )
created_byr"   rC   rE   rG      
   versionerror_correctionbox_sizeborderTfit
fill_color
back_colorRGBAr   r   font         r_   fillPNGformatr   z1)Error creating QR code for zDatabase error occurredzUnexpected error occurredc                 8    g | ]\  }}t          |           S ))r   batch)r   )r.   coder/   	new_batchs      r$   
<listcomp>z$generate_qr_code.<locals>.<listcomp>   s7     '
 '
 '
9@qId)444'
 '
 '
r&   zipError creating PDF or ZIP)2usercompanyredirect_url	ExceptionprintintPOSTgetr   r    r	   errorr   
ValueErrorr=   strr   createsaver"   cost_per_qr_codeqr_generation_pricetotal_amountr   atomicqrcodeQRCode	constantsERROR_CORRECT_Hadd_datamake
make_imageconvertr   Drawr   load_defaulttextbboxwidthheighttextr   seekappendgetvaluer   r   bulk_createcreate_pdf_with_qr_codescreate_zip_with_qr_codes)requestBASE_URLer;   r"   category_idrC   rE   rG   rI   unique_hash_codesro   
list_of_qrunique_hash_codeurlqrimgdrawr_   	text_bboxxybuffereddownload_responserk   s                           @r$   generate_qr_coder   6   se   !)	2|+8<HH 	2 	2 	2!K   1HHHHHH	2 W\%%h2233GL$$Wa0011l&&z488 	<'++{+;;H  '22,""9m<<!,**+<eDD 	-N7$>8TTTTO,,, 	-N7$;QQQQO,,, 	-N7$FS[\\\\O,,,GN7$S`hiiiiO,,,   ) ) )w ?HUUUU((((())/>> ) ) )w HA H HU]^^^^(((((((() <D;M((DPX`e18 ) : :	(+IO(<(<s4<C`?a?a(a	%( 	 L%%)FF%%L!! $	5 $	5J$5 "5 "5 !5"%55C !)/)9)I!# 	  B #JJJKK$$$GGG%%%--5W-MMC++f--C$>#..D$133D $f6FT R RIYq\1a7A
Yq\1A5AIIq!f&6TINNN&yyHHHXeH444MM!$$$%%'79J9J9L9L&MNNNN  5 5 5!HHHN7,^L\,^,^kstttt#O444444444I$	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5B5?"5$	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5 $	5X    w :xPPPP}}   aw ;QQQQ}}
)%% '
 '
 '
 '
DN'
 '
 '
 	 	 	 e## 8Y O O%% 8Y O O !  ) ) )aw ;QQQQ(((((((()s   G 
?:G ?DG 'G 0'G +G 0G87G8<H 
I5III"CT$ .T7E5R.,T.
T87T/T0T6T$ TTT$ TT$ T T$ $
V%.%UV%V%&4V V% V%)AX 
Y4YYYc                    |sd}g }t           j                            |          }t          j                            |          }d |D             }|D ]}	 	 | j        j        j        dz   }n)# t          $ r}	t          |	d           d}Y d }	~	nd }	~	ww xY w||z   }
t          j        dt          j        j        d	d
          }t          |
           |                    |
           |                    d           |                    |j        |j                  }|                    d          }t)          j        |          }t-          j                    }|                    d||          }|j        |d         z
  dz  }|j        |d         z
  dz
  }|                    ||f|||j                   t9                      }|                    |d           |                    d           |                    ||                                 f           # t          $ rB}	t          |	d           tC          j"        | dd           tG          d          cY d }	~	c S d }	~	ww xY w	 |dk    rtI          ||          }n|dk    rtK          ||          }|S # t          $ r0}	tC          j"        | dd           tG          d          cY d }	~	S d }	~	ww xY w)NrJ   id)ri   c                     g | ]	}|j         
S  r   r.   customers     r$   rl   z"download_batch.<locals>.<listcomp>       HHHh)HHHr&   r?   r@   rA   rP   rQ   rR   TrW   rY   r\   r]   r^   r`   ra   rb   rc   re   rf   r   rw   Error creating QR coderK   rL   rN   rm   rn   )&r   r    rv   r   r!   ro   rp   rq   rr   rs   r   r   r   r   r   r   r   rE   rG   r   r   r   r   r   r   r   r   r   r   r{   r   r   r   r	   rw   r   r   r   )r   batch_idrI   r   ri   all_customersr   r   r   r   r   r   r   r   r_   r   r   r   r   r   s                       r$   download_batchr      s5      JM**E%,,5,99MHH-HHH- &- &-%	-6"</<S@ 6 6 6a%%%56 --C!'!1!A	  B #JJJKKGGG--5;5=-QQC++f%%C>#&&D)++Df.>TJJIYq\)a/A
Yq\)A-AIIq!f.TILLLyyHHHXeH,,,MM!/1B1B1D1DEFFFF 	- 	- 	-!WN7$<RRRRO,,,,,,,,,,	-)e## 8U K K%% 8U K K   ) ) )w ;QQQQ(((((((()sa   A/.H/
B9BHBFH
I*(5I%I*%I*..J 
K'%KKKc                    |sd}g }t           j                            |          }t          |          dk    r|d         j        }n&t          j        | dd           t          d          S d |D             }	 |D ]
}	 	 | j        j	        j
        d	z   }n)# t          $ r}	t          |	d
           d}Y d }	~	nd }	~	ww xY w||z   }
t          j        dt          j        j        dd          }|                    |
           |                    d           |                    |j        |j                  }|                    d          }t/          j        |          }t3          j                    }|                    d||          }|j        |d         z
  dz  }|j        |d         z
  dz
  }|                    ||f|||j                   t?                      }|                     |d           |!                    d           |"                    ||#                                f           # t          $ rE}	t          |	d           t          j        | d| d           t          d          cY d }	~	c S d }	~	ww xY wnM# t          $ r@}	t          |	d           t          j        | dd           t          d          cY d }	~	S d }	~	ww xY w	 |dk    rtI          ||d|d         j%                  }n#|dk    rtM          ||d|d         j%                  }|S # t          $ r0}	t          j        | d d           t          d          cY d }	~	S d }	~	ww xY w)!NrJ   r   r   zError downloading QR coderK   rL   rN   c                     g | ]	}|j         
S r   r   r   s     r$   rl   z*download_individual_qr.<locals>.<listcomp>   r   r&   r?   r@   rA   rP   rQ   rR   TrW   rY   r\   r]   r^   r`   ra   rb   rc   re   rf   rw   zError creating QR code for r   
individualrm   rn   )'r   r    r!   r6   ri   r	   rw   r   ro   rp   rq   rr   rs   r   r   r   r   r   r   r   rE   rG   r   r   r   r   r   r   r   r   r   r   r{   r   r   r   r   r   r   )r   cust_idrI   r   r   ri   r   r   r   r   r   r   r   r   r_   r   r   r   r   r   s                       r$   download_individual_qrr      s      J%,,,88M
=Qa &w ;QQQQ(((HH-HHH,) 1 &	1 &	1%1:&|3@DHH  : : :![)))9HHHHHH: !11]%+%5%E	   C   D!!!mmu{u}mUUkk&)) ~c** -// MM&2BMNN	Y1-!3J1-1		1a&"2EK	PPP"99%000a   !!#3X5F5F5H5H"IJJJJ 1 1 1a!!!w(XFV(X(Xemnnnn00000000001G&	1P  ) ) )aw 8XNNNN(((((((())e## 8ULZghiZjZr s s%% 8ULZghiZjZr s s   ) ) )w ;QQQQ(((((((()s   ;J
 BH7
B=!B83H78B==E8H75J
 7
J8J9J:J
 JJ
 

K5K	KKAL! !
M+%MMMc                    t          d          }|dk    r
d| d|d<   nd|j         d|d<   t          j        |t                    }t          \  }}t          |           D ]}\  }\  }	}
|dk    r|                                 t          |
          }t          |          }|	                                \  }}|
                    |||z
  d	z  ||z
  d	z  ||
           ~|                                 |S )Nzapplication/pdfcontent_typer   attachment; filename="qr_codes_z.pdf"Content-Disposition)pagesizer   r`   )r   r   )r   batch_numberr   Canvasr   	enumerateshowPager   r   getSize	drawImager{   )r   rk   	called_byr   responsecr   r   ir   img_datar   r   	img_width
img_heights                  r$   r   r   :  s   ):;;;HL  *UB*U*U*U&''*iILb*i*i*i&'h,,,AME6+4Z+@+@ 	q 	q''hq55JJLLL8$$(## #	: 	
C%)+q06J3F!2KS\eoppppFFHHHOr&   c                 l   t          j                    }t          j        |dt          j                  5 }| D ] \  }}| d}|                    ||           !	 d d d            n# 1 swxY w Y   |                    d           t          |d          }	|dk    r
d| d|	d	<   nd|j         d|	d	<   |	S )
Nwz.pngr   zapplication/zipr   r   r   z.zip"r   )	ior   zipfileZipFileZIP_DEFLATEDwritestrr   r   r   )
r   rk   r   r   
zip_bufferzipfr   r   img_filenamer   s
             r$   r   r   T  s    J	S'*>	?	? 24*4 	2 	2&h.444LMM,1111	22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 OOA J5FGGGHL  *UB*U*U*U&''*iILb*i*i*i&'Os   $A%%A),A))NN)8r   osr,   r   r   tempfiler   PILr   r   r   r   r   randomr2   django.contribr	   django.contrib.auth.decoratorsr
   	django.dbr   r   django.httpr   django.shortcutsr   r   django.utilsr   django.views.decorators.csrfr   django.views.decorators.httpr   reportlab.lib.pagesizesr   r   reportlab.lib.utilsr   reportlab.pdfgenr   rest_framework.decoratorsr   accounts.modelsr   r   r   r   qr_manager.modelsr   r%   r=   r   r   r   r   r   r   r&   r$   <module>r      so   				 				         ' ' ' ' ' ' + + + + + + + + + +          # # # # # # 9 9 9 9 9 9 1 1 1 1 1 1 1 1 $ $ $ $ $ $ - - - - - - - - ! ! ! ! ! ! 4 4 4 4 4 4 5 5 5 5 5 5 . . . . . . . . + + + + + + # # # # # # . . . . . . B B B B B B B B B B B B & & & & & &
A A A  & v) v)  v)t 9) 9) 9)z E) E) E)R   4     r&   