B
    cb                 @   s  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
 d dlm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 d d	lmZ d d
lmZmZmZmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z(m)Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD e jEFeDdZGe$e.ZHe$e0 ZIdZJe
ddd d!gZKG d"d# d#eLZMG d$d% d%eLZNG d&d' d'eLZOG d(d) d)eLZPdS )*    N)OrderedDict
namedtuple)partial)forms)loader)IncorrectLookupParametersget_content_type_for_model)label_for_field)cache)FieldDoesNotExist)Http404HttpResponseForbiddenHttpResponseJsonResponse)TemplateResponse)reverse)method_decorator)	force_str)overridegettext_lazyactivate)csrf_exemptcsrf_protect)sensitive_post_parameters)settings)models)	HTMLToPDF)AdminRuntimeWarning)time_it)json_response)get_system_setting)SystemSetting)get_real_company_logo_name)ADDITION_FILE_ROOTZreportsTActionDetailklassnamedescc               @   sH   e Zd Zdd Zedd ZdddZeddd	Zd
d Zdd Z	dS )ModelMixinEssentialc             C   s  | j }|j}t|jtjtjfr&d}n|jj}||}| 	|}| 
|}| |}t|}|j|krv|d|j y|d W n tk
r   Y nX | |}	y4|	|| j ||| j| j| j|| j| j| j| j| }
W n. tk
r   dd l}|  tdY nX |
| _|
S d S )Nidr   Zaction_checkboxzIncorrect Lookup Parameters)model_meta
isinstancepkr   ZOneToOneField
ForeignKeyr&   	get_fieldZget_sort_fieldsget_search_fieldsZget_list_displaylistinsertremove
ValueErrorZget_changelistZlist_display_linksZlist_filterZdate_hierarchyZlist_select_relatedZlist_per_pageZlist_max_show_allZlist_editabler   	traceback	print_excr   Zchange_list)selfrequestr*   optsZpk_namepk_fieldZsort_fieldssearch_fieldslist_displayZcls_change_listclr5    r>   ,G:\easytimepro\master/mysite/admin\mixins.py_setup_changelist:   s:    







z%ModelMixinEssential._setup_changelistc             C   s  ddl m}m} ddlm} | j}|\}}y| |}W n0 tk
rh }	 ztdt	|	dS d }	~	X Y nX d|j
krd|j
kr| }
t|
S | |}d}g }d	}y<||jjt|d }|d krt| d
d rt| d
}d}W n ttfk
 r   d }Y nX y||jjt|d }W n ttfk
r8   d }Y nX x|||t| dd ||D ]}|| qTW t|dd	|dd	gr|t|dd	r|dd	rdnddd|d | |rd|d d< d|d d< | jd| }| jd| }d| d }| jdd }td}| |}|| d |d!t}t|}|| d"|d#t }|dkrp|nd$}|dd	d	d%d|g| ||d&d'd(d)d*||||||||||||rtt!|| d+dndd,}t|S )-Nr   )load_from_json	get_model)result_headers)codemsgpagelimitget_col_modelFZhidden_fieldsTZlist_display_paramsZchangedelete\   .   centerz#model_{}_toolbar)widthaligntoolbar	editPopUp   eventztext-decoration: underline;stylez	%s_%s_delz
%s_%s_editz%s/%sz/export/z
%s_%s_init)baseZadminlogzbiotime:disabled_fieldshistory_optsz[]except_limit02      table)rH   Z
showSelectZcanSelectRowZ	showStyleZedit_colZ	autowidthZcolsurlr)   Zasc)fieldtyper-   )Z	grid_optsZinitSortaction_choicesdisabled_fieldsuse_backend_hidden_fieldsdel_urledit_url
export_urlhistory_urldisabled_fields_url	all_perms
model_name	app_labelZhistory_contenttype)"mysite.admin.utilsrA   rB   mysite.admin.admin_listrC   infor@   r   r   strGETZget_json_result_get_all_permsuserr`   dictgetgetattrAttributeErrorKeyErrorZcolumn_orderappendanyformatZhas_change_permission
admin_siteget_urlr   get_action_sets	get_paramr1   boolintr   )r7   r8   rA   rB   rC   rl   ri   rh   r=   eresultsrg   Z
_col_modelZ	col_modelra   r`   Zuser_prefer_column_ordercolrb   rc   rd   re   rf   r_   rV   
is_historyrW   Z	limit_numZresponse_dictr>   r>   r?   
table_view`   s     





"
"zModelMixinEssential.table_viewNc             C   s   ddl m} ddlm} ddlm} | j\}}| |}| | || d|dt	}	t
|	}
| ||d||
rxd| n|t|	|d	}||pi  || ||
d
}|| |}|| || |S )Nr   )rA   )get_filters_plugin)get_bookmark_contextrV   z[]r[   Zhistory_opts_)Z
table_pathri   rh   rV   rg   )r   )rj   rA   Z"mysite.admin.xadmin.filters_pluginr   r   rl   ro   r@   r|   r1   r}   rz   jsondumpsupdate)r7   r8   extra_contextrA   r   r   ri   rh   rg   rV   r   contextZfilter_contextZbookmark_contextr>   r>   r?   init_view_context   s&    





