U
    {h2                     @   s  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	 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 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! d dl"m#Z# d dl$m%Z% G dd dZ&dd Z'G dd deZ(G dd dZ)dS )    N)OrderedDict)partial)BytesIO)label_for_field)FieldDoesNotExist)FileResponseStreamingHttpResponse)timezone)	Formatter)	force_str)
get_format)cached_property)capfirst)gettext)Workbook)WriteOnlyCell)Buttonmultigetattrc                   @   s   e Zd ZdZdd ZdS )EchozKAn object that implements just the write method of the file-like interface.c                 C   s
   | dS )z@Write the value by returning it, instead of storing in a buffer.zUTF-8)encode)selfvalue r   b/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/views/mixins.pywrite   s    z
Echo.writeN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c                 C   s   t d| S )Nz, )r   joinr   r   r   r   list_to_str    s    r"   c                (   @   st   e Zd ZdZddddddddddd	d	d
d
dddddddddddddddddddddddddd'Zdd Zdd ZdS )ExcelDateFormatterNdddZdddZdddd mmmZmmmZmmmmzmmm.yyZyyyyhZhHhhssz.00zAM/PMzh:mmz
h:mm AM/PMzyyyy-mm-ddThh:mm:ss.00zddd, d mmm yyyy hh:mm:ss)'r%   jDlSwzWr(   nMbEFNtyYLogGr*   HisuaAfPeIOTZcrUc                 C   s   t d}| |S )NZSHORT_DATETIME_FORMAT)r   format)r   rQ   r   r   r   getz   s    zExcelDateFormatter.getc                    s8    j kr fddS tdtj d  dd S )Nc                      s
   j   S N)_formatsr   namer   r   r   <lambda>       z0ExcelDateFormatter.__getattr__.<locals>.<lambda>'z' object has no attribute ')rT   AttributeErrortyper   )r   rV   r   rU   r   __getattr__~   s
    
zExcelDateFormatter.__getattr__)r   r   r   datarT   rR   r\   r   r   r   r   r#   $   sV   Qr#   c                	       s  e Zd ZdZdZdZeefZg Zi Ze	j	edd ie	j
e	jfedieeeeeiiZi ZdZdZ fd	d
Z fddZdd Zdd Zdd Zdd Zd/d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"e!d)d* Z#e$d+d, Z%e$ fd-d.Z&  Z'S )0SpreadsheetExportMixinzUA mixin for views, providing spreadsheet export functionality in csv and xlsx formatsxlsxcsvc                 C   s   t | r| S t | tj jS rS   )r	   Zis_naiveZ
make_naivedatetimeutcr!   r   r   r   rW      s    zSpreadsheetExportMixin.<lambda>Nz'wagtailadmin/shared/export_buttons.htmlzspreadsheet-exportc                    s,   t  j|f|| |jd| jk| _d S )Nexport)supersetupGETrR   FORMATS	is_export)r   requestargskwargs	__class__r   r   re      s    zSpreadsheetExportMixin.setupc                    s   | j r
d S t |S rS   )rh   rd   get_paginate_by)r   querysetrl   r   r   rn      s    z&SpreadsheetExportMixin.get_paginate_byc                 C   s   | j S )zGGets the base filename for the exported spreadsheet, without extensions)export_filenamer   r   r   r   get_filename   s    z#SpreadsheetExportMixin.get_filenamec                    s   t  fdd| jD }|S )zmReturns an OrderedDict (in the order given by list_export) of the exportable information for a model instancec                 3   s   | ]}|t  |fV  qd S rS   r   .0fielditemr   r   	<genexpr>   s    z5SpreadsheetExportMixin.to_row_dict.<locals>.<genexpr>)r   list_export)r   rw   row_dictr   rv   r   to_row_dict   s    z"SpreadsheetExportMixin.to_row_dictc                 C   s\   | j |i }||kr|| S | j D ]&\}}t||r(||kr(||   S q(ttddS )zYReturns the preprocessing function for a given field name, field value, and export formatT)Zstrings_only)custom_field_preprocessrR   custom_value_preprocessitems
isinstancer   r   )r   ru   r   export_formatZformat_dictZvalue_classesr   r   r   get_preprocess_function   s    z.SpreadsheetExportMixin.get_preprocess_functionc                 C   s&   |  |||}|dk	r||S |S dS )z?Preprocesses a field value before writing it to the spreadsheetN)r   )r   ru   r   r   Zpreprocess_functionr   r   r   preprocess_field_value   s    z-SpreadsheetExportMixin.preprocess_field_valuec                 c   sH   |  D ]:\}}t|| ||| j}|r<t|tjr<||_|V  qdS )z)Generate cells to append to the worksheetN)r~   r   r   FORMAT_XLSXr   ra   Znumber_format)r   	worksheetrz   date_formatru   r   cellr   r   r   generate_xlsx_row   s     z(SpreadsheetExportMixin.generate_xlsx_rowc                    s   |  fdd| D S )Nc                    s"   i | ]\}}|  || jqS r   )r   
FORMAT_CSV)rt   ru   r   rq   r   r   
<dictcomp>   s    z8SpreadsheetExportMixin.write_csv_row.<locals>.<dictcomp>)writerowr~   )r   writerrz   r   rq   r   write_csv_row   s
    
