U
    ߇^                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZm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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 )'    )renderredirect)RequestContext)timezone)Download)StreamingHttpResponseHttpResponseHttp404c                 C   s
   t | dS )Nzigor/short.htmlr   request r   /var/www/djphys/igor/views.pyshort_intro	       r   c                 C   s
   t | dS )Nzigor/long.htmlr
   r   r   r   r   
long_intro
   r   r   c                 C   s
   t | dS )Nzigor/screencasts.htmlr
   r   r   r   r   videos   r   r   c                 C   s
   t | dS )Nzigor/faq.htmlr
   r   r   r   r   faq   r   r   c                 C   s
   t | dS )Nzigor/fitting.htmlr
   r   r   r   r   fitting   r   r   c                 C   s
   t | dS )Nzigor/power.htmlr
   r   r   r   r   	power_law   r   r   c                 C   s
   t | dS )Nzigor/propagation.htmlr
   r   r   r   r   propagation   r   r   c                 C   s
   t | dS )Nzigor/statistics.htmlr
   r   r   r   r   
statistics   r   r   c                 C   s
   t | dS )Nzigor/spreadsheets.htmlr
   r   r   r   r   spreadsheets   r   r   c                 C   s
   t | dS )Nzigor/HMCproc.htmlr
   r   r   r   r   menu   r   r   c                 C   s
   t | dS )Nzigor/hmc_menu.htmlr
   r   r   r   r   hmcmenu   r   r   c                 K   s   t | d|S )Nzigor/igor.htmlr
   )r   kwargsr   r   r   main   s    r   c                 C   s   | j dkrtdS | jd }ddlm} ||}| sHt| d|dS |j}d}| d	krdd
}n| dkr||	 
 }|st| d|dS tjj|d}|  t| |S )NPOSTigornamer   )identify_personu   I did not recognize ‘{0}’errorFfacultyTstudentz{0} is not a valid user)person)methodr   r   people.identifierr    r   formatr%   is_xsubclass	is_activer   objectscreatesendsend_download_info)r   r   r    pipeepZlegitdr   r   r   download   s$    

r3   c                 C   s   t | }t| dd|iS )Nzigor/send_download_info.htmlr3   )r   r   )r   r3   cr   r   r   r/   2   s    r/   c              
   C   s  | j dkrt| S z| jd }tjj|d}| dkrFt| ddW S |d }|jrft| fddiW S t	 |_|
  | jd	 }|d
rd|d  d}nd|d  d}t| d|W S  tk
 r } zt| d| dd W Y S d }~X Y nX t| S )Nr   key)r5   r   zEntered key was incorrect.r!   r"   zThat key has already been used.versionmacZIgor   z.dmgZ	setupIgorz.exez/var/private/docs/igor/{0}zEncountered error z. Please try again.)r&   r   r   r   r,   filtercount	retrievedr   nowsave
startswith	xsendfiler(   	Exception)r   r5   Z	downloadsZthe_downloadr6   filenameeepsr   r   r   do_download8   s(    




*rC   c                 C   s  t d| ddl}ddl}ddlm} ||d }|jrddlm	} |t
|d}|dkrnt||d}n t|d	d}d
|j| |d< |j||d< |S zLt|d}d|d< ddl}	|	j|d}
|
|d< d
|j| |d< W n   tY nX |S )z
        This is a fake version of xsendfile that actually serves the file from
        within Django. Note that the directories up the line must be executable for
        www-data and that the file itself must be visible to www-data.
    zxsendfile for {0}r   N)settings)FileWrapperrbz
image/jpeg)content_typezapplication/force-downloadzattachment; filename=zContent-DispositionzContent-Length zContent-Typezutf-8z
X-Sendfile)printr(   	mimetypesosdjango.confrD   
guess_typeDEBUGwsgiref.utilrE   openr   pathbasenamegetsizer   urllib.parseparsequoteencoder	   )r   rQ   rJ   rK   rD   contentTyperE   wrapperresponseurllibur   r   r   r?   P   sD      



r?   N)django.shortcutsr   r   django.templater   django.utilsr   Zigor.modelsr   django.httpr   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r3   r/   rC   r?   r   r   r   r   <module>   s(   