z%ModelMixinEssential.init_view_contextc       	   
   C   s|   | j }|j}| j\}}y| ||}W n0 tk
rV } ztdt|dS d }~X Y nX t|| jpvd	||j
 dg|S )NrD   )rE   rF   z{0}/{1}/grid.htmlzdata_grid.html)r*   r+   rl   r   r   r   rm   r   Zgrid_templaterx   Zobject_namelower)	r7   r8   r   r*   r9   ri   rh   r   r   r>   r>   r?   	init_view   s    
 zModelMixinEssential.init_viewc             C   s@   ddl m} ||kr<t| dj|dd }|r<t|r<||S d S )Nr   )NATIVE_PERMISSION_SETzhas_{verb}_permission)verb)rj   r   rs   rx   callable)r7   r   r8   r   Zpre_permission_check_methodr>   r>   r?   native_permission_check   s    z+ModelMixinEssential.native_permission_checkc                s  ddl m} ddlm} ddlm} ddlm} ddlm} y|j	j
||d}W n |jk
rf   dS X g }	xL j d d	 d d d D ],}
t|
d
g }|r|	 fdd|D  qW x\ j d d	 d d d D ]<}
t|
dg }|rx&|D ]}|	 fdd|jD  qW qW td |	}	t }x|	D ]\}}}|dkrHq.n||kr.tdv d|||}dj||d}y(|j	j|||d\}}|r|  W n ||fk
r   w.Y nX || W d Q R X q.W t|S )Nr   )ContentType)
Permission)hungary_notation)ProgrammingError)DatabaseError)ri   r*   rD   rR   actionsc                s   g | ]}  |qS r>   )
get_action).0action)r7   r>   r?   
<listcomp>  s    z9ModelMixinEssential.handle_permission.<locals>.<listcomp>action_setsc                s   g | ]}  |qS r>   )r   )r   r   )r7   r>   r?   r     s    ZGeneralActionDeleteZenz{0}_{1}zCan {verb} {target})r   target)r&   Zcodenamecontent_type)Z"django.contrib.contenttypes.modelsr   Zdjango.contrib.auth.modelsr   rj   r   	django.dbr   r   objectsrr   ZDoesNotExist	__class__mrors   extendclsfiltersetr   rx   Zget_or_createsaveaddlen)r7   ri   rh   r   r   r   r   r   r   r   r%   Zclass_actionsZclass_action_sets
action_setseenZ_klassr&   r'   Z	code_name_nameobjZis_newr>   )r7   r?   handle_permission  sJ    ""
"





z%ModelMixinEssential.handle_permission)N)N)
__name__
__module____qualname__r@   r   r   r   r   r   r   r>   r>   r>   r?   r(   8   s   &n
	r(   c               @   sz   e Zd Zedd ZdddZdddZee d	d
 Z	dd Z
dddZdddZdd Zedd Zedd ZdS )ModelMixinExtendc             K   sp   d }xZ| D ]R}| |}|r&|d kr&q
|d krD|jf d|i|}q
||jf d|i|7 }q
W |d krlg S |S )Nnode)get_nodeZto_simple_data)rain_forestnidkwargsresulttr   r>   r>   r?   _get_tree_result5  s    

z!ModelMixinExtend._get_tree_result Nc             C   sB   | j j| j}t|dr2t|d}t|r2| }| |d||S )Nr   1)ry   	_registryrr   r*   hasattrrs   r   Z_changeform_view)r7   r8   Zform_urlr   model_adminZadmin_contextr>   r>   r?   	page_viewB  s    

zModelMixinExtend.page_viewc             C   s<   ddl m} ddlm} | jj|}|||}|||S )Nr   )ZKModelAdmin)AdminLog)mysite.admin.kernelr   Zmysite.base.modelsr   ry   r   rr   r   )r7   r8   r   r   r   r   instancer>   r>   r?   model_history_viewJ  s
    
z#ModelMixinExtend.model_history_viewc       ,   
   C   s  ddl m} ddlm} ddlm} t| j|d}yt|d}W n t	t
fk
r^   d }Y nX |dd d}|d	}|d
}	|d ks|	d krt t| j|	d }
|
jj}|
jj}| jjj}|jj}i }d}|d krd}nd}|t||d |jj|ddjf |d |fi}x6|jjD ]&}t||r||jkr|j}P qW d S d||}d||}t|d }t|d }|d k	r|d k	ry,x&|D ]}|||  }d||_qW W n8 t t!fk
r } zt	dt"| W d d }~X Y nX |j#||f|S d}d}|d i}|jjf |}|jj$f |}t% }g } i }xHt&|D ]<\}!}"| }#|'|# |!||"j(< |#j)t|"j(t*|"d qFW xF|D ]<}$i }%t*|$}&t|$|d }'tt|$|jj(j}||ko|dkp|}(|(dkr|dkrd}t|'j(|%d< ||'j(d })|)d krt+d||) }#|#d k	r:|)||< nt	d|#|'j(d krf|#j)t|'j(d |#|}*|*d k	r|&|'r|'j(pd  |*_,|*_-n"|#j)|&||'rt|'j(pd d}*|(r| '|* qW t.||d t.||d x| D ]}+d|+_qW |j#||f|S )Nr   )r   )Tree)r.   )r8   r   level_limit)defaultZobj_idasync_fieldTF)has_rootlevel)ZflatZ__inz
{}-{}-treez	{}-{}-mapzInconsistent cache: )identitytagZpidz'Improper mapping in tree_root_to_forestzImproper mapping in rain_forest)r   )r   r   parentiQ )/r   r   Zmysite.admin.tree_structurer   Zdjango.db.models.fields.relatedr.   r   r|   r~   r4   	TypeErrorr   rs   r*   ZreltoZthroughr+   rh   r   rq   r   Zvalues_listr   fieldsr,   r&   rx   r
   rr   r   checked
