U
    7`Q                     @   s   d dl mZ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mZ d dlZd dlZd dlmZ d dlZd dlZd dlZd	d
 Zdd Zdd Zdd Zd dlmZ G dd deZe
edd Ze
edd Zdd Z dS )    )renderget_object_or_404)HttpResponseRedirectHttpResponseHttpResponseBadRequest)RequestContext)user_passes_test)settings)NewsItemN)is_fac_or_adminc              	   C   s   zt jj|d}W n   td Y S X g }t jjddD ]X}t|dksf|t|d  d |jjkrx||jjg g |t|d  d | q:t	| d||jjd|dd	S )
Nid/news/scategoryr      znews/detail.html   theStoryZtheYearZmenu_base_urlstoriesZmaxwords)
r
   objectsgetr   filterlendateyearappendr   )requestpkr   r   r    r    /var/www/djphys/news/views.pydetail   s    (r"   c              	   C   st   zt jj|d}W n:   t jjdd}| dkr@td Y S |d }Y nX t| d||jjdt jjddddS )	Nr   Pr   r   r   znews/profile.htmlr   r   	r
   r   r   r   countr   r   r   r   r   r   r   r    r    r!   profile%   s    r'   c              	   C   st   zt jj|d}W n:   t jjdd}| dkr@td Y S |d }Y nX t| d||jjdt jjddddS )	Nr   r#   r   r   r   znews/statement.htmlr   r   r$   r&   r    r    r!   	statement7   s    r(   c                 C   s   t t|d}t| dd|iS )N)r   znews/announcement.htmlannouncement)r   r
   r   )r   r   Zthe_announcementr    r    r!   r)   I   s    r)   )Imagec                   @   sL   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
dS )Picc              	   C   sF   || _ || _t|  }|j\| _| _W 5 Q R X | j| j | _d S N)	dirnamer*   openpathsizewidthheightaspect)selfr-   r.   imr    r    r!   __init__R   s
    zPic.__init__c                 C   s   | j S r,   )r.   r5   r    r    r!   __str__Y   s    zPic.__str__c                 C   s   t jtjdS Nphotos)osr0   joinr	   
MEDIA_ROOTr8   r    r    r!   root\   s    zPic.rootc                 C   s   t j|  | j| jS r,   )r<   r0   r=   r?   r-   r.   r8   r    r    r!   r0   _   s    zPic.pathc                 C   s   t jtjdS r:   )r<   r0   r=   r	   	MEDIA_URLr8   r    r    r!   baseurlb   s    zPic.baseurlc                 C   s$   t j|  | j| j}tj|S r,   )	r<   r0   r=   rA   r-   r.   urllibparsequoter5   tr    r    r!   urle   s    zPic.urlc                 C   s.   t j|  | jd| j}|   tj	|S )Nthumb)
r<   r0   r=   rA   r-   r.   
make_thumbrB   rC   rD   rE   r    r    r!   rH   i   s    z	Pic.thumbc              	   C   sN   t j|  | jd| j}t j|sJtdddd	|  d	|g d S )NrH   convertz-resizez12.5%z{0})
r<   r0   r=   r?   r-   r.   exists
subprocesscallformatrE   r    r    r!   rI   n   s    
 zPic.make_thumbN)__name__
__module____qualname__r7   r9   r?   r0   rA   rG   rH   rI   r    r    r    r!   r+   Q   s   r+   c              
   C   sV  t jtjd}t|}t j||}t j|d}t j|sLt | g }g }t 
|D ]l}t j||}	t j|	rt j|\}
}| dkr|t	||d q^t j|	r^|dkr^|| q^|jdd d |  dg}|d	D ]0}|r||t j|d
 d tj|f qt|dkr<|  ||||d}t| d|S )z
    Given a valid logged in user and a suitable path to a folder, 
    of images, produce a page of thumbnails with links to the
    photographs. We will look to the settings module for the
    location of these files.
    r;   rH   )z.jpgz.jpegz.png)r-   r.   c                 S   s   d d| j | jS )Nz
{0:.2f}{1}g      ?)rN   r4   r.   )xr    r    r!   <lambda>       zdirectory.<locals>.<lambda>)key)homez/photos/r   )pixdirsr0   targetznews/picdic.html)r<   r0   r=   r	   r>   htmlunescaperK   mkdirr+   listdirisfilesplitextlowerr   isdirsortsplitrB   rC   rD   r   popr   )r   r   baser[   destinationZthumbspicsrZ   fpbextr0   dparamsr    r    r!   	directoryu   s8    

 rp   c                 C   s
   t | dS )N )rp   )r   r    r    r!   basedirectory   s    rr   c              
   C   sv   z"t jjdddd}t| W n* tk
rL } zt| W 5 d}~X Y nX | dkrbtd|d }t| |jS )z
    5/12/21 Added a page with the affirmation of our values. For the
    moment, it goes to the announcement with all the context. Eventually,
    I should update to just the short and sweet.
    SZ	community)r   keywords__containsz
-post_dateNr   zI couldn't find the affirmation)	r
   r   r   order_byprint	Exceptionr%   r(   r   )r   
statementseepsrecentr    r    r!   affirmation_of_values   s     r{   )!django.shortcutsr   r   django.httpr   r   r   django.templater   django.contrib.auth.decoratorsr   django.confr	   news.modelsr
   r<   os.pathpeople.funcsr   r\   rB   rL   r"   r'   r(   r)   PILr*   objectr+   rp   rr   r{   r    r    r    r!   <module>   s,   $
0