z$SpreadsheetExportMixin.write_csv_rowc              	   C   sT   | j |}|rt|S zttt||jW S  ttfk
rN   t| Y S X dS )zQGet the heading label for a given field for a spreadsheet generated from querysetN)export_headingsrR   r   r   r   modelrZ   r   )r   ro   ru   Zheading_overrider   r   r   get_heading   s    z"SpreadsheetExportMixin.get_headingc                 #   sR   t jt jd}| fddjD V   D ]}||V  q4dS )zUGenerate a csv file line by line from queryset, to be used in a StreamingHTTPResponse)
fieldnamesc                    s   i | ]}|  |qS r   r   rs   ro   r   r   r   r      s      z5SpreadsheetExportMixin.stream_csv.<locals>.<dictcomp>N)r`   
DictWriterr   ry   r   r   r{   )r   ro   r   rw   r   r   r   
stream_csv   s    z!SpreadsheetExportMixin.stream_csvc                    sr   t ddd}|jdd}| fddjD  t  } D ] }|j|||d qB|| dS )	z&Write an xlsx workbook from a querysetT)Z
write_onlyZ	iso_datesZSheet1)titlec                 3   s   | ]}  |V  qd S rS   r   rs   r   r   r   rx      s    z4SpreadsheetExportMixin.write_xlsx.<locals>.<genexpr>)r   N)	r   Zcreate_sheetappendry   r#   rR   r   r{   save)r   ro   outputZworkbookr   r   rw   r   r   r   
write_xlsx   s    
  z!SpreadsheetExportMixin.write_xlsxc                 C   s6   t  }| || |d t|dd|   ddS )z<Write an xlsx file from a queryset and return a FileResponser   TzAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetz.xlsx)Zas_attachmentcontent_typefilename)r   r   seekr   rr   )r   ro   r   r   r   r   write_xlsx_response
  s    
z*SpreadsheetExportMixin.write_xlsx_responsec                 C   s,   |  |}t|dd}d|  |d< |S )Nztext/csv)r   zattachment; filename="{}.csv"zContent-Disposition)r   r   rQ   rr   )r   ro   streamresponser   r   r   write_csv_response  s    
z)SpreadsheetExportMixin.write_csv_responsec                 C   s,   || j kr| |S || jkr(| |S dS )zjReturn a response with a spreadsheet representing the exported data from queryset, in the format specifiedN)r   r   r   r   )r   ro   Zspreadsheet_formatr   r   r   as_spreadsheet   s    


z%SpreadsheetExportMixin.as_spreadsheetc                 C   s(   | j j }||d< | j jd |  S )Nrc   ?)ri   rf   copypath	urlencode)r   rQ   paramsr   r   r   get_export_url'  s    z%SpreadsheetExportMixin.get_export_urlc                 C   s
   |  dS )Nr_   r   rq   r   r   r   xlsx_export_url,  s    z&SpreadsheetExportMixin.xlsx_export_urlc                 C   s
   |  dS )Nr`   r   rq   r   r   r   csv_export_url0  s    z%SpreadsheetExportMixin.csv_export_urlc                 C   s
   t | jS rS   )boolry   rq   r   r   r   show_export_buttons4  s    z*SpreadsheetExportMixin.show_export_buttonsc                    sN   t  j }| jrJ|ttd| jddd |ttd| jddd |S )NzDownload XLSXdownloadZ   )urlZ	icon_namepriorityzDownload CSVd   )	rd   header_more_buttonsr   r   r   r   _r   r   )r   Zbuttonsrl   r   r   r   8  s&    	z*SpreadsheetExportMixin.header_more_buttons)N)(r   r   r   r   r   r   rg   ry   r|   ra   datetimelistr"   r}   r   Zexport_buttons_template_namerp   re   rn   rr   r{   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   __classcell__r   r   rl   r   r^      sR    
  




	


r^   )*r`   ra   collectionsr   	functoolsr   ior   Zdjango.contrib.admin.utilsr   Zdjango.core.exceptionsr   Zdjango.httpr   r   Zdjango.utilsr	   Zdjango.utils.dateformatr
   Zdjango.utils.encodingr   Zdjango.utils.formatsr   Zdjango.utils.functionalr   Zdjango.utils.textr   Zdjango.utils.translationr   r   Zopenpyxlr   Zopenpyxl.cellr   Zwagtail.admin.widgets.buttonr   Zwagtail.coreutilsr   r   r"   r#   r^   r   r   r   r   <module>   s,   b