U
    R^*                     @   s  d dl Z d dlZd dlZd dlZ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l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 eejejdZdZdZG dd dej Z!G dd dej Z"dd Z#G dd dej Z$G dd de$Z%dd Z&dS )    N)models)get_template)RichTextField)	mark_safe)FileSystemStorage)settings)CourseFileKind
CourseFile)	get_epoch)PERMISSION_CHOICES)CourseRecord)locationbase_url))lZLecture)rZ
Recitation)bLab)eZExam)hZHoliday))CzClaremont only)FzPhysics faculty only)1Anyone)LzOnly registered usersc                   @   sd   e Zd ZejedejdZejeddejdZ	ej
ddddZdd Zdd	 Zd
d ZdddZdS )
CourseSiteT)null	on_delete)blankr   r   z4Define any CSS styles here you wish used on the site)r   r   	help_textc                 C   s   d | jjS )NzCourseSite for {0})formatcoursenumberself r%   !/var/www/djphys/courses/models.py__str__5   s    zCourseSite.__str__c                 C   sJ   t jj| j|j|jd}| dkr2|j|jd}| dkrF|d S d S )N)r!   yearsemester   )part   r   )r   objectsfilterr!   r(   r)   countportion)r$   epochcrsr%   r%   r&   course_record8   s    
 zCourseSite.course_recordc              	   C   s   |  t|}|sd S tjj|d}td}g }d\}}}|D ]X}	|	jdkrZ|d7 }|}
n$|	jdkrr|d7 }|}
n|d7 }|}
|||	|
|d q>td}t	|||d	S )
N)r3   zcourses/meeting.html)r   r   r   r   r,   r   )meetingr"   requestzcourses/calendar.html)meetingsr3   )
r3   r   Meetingr-   r.   r   session_typeappendrenderr   )r$   r5   
course_recr6   tempZmeetsZlecsrecslabsmxr%   r%   r&   calendarA   s2    




zCourseSite.calendar c                 C   s   |  \}}dddddd}|| | }tddD ]}	||	 }
|
|kr2|	|  }|dk rb|d	7 }|tj|d
 }||kr2g }|
|kr|d |
|kr|d |
|kr|d |D ]}tjj|||d q|tjd	d
7 }qrq2d S )NMTWRr   )r   r,   r*         r         )daysr   r   r   )r3   r8   date)	spanrangeweekdaydatetime	timedeltar9   r7   r-   create)r$   r3   lecturesrecitationsr>   startfinishZdaymap
everythingdayZ	dayletterdtdZstypesstr%   r%   r&   make_calendar]   s0    


 zCourseSite.make_calendarN)rB   rB   rB   )__name__
__module____qualname__r   
ForeignKeyr   CASCADEr!   r
   logo	TextFieldstyler'   r3   rA   r\   r%   r%   r%   r&   r   .   s    	r   c                   @   s   e Zd ZejeejdZejde	dZ
e ZejddddZejddddZejddddZejddddZedddZe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G dd dZdS )r7   r   r,   )
max_lengthchoicesd   Trf   r   r   )r   r   c                 C   s   | j jS N)r3   r!   r#   r%   r%   r&   r!      s    zMeeting.coursec                 C   s^   d | jj| j|  }| jr.|d | j7 }| jrD|d | j7 }| jrZ|d | j7 }|S )Nz{0} {1} {2}z: {0}z, Reading: {0}z, Homework due: {0})r    r!   order_fieldrL   Zget_session_type_displaytopicreadinghomework)r$   sr%   r%   r&   r'      s    
 zMeeting.__str__c                 C   s   | j |S rj   )rL   strftime)r$   fr%   r%   r&   r       s    zMeeting.formatc                 C   s
   |  dS )Nz%Ar    r#   r%   r%   r&   DayName   s    zMeeting.DayNamec                 C   s
   |  dS )Nz%arr   r#   r%   r%   r&   Day   s    zMeeting.Dayc                 C   s
   |  dS )Nz%Brr   r#   r%   r%   r&   Month   s    zMeeting.Monthc                 C   s
   |  dS )Nz%brr   r#   r%   r%   r&   month   s    zMeeting.monthc                 C   s   d | jj| jjS )Nz{0}/{1})r    rL   rv   rX   r#   r%   r%   r&   
short_form   s    zMeeting.short_formc                 C   s*   i }dD ]}t | |rt | |||< q|S )N)rn   rm   exam)getattr)r$   dickr%   r%   r&   fields   s
    