IndexErrorru   rm   r   Zexcluder1   	enumeraterv   r-   Zcreate_noder   IOErrorr   r   r   ),r7   r8   r   r   r.   Z
_get_paramr   r   Z_idZ_fieldr   Zasync_modelZ	m2m_modelZsrc_model_nameZdst_model_namer   r   Zselected_valuer]   Zself_referred_fieldZ_TREE_CACHE_KEYZ_TREE_CACHE_MAPr   Ztree_root_to_forestvr   r   Zallow_multiple_selectedZhas_selectedZ
conditionsZall_parent_nodeZall_non_parent_nodeZselected_node_of_treeipZtreerecordZ	node_datar&   r   Zselectedidxr   nr>   r>   r?   	tree_viewQ  s    



"







"
zModelMixinExtend.tree_viewc          
   C   s>  dd l }dd l}ddlm} ddlm} ddlm} t }x0|j	
 D ]"\}}	|dkrXqF|||	 qFW yt|j	dd}
W n tk
r   d}
Y nX yt|j	dd	}W n tk
r   d	}Y nX | j}|d k	r|jjr|j j|jj }n|j |jjj}|r| }x^|D ]V}y|j|}W n |tfk
rT   w"Y nX t||r"|||d
|< q"W |jf |}|||}|j}|
|j kr|j!}
|"|
}g }xrt#|j$j%| j& ddD ]X\}}x6|
 D ]*\}}t||j'|j(frt)|||< qW |j|d |*| qW dd||d}|S )Nr   )	CharField)r   )	PaginatorkeyrG   rZ   rH   rY   z{}__icontains)start)hashr   )rE   rF   countdata)+r   datetimeZdjango.db.modelsr   django.core.exceptionsr   Zdjango.core.paginatorr   rq   POSTitemsr   loadsr~   rr   r   r*   r+   Zorderingr   allZorder_byr-   r&   keysr/   rt   r,   poprx   r   r   Z
page_rangeZ	num_pagesrG   r   Zobject_listvalues_list_displaydatetimerm   rv   )r7   r8   r   r   r   r   r   Z	post_argskr   Zpage_num	page_sizer   ZqsZall_keysr]   Zmodel_fieldZ	paginatortotalrG   Zrowsindexr   itemvaluer   r>   r>   r?   data_set  sd    





 zModelMixinExtend.data_setc             K   s   |d krt j}| jd k	r| jS d|kr2|d}n
| |}|sDd S | j|d}tf}t|drj|jtf}t	t
d||}t| j|d}|rt||_| jjt
d }	||d}
t	||	|f|
S )Nr   )r*   r   Meta)r8   Z
SearchForm)r   formfield_callback)r   Z	ModelFormZsearch_formr   r0   r*   objectr   r   r^   rm   r   Zformfield_for_dbfieldstaticmethodr   r   )r7   r8   formr   r;   Zattrsr   metar   
class_nameZform_class_attrsr>   r>   r?   get_search_form  s*    




z ModelMixinExtend.get_search_formTc             C   s.  | j j}|j}g }d}d}|r<|tdd|ddddd	d
 |tt|j|ddddd t| j	}|j|kr~|
|j dtd| t| }	xdt|D ]X\}
}y||j}W n tk
r   wY nX tt||	tt|d}|| qW |r(|tdd|dd|jd |gS )N      r   truerQ   zcursor: pointer; color: #57c5f7leftrM   Znumbers)r]   unresizeZminWidthrS   rT   fixedrO   r^   Zcheckbox)r]   rN   r  rO   r  r^   z{}%`   )r]   rN   titlerightz#model_{}_toolbar)r  rN   rO   rP   )r*   r+   r-   rv   rq   sixZ	text_typer&   r1   r   r3   rx   r~   r   r   r/   verbose_namer   _rh   )r7   Zhash_colZtool_barr9   r:   r   Z	fix_widthZtoolbar_percentr   Zwidth_percentr   fZ
field_nameZcolumn_propertiesr>   r>   r?   rI   1  sV    




zModelMixinExtend.get_col_modelc             C   s  ddl m} ddlm} |jdkr|jdd}|jdd}|jd	d
}|jdd
}t|dkstt|dkrtdt	ddS t
|ts|dkrdnd}t| j}|r|jj||jj|d}	n|jj||j|jj|d}	|	 dkrT|dkr
tdt	ddS |dkrT|	 }
||
_|
  ||
