
&W                 @   s0  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 l m Z Gd d   d e  Z e
 e  d d    Z e
 e  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   y t  j j d |  } Wn t d  SYn Xg  } x t  j j d d  D]t } t |  d k s | t |  d d | j j k r | j | j j g  g  | t |  d d j |  qJ Wt	 |  d d | d	 | j j d
 d d | d d i  S)Nidz/news/categorysr      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    6#r   c             C   s(   t  t d | } t |  d d | i  S)Nr   znews/announcement.htmlannouncement)r   r
   r   )r   r   Zthe_announcementr   r   r   r   #   s    r   )Imagec               @   sp   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   s]   | |  _  | |  _ t j |  j     } | j \ |  _ |  _ Wd  QRX|  j |  j |  _ d  S)N)	dirnamer    openpathsizewidthheightaspect)selfr"   r#   imr   r   r   __init__+   s
    		zPic.__init__c             C   s   |  j  S)N)r#   )r*   r   r   r   __str__2   s    zPic.__str__c             C   s   t  j j t j d  S)Nphotos)osr%   joinr	   
MEDIA_ROOT)r*   r   r   r   root5   s    zPic.rootc             C   s"   t  j j |  j   |  j |  j  S)N)r/   r%   r0   r2   r"   r#   )r*   r   r   r   r%   8   s    zPic.pathc             C   s   t  j j t j d  S)Nr.   )r/   r%   r0   r	   	MEDIA_URL)r*   r   r   r   baseurl;   s    zPic.baseurlc             C   s4   t  j j |  j   |  j |  j  } t j j |  S)N)	r/   r%   r0   r4   r"   r#   urllibparsequote)r*   tr   r   r   url>   s    $zPic.urlc             C   sA   t  j j |  j   |  j d |  j  } |  j   t j j	 |  S)Nthumb)
r/   r%   r0   r4   r"   r#   
make_thumbr5   r6   r7   )r*   r8   r   r   r   r:   B   s    '
z	Pic.thumbc             C   sq   t  j j |  j   |  j d |  j  } t  j j |  sm t j d d d d j	 |  j    d j	 |  g  d  S)Nr:   convertz-resizez12.5%z{0})
r/   r%   r0   r2   r"   r#   exists
subprocesscallformat)r*   r8   r   r   r   r;   G   s    'zPic.make_thumbN)__name__
__module____qualname__r,   r-   r2   r%   r4   r9   r:   r;   r   r   r   r   r!   *   s   r!   c             C   s  t  j j t j d  } t j |  } t  j j | |  } t  j j | d  } t  j j |  sp t  j |  g  } g  } x t  j	 |  D] } t  j j | |  }	 t  j j
 |	  rt  j j |  \ }
 } | j   d k r-| j t d | d |   q t  j j |	  r | d k r | j |  q W| j d d	 d
    | j   d g } xS | j d  D]B } | rj| j | t  j j | d d t j j |   f  qjWt |  d k r| j   d | d | d | d | i } 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.   r:   .jpg.jpeg.pngr"   r#   keyc             S   s   d j  d |  j |  j  S)Nz
{0:.2f}{1}g      ?)r@   r)   r#   )xr   r   r   <lambda>q   s    zdirectory.<locals>.<lambda>home/photos/r   pixdirsr%   targetznews/picdic.html)rD   rE   rF   )zhomerK   )r/   r%   r0   r	   r1   htmlunescaper=   mkdirlistdirisfilesplitextlowerr   r!   isdirsortsplitr5   r6   r7   r   popr   )r   r   baserO   destinationZthumbspicsrN   fpbextr%   dparamsr   r   r   	directoryM   s4    
	:
re   c             C   s   t  |  d  S)N )re   )r   r   r   r   basedirectory|   s    rg   )django.shortcutsr   r   django.httpr   r   r   django.templater   django.contrib.auth.decoratorsr   django.confr	   Znews.modelsr
   r/   os.pathpeople.funcsr   rQ   r5   r>   r   r   PILr    objectr!   re   rg   r   r   r   r   <module>   s"   #/