o
    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! 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)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   Z/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/views/mixins.pywrite      
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                   @   s  e Zd ZdZi 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i 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/d0d1d2d
d3d
d4d
d
d
d5d6d
d7Zd8d9 Zd:d; ZdS )<ExcelDateFormatterNdddjDdddlddddS wzWmmmnMmmmbEmmmmFNzmmm.tyyyYyyyyLoghGhHhhHisssuz.00azAM/PMAfzh:mmPz
h:mm AM/PMeIOzyyyy-mm-ddThh:mm:ss.00zddd, d mmm yyyy hh:mm:ss)TZcrUc                 C   s   t d}| |S )NSHORT_DATETIME_FORMAT)r   format)r   rW   r   r   r   getx   s   
zExcelDateFormatter.getc                    s4    j v r fddS tdtj d  d)Nc                      s
   j   S N)_formatsr   namer   r   r   <lambda>~   s   
 z0ExcelDateFormatter.__getattr__.<locals>.<lambda>'z' object has no attribute ')rZ   AttributeErrortyper   )r   r\   r   r[   r   __getattr__|   s
   
zExcelDateFormatter.__getattr__)r   r   r   datarZ   rX   ra   r   r   r   r   r"   "   s    
 "$&(*,.02468:<>@BDF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 rY   )r	   is_naive
make_naivedatetimeutcr    r   r   r   r]      s   zSpreadsheetExportMixin.<lambda>Nz'wagtailadmin/shared/export_buttons.htmlzspreadsheet-exportc                    s2   t  j|g|R i | |jd| jv | _d S )Nexport)supersetupGETrX   FORMATS	is_export)r   requestargskwargs	__class__r   r   rl      s   zSpreadsheetExportMixin.setupc                    s   | j rd S t |S rY   )ro   rk   get_paginate_by)r   querysetrs   r   r   ru      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 rY   r   .0fielditemr   r   	<genexpr>   s    
z5SpreadsheetExportMixin.to_row_dict.<locals>.<genexpr>)r   list_export)r   r~   row_dictr   r}   r   to_row_dict   s   z"SpreadsheetExportMixin.to_row_dictc                 C   s\   | j |i }||v r|| S | j D ]\}}t||r'||v r'||   S qttddS )zYReturns the preprocessing function for a given field name, field value, and export formatT)strings_only)custom_field_preprocessrX   custom_value_preprocessitems
isinstancer   r   )r   r|   r   export_formatformat_dictvalue_classesr   r   r   get_preprocess_function   s   z.SpreadsheetExportMixin.get_preprocess_functionc                 C   s"   |  |||}|dur||S |S )z?Preprocesses a field value before writing it to the spreadsheetN)r   )r   r|   r   r   preprocess_functionr   r   r   preprocess_field_value   s   z-SpreadsheetExportMixin.preprocess_field_valuec                 c   sV    ddl m} | D ]\}}||| ||| j}|r%t|tjr%||_|V  qdS )z)Generate cells to append to the worksheetr   )WriteOnlyCellN)openpyxl.cellr   r   r   FORMAT_XLSXr   rh   number_format)r   	worksheetr   date_formatr   r|   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)r{   r|   r   rx   r   r   
<dictcomp>   s    z8SpreadsheetExportMixin.write_csv_row.<locals>.<dictcomp>)writerowr   )r   writerr   r   rx   r   write_csv_row   s
   
z$SpreadsheetExportMixin.write_csv_rowc              	   C   sN   | j |}|rt|S zttt||jW S  ttfy&   t| Y S w )zQGet the heading label for a given field for a spreadsheet generated from queryset)export_headingsrX   r   r   r   modelr_   r   )r   rv   r|   heading_overrider   r   r   get_heading   s   z"SpreadsheetExportMixin.get_headingc                 #   sT    t jt jd}| fddjD V   D ]}||V  qdS )zUGenerate a csv file line by line from queryset, to be used in a StreamingHTTPResponse)
fieldnamesc                    s   i | ]	}|  |qS r   r   rz   rv   r   r   r   r      s    z5SpreadsheetExportMixin.stream_csv.<locals>.<dictcomp>N)re   
DictWriterr   r   r   r   r   )r   rv   r   r~   r   r   r   
stream_csv   s   z!SpreadsheetExportMixin.stream_csvc                    s~   ddl m} |ddd}|jdd}| fddjD  t  } D ]}|j|||d	 q'|	| d
S )z&Write an xlsx workbook from a querysetr   )WorkbookT)
write_only	iso_datesSheet1)titlec                 3   s    | ]	}  |V  qd S rY   r   rz   r   r   r   r      s    
z4SpreadsheetExportMixin.write_xlsx.<locals>.<genexpr>)r   N)
openpyxlr   create_sheetappendr   r"   rX   r   r   save)r   rv   outputr   workbookr   r   r~   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)as_attachmentcontent_typefilename)r   r   seekr   ry   )r   rv   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   rW   ry   )r   rv   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   rv   spreadsheet_formatr   r   r   as_spreadsheet"  s
   



z%SpreadsheetExportMixin.as_spreadsheetc                 C   s(   | j j }||d< | j jd |  S )Nrj   ?)rp   rm   copypath	urlencode)r   rW   paramsr   r   r   get_export_url)  s   z%SpreadsheetExportMixin.get_export_urlc                 C   
   |  dS )Nrd   r   rx   r   r   r   xlsx_export_url.  r   z&SpreadsheetExportMixin.xlsx_export_urlc                 C   r   )Nre   r   rx   r   r   r   csv_export_url2  r   z%SpreadsheetExportMixin.csv_export_urlc                 C   s
   t | jS rY   )boolr   rx   r   r   r   show_export_buttons6  r   z*SpreadsheetExportMixin.show_export_buttonsc                    sN   t  j }| jr%|ttd| jddd |ttd| jddd |S )NzDownload XLSXdownloadZ   )url	icon_namepriorityzDownload CSVd   )	rk   header_more_buttonsr   r   r   r   _r   r   )r   buttonsrs   r   r   r   :  s&   	z*SpreadsheetExportMixin.header_more_buttonsrY   )(r   r   r   r   r   r   rn   r   r   rh   datetimelistr!   r   r   export_buttons_template_namerw   rl   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   __classcell__r   r   rs   r   rc      sN    


	


rc   )&re   rh   collectionsr   	functoolsr   ior   django.contrib.admin.utilsr   django.core.exceptionsr   django.httpr   r   django.utilsr	   django.utils.dateformatr
   django.utils.encodingr   django.utils.formatsr   django.utils.functionalr   django.utils.textr   django.utils.translationr   r   wagtail.admin.widgets.buttonr   wagtail.coreutilsr   r   r!   r"   rc   r   r   r   r   <module>   s*    b