jdd}td||ddS |t| j||j||d}|  td|j||jddddS tdt	ddS d S )Nr   )Bookmark)standard_b64encoder   opnewis_shareFr  r   filtersrD   Z&bookmark_missing_valid_parameters_echo)rE   rF   ZonT)r  
company_idr   )r  rp   r  r   Zbookmark_duplication_echor   zutf-8)r  r  )r   r  rp   r  r  zunsupported request method)Zmysite.base.models.bookmarkr  base64r  methodr   rr   r   r   r
  r,   r}   r   r*   r   r   rp   current_companyr   firstr  r   encodedecoder  )r7   r8   r  r  r  r  Zp_titleZ	p_filtersr   Zfound_bookmarksZ_bksZbookmarkr>   r>   r?   bookmark_viewv  s^    




 zModelMixinExtend.bookmark_viewc             C   s   |j dkrddlm} ||| jj| jjd}|  |  |j}|j	}x|D ]}| }xvt
| jD ]`\}}	t||	}
t|
ddr|
jjjjf | j|	 || i}|rt||	| qdt||	||  qdW |  qNW d S )Nr   r   )
ImportData)Zreqri   rh   r]   F)r  Zmysite.personnel.import_datar  r9   ri   rh   Zvalidate_formatZget_file_datarecordsZ	model_clsr   Zimport_fieldsrs   r]   r*   r   rr   import_related_fieldssetattrr   )r7   r8   r  Z
obj_importr  r9   Z	records_iZopts_ir   Zimport_fields_iZ	file_typer  r>   r>   r?   import_file  s$    


zModelMixinExtend.import_filec             C   s  ddl }yddlm} W n   ddlm} Y nX ddlm} t|d| j	j
 | j	j g }g }x|D ]}|d| qlW | }||}	|	d}
xBt|D ]6\}}d|}|
d|| |
d|t|d	  qW |	  |d t| d
d}d| j	j|d< |S )z
        Response for 'Download Template' in import page'
        :param request:
        :return: return import template
        r   N)StringIO)BytesIO)configz{0}_{1}_TEMPLATEz{0}ZTemplate   zAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet)r   z-attachment; filename={0}_Import_Template.xlsxzContent-Disposition)
xlsxwriter	cStringIOr!  ior"  Zmysite.personnelr#  rs   rx   r9   ri   upperrh   rv   Workbookadd_worksheetr   write
set_columnr   closeseekr   read)r7   r8   r%  r!  r#  r   r	  r  outputwbZsheetr   r   headresponser>   r>   r?   import_template  s0    




z ModelMixinExtend.import_template)r   N)N)N)TT)r   r   r   r   r   r   r   r   r   r   r   r   rI   r  csrf_protect_mr   r4  r>   r>   r>   r?   r   3  s   

 C

EJr   c               @   s   e Zd Zedd Zdd Zedd Zdd Zd"d
dZd#ddZ	d$ddZ
d%ddZd&ddZdd Zdd Zedd Zd'ddZd(ddZdd Zd d! Zd	S ))ModelMixinExportc          
   C   s  y|  |}W n0 tk
r> } ztdt|dS d }~X Y nX t|rT|d  ng }|jdd}|jdd}y|r|d}nt	| dr| j
}n|}y|d W n   Y nX x6t|D ]*\}}|| jkr| j| r| j| ||< qW g }	x&|D ]}||kr||	kr|	| qW |r&|	}W n2 tk
rZ } ztdt|dS d }~X Y nX |d	krxd
}
td|
dS |dkr| ||}nZ|dkr| |||}n@|dkr| ||}n(|dkr| |||}ntdddS |S )NrD   )rE   rF   r   export_headersr   export_type,r<   )csvtxtxlspdfxlsxz0export_type required and must in csv txt xls pdfr:  )r<  r>  r;  r=  z#export_type must in csv txt xls pdf)get_export_datar   r   rm   r   r   rn   rr   splitr   r<   r3   r   export_fields_dictrv   	Exceptionexport_to_csvexport_to_xlsexport_to_txtexport_to_pdf)r7   r8   datasr   Zdata_fieldsr7  r8  r   headerZnew_export_headersrF   r3  r>   r>   r?   export  sX     



 




zModelMixinExport.exportc             C   s.   g }x$|D ]}| tt|| j|  q
W |S )N)rv   rm   r	   r*   )r7   r7  translated_headersrH  r>   r>   r?   get_headers_translation9  s    
z(ModelMixinExport.get_headers_translationc       	      C   s   ddl m} | j}g }x>| jD ]4}|| jkrH| j| rR|| j|  q|| qW || _| |}|| _|j r| j}d| _t	||}|| _|S g S )Nr   )yield_export_resultsr   )
rk   rL  r<   rA  rv   r@   result_listr   Zempty_value_displayr1   )	r7   r8   rL  Ztemp_list_displayZexport_fieldZeachr=   Ztemp_empty_value_displayrM  r>   r>   r?   r?  @  s$    



z ModelMixinExport.get_export_datac             C   s   t | jjjS )N)rm   r*   r+   r	  )r7   r>   r>   r?   get_file_titleY  s    zModelMixinExport.get_file_titleNc                s   t dd}|tj tj|dd}|| | x$|D ] | fdd|D  q:W t|t	sz| 
 tdd  }d	| |d
< |S )Nztext/csv;charset=UTF-8)r   Zexcel)Zdialectc                s   g | ]}  |d qS )r   )rr   )r   r   )r   r>   r?   r   c  s    z2ModelMixinExport.export_to_csv.<locals>.<listcomp>z_%Y-%m-%d-%H-%M-%S_rI  zattachment;filename=%s.csvzContent-Disposition)r   r+  codecsBOM_UTF8r:  writerZwriterowrK  r,   rm   rN  r   strftime)r7   r7  rG  	file_nameZtranslation_headersr3  rQ  r>   )r   r?   rC  ]  s    


zModelMixinExport.export_to_csvc       	         s   t dd}d}d}| |}||||  x2|D ]* | fdd|D }|||  q6W t|ts|  tdd  }d	| |d
< |S )Nz
text/plain)r   r9  z
c                s,   g | ]$}t  |d krdn |qS )Nr   )rm   rr   )r   r   )r   r>   r?   r   r  s   z2ModelMixinExport.export_to_txt.<locals>.<listcomp>z_%Y-%m-%d-%H-%M-%S_rI  zattachment;filename=%s.txtzContent-Disposition)	r   rK  r+  joinr,   rm   rN  r   rR  )	r7   r7  rG  rS  r3  Z
split_charZline_endrJ  Zrow_strr>   )r   r?   rE  i  s    





zModelMixinExport.export_to_txtc       
      C   sj   dd l }yddlm} W n   ddlm} Y nX | }||}| j||||dd |  | }	|	S )Nr   )r"  )r!  Zreport)sheet_items
sheet_name)	r%  r'  r"  r&  r!  r)  _create_xls_sheetr-  getvalue)
r7   r8   r7  rG  rS  r%  r!  Zfile_bufferworkbookxls_datar>   r>   r?   _get_xls_dataz  s    


zModelMixinExport._get_xls_datac       1         sz  t jjd|jjd }d}d}d}	|r`t|j}
|
	dd}	t
