a
    QhS                     @   s   d dl mZ ddlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlZG dd	 d	e
ZG d
d de
ZeeG dd dejZeeG dd dejZeeG dd dejZdS )    )admin   )EstufaSelecaoLogFinalizacaoEstufa)gettext_lazy)SimpleListFilter)HttpResponseNc                   @   s(   e Zd ZedZdZdd Zdd ZdS )AnoEstufaFiltroAnoanoc                 C   s,   t dd |jj D }dd t|D S )Nc                 s   s   | ]}|j r|j jV  qd S N)data_estufayear.0obj r   )/var/www/sistema_ama/apps/estufa/admin.py	<genexpr>       z*AnoEstufaFiltro.lookups.<locals>.<genexpr>c                 S   s   g | ]}|t |fqS r   strr   r   r   r   r   
<listcomp>   r   z+AnoEstufaFiltro.lookups.<locals>.<listcomp>setmodelobjectsallsortedselfrequestmodel_adminZanosr   r   r   lookups   s    zAnoEstufaFiltro.lookupsc                 C   s   |   r|j|   dS |S )N)Zdata_estufa__yearvaluefilterr"   r#   querysetr   r   r   r*      s    zAnoEstufaFiltro.querysetN__name__
__module____qualname___titleparameter_namer%   r*   r   r   r   r   r
   
   s   r
   c                   @   s(   e Zd ZedZdZdd Zdd ZdS )AnoSelecaoFiltror   r   c                 C   s,   t dd |jj D }dd t|D S )Nc                 s   s   | ]}|j r|j jV  qd S r   )data_selecaor   r   r   r   r   r      r   z+AnoSelecaoFiltro.lookups.<locals>.<genexpr>c                 S   s   g | ]}|t |fqS r   r   r   r   r   r   r      r   z,AnoSelecaoFiltro.lookups.<locals>.<listcomp>r   r!   r   r   r   r%      s    zAnoSelecaoFiltro.lookupsc                 C   s   |   r|j|   dS |S )N)Zdata_selecao__yearr&   r)   r   r   r   r*   !   s    zAnoSelecaoFiltro.querysetNr+   r   r   r   r   r2      s   r2   c                   @   sv   e Zd ZdZdedfZdZddgZdZdZ	dd	d
ifdd	diffZ
ddgZejdddd Zejdddd ZdS )EstufaAdmin)	semeaduraget_numero_loter   quantidade_mortalidade
finalizadoregistrado_porr8   r9   semeadura__numero_mudaZsemeadura__lote__numero_loter5   r   r6      Informações da Semeadurafieldsr5   r6   zDados de Mortalidade)r   r7   r8   r9   marcar_como_finalizadoexportar_csvzMarcar como finalizadodescriptionc                 C   s"   |j dd}| || d d S )NT)r8   z% registros marcados como finalizados.updatemessage_userr"   r#   r*   updatedr   r   r   r@   >   s    z"EstufaAdmin.marcar_como_finalizadoExportar para CSVc                    s^   t dd}d|d< t|}|dd tjjD  |D ]  | fddtjjD  q8|S )Ntext/csvcontent_typez attachment; filename=estufas.csvContent-Dispositionc                 S   s   g | ]
}|j qS r   namer   fieldr   r   r   r   H   r   z,EstufaAdmin.exportar_csv.<locals>.<listcomp>c                    s   g | ]}t  |jqS r   getattrrO   rP   r   r   r   r   J   r   )r	   csvwriterwriterowr   _metar>   r"   r#   r*   responserV   r   rT   r   rA   C   s    

zEstufaAdmin.exportar_csvN)r,   r-   r.   list_displayr
   list_filtersearch_fieldsautocomplete_fieldsdate_hierarchyreadonly_fields	fieldsetsactionsr   actionr@   rA   r   r   r   r   r4   '   s$   
	


r4   c                   @   sv   e Zd ZdZdedfZdZddgZdZdZ	dd	d
ifdd	diffZ
ddgZejdddd Zejdddd ZdS )SelecaoAdmin)r5   r6   r3   quantidade_selecaovendar9   rf   r9   r:   r5   r3   r<   r=   r>   r?   u   Dados de Seleção)r3   re   rf   r9   r@   rA   zMarcar como vendarB   c                 C   s"   |j dd}| || d d S )NT)rf   u     seleções marcadas como venda.rD   rG   r   r   r   r@   e   s    z#SelecaoAdmin.marcar_como_finalizadorI   c                    s^   t dd}d|d< t|}|dd tjjD  |D ]  | fddtjjD  q8|S )NrJ   rK   z!attachment; filename=selecoes.csvrM   c                 S   s   g | ]
}|j qS r   rN   rP   r   r   r   r   o   r   z-SelecaoAdmin.exportar_csv.<locals>.<listcomp>c                    s   g | ]}t  |jqS r   rR   rP   rT   r   r   r   q   r   )r	   rU   rV   rW   r   rX   r>   rY   r   rT   r   rA   j   s    

zSelecaoAdmin.exportar_csvN)r,   r-   r.   r[   r2   r\   r]   r^   r_   r`   ra   rb   r   rc   r@   rA   r   r   r   r   rd   N   s$   
	


rd   c                   @   sF   e Zd ZdZdZdZdZdd Zde_dd	 Z	dddZ
dddZd
S )LogFinalizacaoEstufaAdmin)r5   numero_lote	data_lotedata_exclusaohora_exclusaousuario_exclusaonome_usuario_exclusaomotivo_formatado)rj   motivo_exclusao)rh   rm   ro   )	r5   rh   ri   rj   rk   rl   rm   ro   motivo_exclusao_outrosc                 C   s(   |j dkr"|jr"|j  d|j S |j S )NOutrosz - )ro   rp   )r"   r   r   r   r   rn      s    z*LogFinalizacaoEstufaAdmin.motivo_formatadoMotivoc                 C   s   dS NFr   )r"   r#   r   r   r   has_add_permission   s    z,LogFinalizacaoEstufaAdmin.has_add_permissionNc                 C   s   dS rs   r   r"   r#   r   r   r   r   has_change_permission   s    z/LogFinalizacaoEstufaAdmin.has_change_permissionc                 C   s   dS rs   r   ru   r   r   r   has_delete_permission   s    z/LogFinalizacaoEstufaAdmin.has_delete_permission)N)N)r,   r-   r.   r[   r\   r]   r`   rn   short_descriptionrt   rv   rw   r   r   r   r   rg   u   s   
rg   )Zdjango.contribr   modelsr   r   r   django.utils.translationr   r/   django.contrib.adminr   django.httpr	   rU   r
   r2   register
ModelAdminr4   rd   rg   r   r   r   r   <module>   s   &&