o
    gd                     @   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	lmZ d
d 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d Z$dS )    )renderget_object_or_404)HttpResponseRedirectHttpResponseHttpResponseBadRequest)RequestContext)user_passes_test)settings)NewsItemN)is_fac_or_admin)Redirectc                 C   s8   t | d}|r
| S tjjddd}t| dd|iS )Nznews/scategoryz-dateznews/index.htmllatest_news_list)r   r
   objectsfilterorder_byr   )requestrqs r   /var/www/djphys/news/views.pyall_news   s
   
r   c              	   C   s   z	t jj|d}W n	   td Y S g }t jjddD ],}t|dks2|t|d  d |jjkr;||jjg g |t|d  d | qt	| d||jjd|dd	S )
Nid/news/r   r   r      znews/detail.html   theStorytheYearmenu_base_urlstoriesmaxwords)
r
   r   getr   r   lendateyearappendr   )r   pkr    r#   r   r   r   r   detail   s   (r+   c              	   C   p   z	t jj|d}W n   t jjdd}| dkr td Y S |d }Y 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.      
r1   c              	   C   r,   )	Nr   r-   r   r   r   znews/statement.htmlr   r   r.   r0   r   r   r   	statement@   r2   r3   c                 C   s   t t|d}t| dd|iS )N)r*   znews/announcement.htmlannouncement)r   r
   r   )r   r*   the_announcementr   r   r   r4   R   s   r4   )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   sZ   || _ || _t|  }|j\| _| _W d    n1 sw   Y  | j| j | _d S N)	dirnamer6   openpathsizewidthheightaspect)selfr9   r:   imr   r   r   __init__[   s   zPic.__init__c                 C   s   | j S r8   )r:   rA   r   r   r   __str__b   s   zPic.__str__c                 C      t jtjdS Nphotos)osr<   joinr	   
MEDIA_ROOTrD   r   r   r   roote      zPic.rootc                 C   s   t j|  | j| jS r8   )rI   r<   rJ   rL   r9   r:   rD   r   r   r   r<   h   s   zPic.pathc                 C   rF   rG   )rI   r<   rJ   r	   	MEDIA_URLrD   r   r   r   baseurlk   rM   zPic.baseurlc                 C   s$   t j|  | j| j}tj|S r8   )	rI   r<   rJ   rO   r9   r:   urllibparsequoterA   tr   r   r   urln   s   zPic.urlc                 C   s.   t j|  | jd| j}|   tj	|S )Nthumb)
rI   r<   rJ   rO   r9   r:   
make_thumbrP   rQ   rR   rS   r   r   r   rV   r   s   z	Pic.thumbc              	   C   sR   t j|  | jd| j}t j|s'tdddd	|  d	|g d S d S )NrV   convertz-resizez12.5%z{0})
rI   r<   rJ   rL   r9   r:   exists
subprocesscallformatrS   r   r   r   rW   w   s   

zPic.make_thumbN)__name__
__module____qualname__rC   rE   rL   r<   rO   rU   rV   rW   r   r   r   r   r7   Z   s    r7   c              
   C   sV  t jtjd}t|}t j||}t j|d}t j|s&t | g }g }	 t |D ]6}t j||}t j|rWt j|\}	}
|
 dv rV|t||d q0t j|rf|dkrf|| q0|jdd d |  d	g}|d
D ]}|r||t j|d d tj|f q{t|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.
    rH   rV   F)z.jpgz.jpegz.png)r9   r:   c                 S   s   d d| j | jS )Nz
{0:.2f}{1}g      ?)r\   r@   r:   )xr   r   r   <lambda>   s    zdirectory.<locals>.<lambda>)key)homez/photos/r   )pixdirsr<   targetznews/picdic.html)rI   r<   rJ   r	   rK   htmlunescaperY   mkdirscandiris_filesplitextr:   lowerr)   r7   is_dirlistdirisfileisdirsortsplitrP   rQ   rR   r&   popr   )r   r*   baserh   destinationthumbspicsrg   fbextpr<   dparamsr   r   r   	directory~   s@   



 r   c                 C   s
   t | dS )N )r   r   r   r   r   basedirectory   s   
r   c              
   C   sx   zt jjdddd}t| W n ty' } z
t| W Y d}~nd}~ww | dkr2t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.
    S	community)r   keywords__containsz
-post_dateNr   zI couldn't find the affirmation)	r
   r   r   r   print	Exceptionr/   r3   r   )r   
statementseepsrecentr   r   r   affirmation_of_values   s   r   c                 C   s   t   t| dS )Nr   )r
   exportr+   r   r   r   r   dump   s   
r   )%django.shortcutsr   r   django.httpr   r   r   django.templater   django.contrib.auth.decoratorsr   django.confr	   news.modelsr
   rI   os.pathpeople.funcsr   ri   rP   rZ   djphys.redirectr   r   r+   r1   r3   r4   PILr6   objectr7   r   r   r   r   r   r   r   r   <module>   s4   $
0