|
	d|}t
|
	d|}| |}t|}|d }|d	d
dddddd}d}|d	|ddd}||}|  }| dk ri }x|D ]}d||< qW || g }xVt|D ]J\}}g }x2|D ]*}|ttt|	|ddd  qW || qW g }x |D ]}|t|d  qTW || tttt| }x(t|D ]\}}|||d | qW t|}|tdkr||d dttd d}t|jj}|r$tj|r$ytj !|d} d| j"d  i}!|dkrJ|#dt
|||! n`|dkrd}"x|D ]}|"|7 }"q^W |!d d|"  d |!d< |#d|||! n|#dd||! | j"d d dkr| j"d d d p| j"d d }#||#7 }|dkr|d8 }W n& t$k
r"   dd l%}$|$&  Y nX |r|	r||kr@d}%nd}%|dkrTd}&n"|dkrrd}&||krvd}%nd}&|d	d
|&|%dddd}'|'ddt
|||	|' |d7 }n|d	d
dddddd}'|'t
|dt
|||| |d7 }x@t|D ]4\}(})|(t
||(t|)| ||(|(||(  qW |d7 }g }*d}+x0t|D ]"\},  fdd|D }-|*|- d|kr|-d  dkr|)t
||, d! xrt|-D ]f\}.}/|/sd}/d|kr|+t|-d k r
|(t
||, |.|/| n|(t
||, |.|/| |.}+qW d|krN|-d  dkrNd"t*j+|-d  f }0|#t
||, |+|0d#d#dd$dd% ||+|+d qNW d S )&Ncompany_setting)r&   r  r   r   company_nameZname_posZlogo_posrZ   ZArialZ16rM   ZvbottomT)	font_nameZ	font_sizerO   valignZboldr  r  r  Zvcenter)r^  rO   r_  Zborderr   iclock_model_unregisterZ%disclaimer_unregistered_users_reportsrx_offsetr$  rR   g|?5^@   topr  c                s,   g | ]$}t  |d krdn |qS )Nr   )rm   rr   )r   r   )row_data_dictr>   r?   r     s   z6ModelMixinExport._create_xls_sheet.<locals>.<listcomp>ZphotorD   d   z%s/upload/%sg?   )Zx_scaleZy_scaleZpositioningrb  Zy_offset),r!   r   r   rp   r  r  r   r   r   rr   r~   rK  r   Z
add_formatr*  rN  __len__rv   r   maxrm   r1   mapzipr,  r
  Zwrite_stringr"   ospathexistsPILZImageopensizeZinsert_imagerB  r5   r6   Zmerge_ranger+  Zset_rowr   r#   )1r7   r8   rY  r7  rU  rV  r\  Zcompany_displayZlogo_displayr]  Zcompany_paramsrJ  Zhead_lenZmax_row_indexZtitle_formatZsideZxcell_formatZ	worksheetr  dr   wcellsr   lineZwcellr   rH  ZwcsxrowZ	row_indexZreal_company_logo_nameZimageoptionsZcoloum_sizeZrsr5   r_  rO   Zcompany_formatr   r   Ztotal_row_datacZrow_index_temZrow_dataZ	col_indexZcol_dataZlocal_addressr>   )re  r?   rW    s    






*




6











z"ModelMixinExport._create_xls_sheetc             C   sV   | j |||d d}t|ts2|  tdd  }tdd}d| |d< || |S )N)rS  z_%Y-%m-%d-%H-%M-%S_rI  zAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet)r   zattachment;filename=%s.xlsxzContent-Disposition)r[  r,   rm   rN  r   rR  r   r+  )r7   r8   r7  rG  rS  rZ  r3  r>   r>   r?   rD  $  s    


zModelMixinExport.export_to_xlsc             C   s   yt d|jj}|jj}W n*   t d|jjjj}|jjjj}Y nX t d|}y|d}W n   d}Y nX |d krdddddd}|d	}||fS )
Npdf_report_settingr\  r]  r   AutoPortraitClassic)z	page-sizezfooter-leftzfooter-rightorientationzreport-stylezreport-style)r    rp   r  Z
departmentZcompanyr)   rr   )r7   r8   ry  r  r\  r]  report_styler>   r>   r?   get_export_style-  s     


z!ModelMixinExport.get_export_stylec       	      C   sV   t t t   ddd}dd |D }|\}}}}}dj|||||d}|S )N -c             S   s   g | ]}|r|qS r>   r>   )r   lr>   r>   r?   r   A  s    z6ModelMixinExport.get_generate_date.<locals>.<listcomp>z{t1} {t2} {t3} {t5} {t4})t1t2t3t5t4)r   asctime	localtimereplacer@  rx   )	r7   Z
local_timeZtime_lsr  r  r  r  r  Zresult_timer>   r>   r?   get_generate_date?  s
    "z"ModelMixinExport.get_generate_datec             C   s@  |  ||}|  |d< | |\|d< }|dkr6dnd}|  tdkrNd}|  dkrp|jd	|  |d
< t|	|}|d 
|d  | ||d }| j||d}	tjttd}
|  }td}|dkr|jd	|  |d
< |jd	|}td}d||d }tj|
|}t|||	d}| S )NZ
print_dater]  r|  zbase_pdf_classic_tmp.htmlzbase_pdf_tmp.htmlr`  z'declaimer_unregistered_user_report.html)EmployeeDevicefilenamer  rG  headers)
_page_sizez%Y%mz%Y%m%d%H%M%S)r  r  ZResignz%d-%m-%Y-%H-%M-%Sz%(report_name)s_%(stamp)s.pdf)report_nameZstamp)Zextra_options)get_pdftpl_contextr  r  rN  r
  rn   rr   r   get_templaterenderrv   get_page_sizeget_pdf_optionsrl  rm  rT  EXPORT_ROOTr   rR  r   Zreturn_response)r7   r8   r7  rG  r   r~  Z	html_filehtmlr   rw  Zexport_pathr  Z	file_timerS  	file_pathZ	htmltopdfr>   r>   r?   rF  F  s2    



