o
    og
                     @   sl   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 Z
G dd	 d	ejZG d
d dejZdS )    )models)timezone)reverse)ValidationError)filesizeformatc                 C   s2   d}| j |krtdt| dt| j  dd S )Ni   zFile size cannot exceed z. Your file size is .)sizer   r   )valuemax_size r   (/var/www/html/gwz/publications/models.pyvalidate_file_size   s   
 r   c                   @   sf   e Zd ZejddZejddZejddZ	ej
ddZej
ddZG dd	 d	Zd
d Zdd ZdS )Categoryd   
max_lengthTunique)blankauto_now_addauto_nowc                   @   s   e Zd ZdZdgZdS )zCategory.Meta
CategoriesnameN)__name__
__module____qualname__verbose_name_pluralorderingr   r   r   r   Meta   s    
r    c                 C      | j S N)r   selfr   r   r   __str__      zCategory.__str__c                 C      t d| jgdS )Nzpublications:category_detailargsr   slugr#   r   r   r   get_absolute_url      zCategory.get_absolute_urlN)r   r   r   r   	CharFieldr   	SlugFieldr+   	TextFielddescriptionDateTimeField
created_at
updated_atr    r%   r,   r   r   r   r   r      s    r   c                       s   e Zd ZdZejddZejddZe	 Z
ejeejddZejdegd	d
ZejdddZejddZejejdZejddZejddZejdedddZG dd dZdd Zdd Z fddZ   Z!S )Publication))activeActive)archivedArchived   r   Tr   publications)	on_deleterelated_namezpublications/documents/%Y/%m/z8Maximum file size: 50MB. Allowed formats: PDF, DOC, DOCX)	upload_to
validators	help_text   )r   r   r   )defaultr   r   
   r6   z(Set to archived to hide from public view)r   choicesrB   r@   c                   @   s2   e Zd ZdgZejddgdejddgdgZdS )zPublication.Metaz-published_datestatuspublished_date)fieldscategoryN)r   r   r   r   r   Indexindexesr   r   r   r   r    9   s
    r    c                 C   r!   r"   )titler#   r   r   r   r%   @   r&   zPublication.__str__c                 C   r'   )Nzpublications:publication_detailr(   r*   r#   r   r   r   r,   C   r-   zPublication.get_absolute_urlc                    sT   | j r| j j}dD ]}|dk r|dd| | _ n|d }q	t j|i | d S )N)BKBMBGBi   z.2f )documentr   	file_sizesupersave)r$   r)   kwargs
size_bytesunit	__class__r   r   rT   F   s   
zPublication.save)"r   r   r   STATUS_CHOICESr   r.   rK   r/   r+   r0   r1   
ForeignKeyr   CASCADErH   	FileFieldr   rQ   rR   PositiveIntegerFielddownload_count	DateFieldr   nowrF   r2   r3   r4   rE   r    r%   r,   rT   __classcell__r   r   rX   r   r5      s4    r5   N)	django.dbr   django.utilsr   django.urlsr   django.core.exceptionsr   django.template.defaultfiltersr   r   Modelr   r5   r   r   r   r   <module>   s    