zMeeting.fieldsc                   @   s   e Zd ZdgZdS )zMeeting.MetarL   N)r]   r^   r_   orderingr%   r%   r%   r&   Meta   s   r~   N)r]   r^   r_   r   r`   r   ra   r3   	CharFieldSESSION_TYPEr8   	DateFieldrL   rl   rn   rm   rx   r   notespropertyr!   r'   r    rs   rt   ru   rv   rw   r|   r~   r%   r%   r%   r&   r7   v   s&   
r7   c           
      C   sn   t | trX| jj}|jj}|j}|j}|j}|dkr8d}d	|||}t
jd||}nd}t
j||}	|	S )NarB   z	{0}{1}{2}coursesunknown)
isinstanceMeetingFiler4   r3   r!   rk   r(   r)   r+   r    ospathjoin)
objfilenamecreccnamer(   r)   r0   Zysemfolder	full_pathr%   r%   r&   FileLocation   s    
r   c                   @   s   e Zd ZejdddejdZejddZeje	ejdZ
ejeedZejddddZejd	ed
dZejdddZejdddZdd Zdd Zdd Zdd Zedd Zedd Zdd ZdS )SecureCourseFilez	auth.UserT)r   r   r   )auto_now_addre   )	upload_tostorage   ri   r,   r   )rf   rg   default)r   r   c                 C   s.   | j rt| j dkr| j n| j}d| j|S )Nr   z	{0} - {1})descriptionlendocumentr    kind)r$   extrar%   r%   r&   r'      s    
zSecureCourseFile.__str__c                 C   s   | j p
| jS rj   )release_timerelease_untilr#   r%   r%   r&   
has_window   s    zSecureCourseFile.has_windowc                 C   s:   t j tj}| jr"|| jk r"dS | jr6|| jkr6dS dS )NFT)rP   nowpytzutcr   r   )r$   r   r%   r%   r&   	in_window   s    zSecureCourseFile.in_windowc                 C   s,   |j jrdS | jdkr$|jd s$dS |  S )NTr   	ClaremontF)useris_authenticatedaccesssessionr   )r$   r5   r%   r%   r&   
has_access   s    

zSecureCourseFile.has_accessc                 C   sp   t jt| jd }|dd   }dddddddddddd	d
}z|| }W n   d}Y nX d|}|S )Nr,   excelwordjpgnbpdfpngtextxt)xlsxlsxdocdocxr   jpegr   r   r   r   latexr   unkzico/{0}.png)r   r   splitextstrr   lowerr    )r$   extmappingicoiconr%   r%   r&   r     s*    

zSecureCourseFile.iconc                 C   s   t jd| jS )Nz/c/private/{0})urllibparsequoter    r   r#   r%   r%   r&   url  s    zSecureCourseFile.urlc                 C   sp   | j rt| j dkr| j n| j}|jjr,dnd}|  rL||  rFdnd7 }d}|| j|t	j
| j|}t|S )Nr   profstudentz	 inwindowz
 outwindowzD<a href="{0}" class="{1}"><img src="{2}{3}" width="36">&nbsp;{4}</a>)r   r   r   r   r   r   r   r    r   r   
STATIC_URLr   r   )r$   r5   descklassro   r   r%   r%   r&   link#  s    zSecureCourseFile.linkN)r]   r^   r_   r   r`   ra   uploaded_byDateTimeFielddate_postedr	   r   	FileFieldr   
private_fsr   r   r   ACCESS_CHOICESr   r   r   r'   r   r   r   r   r   r   r   r%   r%   r%   r&   r      s2      

r   c                   @   s   e Zd ZejeejdZdS )r   re   N)r]   r^   r_   r   r`   r7   ra   r4   r%   r%   r%   r&   r   .  s   r   c                  C   s@   t jjdd} tjj| dd}tjj| d}|j|ddd d S )	Np117)rk   i  )r!   r(   )r!   ZTRr   )rS   rT   )r   r-   getr   r   r\   )r   r   Zcsiter%   r%   r&   test5  s    r   )'rP   r   r   urllib.parser   	django.dbr   django.template.loaderr   djrichtextfield.modelsr   django.utils.safestringr   django.core.files.storager   django.confr   djphys.modelsr   r	   r
   djphys.funcsr   djphys.constantsr   people.modelsr   PRIVATE_ROOTPRIVATE_URLr   r   r   Modelr   r7   r   r   r   r   r%   r%   r%   r&   <module>   s2    	H65M