zModelMixinExport.export_to_pdfc                sX   |  |}g }x$|D ] | fdd|D  qW i }|  |d< ||d< ||d< |S )Nc                s*   g | ]"}  |d krdn
  |dqS )Nr   )rr   )r   r   )r   r>   r?   r   l  s   z7ModelMixinExport.get_pdftpl_context.<locals>.<listcomp>r  r  rG  )rK  rv   rN  )r7   r7  rG  rS  rJ  	data_listr   r>   )r   r?   r  h  s    

z#ModelMixinExport.get_pdftpl_contextc             C   s   t j  }td|jj}|d kr*ddd}|d}|d}|d}|d}d|jd	|   d
|j|df d}	|jdddkr|jd}i }
|r||
d< |r||
d< |r|	|d|
d< |r|	|d|
d< | 	|
|jj |r||
d< |
S )Nry  r   )zfooter-leftzfooter-rightz	page-sizer}  zfooter-leftzfooter-rightz[page]/[toPage]zGenerated On: %sz%s %sz%Y-%m-%d)z[page]/[toPage]ZAuthorZTimezAuthor+Time)r{  	Landscape)
r   Znowr    rp   r  rr   r  rR  rn   
set_header)r7   r8   r  Zdt_nowry  r   r}  Zfooter_leftZfooter_rightZoption_dictrw  r>   r>   r?   r  w  s8    





z ModelMixinExport.get_pdf_optionsc       	   	   C   s   t d|}|rtjtd}d| }tj||}tj|sHt| td	|}t
|d}|| W d Q R X ||d< d S )Nr\  zpdf_header/zpdf_header_%s.htmlzpdf_header.htmlwzheader-html)r    rl  rm  rT  r#   rn  makedirsr   r  r  rp  r+  )	r7   rw  r  r\  Zpdf_header_pathZlogo_html_nameZ	logo_htmlr  r  r>   r>   r?   r    s    

zModelMixinExport.set_headerc             C   s4  |j ddkr(|j d}|dkr(|S td|jj}|d krBi }|j ddkrh|d}|dkrh|S |j ddd	kr|j d}n
|d}d
d |D }tttt| }d| }d}	xdD ]}
||
k r|
}	P qW ddgddgddgddgddgddgddgd}||	ddg}|dkr,|d S |d S )Nzpdf-page-sizeZDefault)A0A1A2A3A4A5ZB0ZB1ZB2ZB3ZB4ZB5ry  rz  z	page-sizer}  r   )r{  r  c             S   s   g | ]}d d |D qS )c             S   s   g | ]}t t|qS r>   )r   rm   )r   r   r>   r>   r?   r     s   z=ModelMixinExport.get_page_size.<locals>.<listcomp>.<listcomp>r>   )r   rt  r>   r>   r?   r     s   z2ModelMixinExport.get_page_size.<locals>.<listcomp>g      @r   )      i)  i  iR  iI  i  r  r  r  r  r  r  r  rZ   )	rn   rr   r    rp   r  sumrj  ri  rk  )r7   r8   r  r   ry  r}  rs  Zmax_lenZstr_sizeZpdf_sizer   Zpage_size_dictr>   r>   r?   r    s@    


zModelMixinExport.get_page_size)NN)N)N)N)N)N)N)r   r   r   r   rI  rK  r?  rN  rC  rE  r[  rW  rD  r  r  rF  r  r  r  r  r>   r>   r>   r?   r6    s"   ;



 
	"

(r6  c               @   sX   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
edd ZdS )ModelMixinActionc             C   sN   xH| j  d d D ]2}t|dg }|s*qx|D ]}t||r0|S q0W qW d S )NrR   r   )r   r   rs   
issubclass)r7   Zsuper_classr%   actions_classa_clsr>   r>   r?   &_get_first_subclass_of_specific_action  s    

z7ModelMixinAction._get_first_subclass_of_specific_actionc          	      s   ddl m jjt|ttfr<|d }d}|d }n|j}d}|g}j	d  t
 } fdd}t||ttd tt|||fS )	Nr   )capfirstTrZ   Fz%s_%s_actionc                s    fdd}|S )Nc       	         s  ddl m} | j}t| dd dks2t| dd dkr6d S |jksH|krLd S ||\}}|}|d k	rt|tr|sd S n|sd S 	| | }t
| dr| j}n|jdd}trt|dd	d}|jd k	rj|j }n }t|t|t|t|jt|jt|jt|j|j|j|j|j|j|jrtttt d
d |j!|j"frxdnddS )Nr   )get_permission_nameZvisibleFshort_descriptionr
  r  zutf-8hexc             S   s   | d kS )Nr>   )ru  r>   r>   r?   <lambda>'      zkModelMixinAction.get_action_set.<locals>.get_action_attr_wrapper.<locals>.get_action_attr.<locals>.<lambda>T)r&   descriptionr\   Ziconhelp_txtr	  confirmationconfirmation_timesaction_confirm_passwordkeep_page_openbatch_selectuniqueZsimple)#rj   r  r   rs   Zactions_disabledr   r,   r}   has_permr   r   r  r  ENCRYPT_ACTION_NAMErO  r  r  
action_urlry   rz   r   Zaction_iconr  r	  r  r  r  r  r  Zunique_object_requiredr   r1   rj  action_formaction_template)	r%   r  action_nameZ	perm_namer   Zcheck_resultr   r  Z_url)r  r  rl   r8   r7   
unique_setrp   r>   r?   get_action_attr  sJ     



zYModelMixinAction.get_action_set.<locals>.get_action_attr_wrapper.<locals>.get_action_attrr>   )r  r  )r  r  rl   r8   r7   rp   )r  r?   get_action_attr_wrapper  s    0z@ModelMixinAction.get_action_set.<locals>.get_action_attr_wrapper)Zdjango.utils.textr  rl   rp   r,   tupler1   r	  ry   rz   r   r   r   rj  )r7   r   r8   Zset_namemultir   r   r  r>   )r  r  rl   r8   r7   rp   r?   get_action_set  s    
3zModelMixinAction.get_action_setc             C   s   |j S )z-
        Hook for specifying fields.
        )r   )r7   r8   r   r   r>   r>   r?   get_action_fields/  s    z"ModelMixinAction.get_action_fieldsc             C   s(   t |dr|jS d d| |||ifgS )N	fieldsetsr   )r   r  r  )r7   r8   r   r   r>   r>   r?   get_action_fieldsets5  s    
z%ModelMixinAction.get_action_fieldsetsc          
      s   ddl m} i }|rV|  ||}x2|D ]*\}}	| fddt|	d D  q(W y||_|jf |S  tk
r }
 z|t	|
W d d }
~
X Y nX d S )Nr   )ActionHandleErrorc                s    i | ]\}} j |d |qS )r   )r   rr   )r   r&   r]   )r8   r>   r?   
<dictcomp>@  s    z9ModelMixinAction.response_action_post.<locals>.<dictcomp>r   )
mysite.admin.actionr  r  r   r  Z	iteritemsZ
valid_formr   rB  rm   )r7   r8   r   r   action_instancer  Zparamsr  r&   Zfieldsetr   r>   )r8   r?   response_action_post:  s    &z%ModelMixinAction.response_action_postc             C   s    t |dd }|rt|r|S d S )Nr  )rs   r   )r7   r8   r   r   r>   r>   r?   get_action_formM  s    z ModelMixinAction.get_action_formc             C   s6   | j d kr| jd krg S | |}dd t|D S )Nc             S   s   g | ]\}}}|||d qS ))r&   r  r   r>   )r   r&   r  r   r>   r>   r?   r   X  s    z4ModelMixinAction.get_action_sets.<locals>.<listcomp>)r   r   get_action_item_and_setr  Z
itervalues)r7   r8   r   r>   r>   r?   r{   S  s
    
z ModelMixinAction.get_action_setsc                s  ddl m} ddlm  |jj| jj| jjd}g }t	
|d }|d krg }xp| j d d d d d D ]P}xJ|j|jfD ]:}||jkrq|t||g }|sq|x|D ]}	||	 qW q|W qjW |rt	|| x|D ]}
|| |
| qW t fdd|D S )	Nr   )const)ActionToolbar)appr*   rR   rD   c                s"   g | ]\}}}| |||fqS r>   r>   )r   r&   r  r   )r  r>   r?   r   u  s   z<ModelMixinAction.get_action_item_and_set.<locals>.<listcomp>)mysite.adminr  r  r  ZACTION_CACHE_KEYrx   r9   ri   rh   r
   rr   r   r   ZADMIN_ACTION_FILEDZADMIN_ACTION_SET_FIELD__dict__rs   rv   r   r  r   )r7   r8   r  Z_action_keyZ_action_setsZ_action_classesr%   Zaction_attrr  r  r   r>   )r  r?   r  [  s.    "



z(ModelMixinAction.get_action_item_and_setc       "         s  ddl m} ddlm} ddlm} ddlm} ddlm} ddlm} |j	
d|j
dd }|sjt | |}	|	s~t |}
trt|d	d
d	}tjrtd| | j\}}|jdj|||
|||dst S ||	krt |	| }|\}}}|jd|j	dd }||| |}t|dd }|r@| S | ||}d  |jdkr|rp||j	|j d} r   sd}|rdt|d| }d} rd! fdd j"# D }y| $|| |}W n` |k
r: } z@tjrdd l%}|&  |||||| j'dd t(t)|ddS d }~X Y nLX |||pH||dt)|* t)|pb|+dd| j'd t(dddS nt(| j,ddS n|r|   r|j- | .|| | d}nd }|/ d krdd!l0m1} || | j2j3| j2j4/ f}||
d"| j56d#| |
f |/ |d$}t7|d%rXt8|j9rX|9 }|:| | j2j3| j2j4/ |j;f}t|d&d } d'| d(g}!t<|| p|!|S ))Nr   )r  )log_operation)export_error_dict)helpers)r   )BUILD_IN_PERMISSIONr  zutf-8r  zaction_name=z{app}.{action}_{model})r  r   r*   r)   Zresponse_actionr   TFz%sr	  r   r9  c                s(   g | ] \}}d j  j|j|dqS )z{key}={val})r   val)rx   r   rr   label)r   r   r   )r   r>   r?   r     s   z0ModelMixinAction.action_view.<locals>.<listcomp>rD   )r*   Zaction_status)rF   rE   z	{0} = {1}|)r*   )r  Zadmin)Zbiophotoqrcode)create_qrcodez%s?action_name=%sz%s_%s_action)r   r  r   Zaction_reprrh   r   r  z%s/%s/actions/%s.htmlzlayui/action.html)=r  r  rj   r  r  r  r  r   r  r   rr   rn   r   Zget_actionsr  rO  r  r  r   DEBUGprintrl   rp   r  rx   r   Zgetlistr%   rs   r  r  ZFILESZis_validrT  Zcleaned_datar   r  r5   r6   r*   r   rm   
capitalizer  errorsZZKActionAdminFormr  r   Zmysite.base._qrcoder  r9   ri   rh   ry   rz   r   r   r   r   r   r   )"r7   r8   r  r  r  r  r   r  r  r   Z_action_nameri   rh   r   r   r&   r'   Zobj_idsr  Z_builtin_responser  Z
form_validZaction_verbose_nameZmessagesr   r   r5   Zadmin_action_formr  rl   r   Zaction_contextr  Ztemplate_listr>   )r   r?   action_viewy  s    



  

zModelMixinAction.action_viewN)r   r   r   r  r  r  r  r  r  r{   r  r5  r  r>   r>   r>   r?   r    s   Gr  )Qrl  r  r   r   rO  r:  r   ro  collectionsr   r   	functoolsr   Zdjangor   Zdjango.templater   Zdjango.contrib.admin.optionsr   r   Zdjango.contrib.admin.utilsr	   Zdjango.core.cacher
   r   r   Zdjango.httpr   r   r   r   Zdjango.template.responser   Zdjango.urlsr   Zdjango.utils.decoratorsr   Zdjango.utils.encodingr   Zdjango.utils.translationr   r   r
  r   Zdjango.views.decorators.csrfr   r   Zdjango.views.decorators.debugr   Zdjango.confr   r   r   Zmysite.tools.html_to_pdfr   Zmysite.admin.exceptionsr   Zmysite.admin.helpersr   Zmysite.admin.json_responser   Zmysite.utilsr    Zmysite.base.models.sys_settingr!   Zmysite.base.utilsr"   Zmysite.settingsr#   rm  rT  r  r5  Zsensitive_post_parameters_mr  r$   r   r(   r   r6  r  r>   r>   r>   r?   <module>   sd   
 |   L   a