3
YK])                 @   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d0Zd5Z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 )6    N)models)get_template)RichTextField)	mark_safe)FileSystemStorage)settings)CourseFileKind
CourseFile)	get_epoch)PERMISSION_CHOICES)CourseRecord)locationbase_urllLecturer
RecitationbLabeExamhHolidayCClaremont onlyFPhysics faculty only1AnyoneLOnly registered usersc               @   s\   e Zd ZejeddZejedd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)blankr#   z4Define any CSS styles here you wish used on the site)r#   r$   	help_textc             C   s   dj | jjS )NzCourseSite for {0})formatcoursenumber)self r*   !/var/www/djphys/courses/models.py__str__4   s    zCourseSite.__str__c             C   sJ   t jj| j|j|jd}|j dkr2|j|jd}|j 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_record7   s    
zCourseSite.course_recordc             C   s   | j t|}|sd S tjj|d}td}g }d
\}}}x`|D ]X}	|	jdkr\|d7 }|}
n$|	jdkrt|d7 }|}
n|d7 }|}
|j|j|	|
|d q@W td}t	|j||d	S )N)r8   zcourses/meeting.htmlr   r   r1   r   )meetingr(   requestzcourses/calendar.html)meetingsr8   )r   r   r   )
r8   r   Meetingr2   r3   r   session_typeappendrenderr   )r)   r:   
course_recr;   tempZmeetsZlecsrecslabsmxr*   r*   r+   calendar@   s.    



zCourseSite.calendar c             C   s   |j  \}}dddddd}|| | }xtddD ]}	||	 }
|
|kr4|	|j  }|dk rd|d	7 }|tj|d
 }xv||krg }|
|kr|jd |
|kr|jd |
|kr|jd x|D ]}tjj|||d qW |tjd	d
7 }qvW q4W d S )NMTWRr   )r   r1   r/         r         )daysr   r   r   )r8   r=   date)	spanrangeweekdaydatetime	timedeltar>   r<   r2   create)r)   r8   lecturesrecitationsrC   startfinishZdaymap
everythingdayZ	dayletterdtdZstypesstr*   r*   r+   make_calendar\   s,    




zCourseSite.make_calendarN)rG   rG   rG   )__name__
__module____qualname__r   
ForeignKeyr   r'   r
   logo	TextFieldstyler,   r8   rF   ra   r*   r*   r*   r+   r"   .   s   	r"   c               @   s   e Zd ZejeZejdedZ	ej
 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 )r<   r1   )
max_lengthchoicesd   T)ri   r$   r#   )r$   r#   c             C   s   | j jS )N)r8   r'   )r)   r*   r*   r+   r'      s    zMeeting.coursec             C   s^   dj | jj| j| j }| jr.|dj | j7 }| jrD|dj | j7 }| jrZ|dj | j7 }|S )Nz{0} {1} {2}z: {0}z, Reading: {0}z, Homework due: {0})r&   r'   order_fieldrQ   Zget_session_type_displaytopicreadinghomework)r)   sr*   r*   r+   r,      s    
zMeeting.__str__c             C   s   | j j|S )N)rQ   strftime)r)   fr*   r*   r+   r&      s    zMeeting.formatc             C   s
   | j dS )Nz%A)r&   )r)   r*   r*   r+   DayName   s    zMeeting.DayNamec             C   s
   | j dS )Nz%a)r&   )r)   r*   r*   r+   Day   s    zMeeting.Dayc             C   s
   | j dS )Nz%B)r&   )r)   r*   r*   r+   Month   s    zMeeting.Monthc             C   s
   | j dS )Nz%b)r&   )r)   r*   r*   r+   month   s    zMeeting.monthc             C   s   dj | jj| jjS )Nz{0}/{1})r&   rQ   rv   r]   )r)   r*   r*   r+   
short_form   s    zMeeting.short_formc             C   s.   i }x$dD ]}t | |r
t | |||< q
W |S )Nro   rn   exam)ro   rn   rx   )getattr)r)   dickr*   r*   r+   fields   s
    

zMeeting.fieldsc               @   s   e Zd ZdgZdS )zMeeting.MetarQ   N)rb   rc   rd   orderingr*   r*   r*   r+   Meta   s   r~   N)rb   rc   rd   r   re   r   r8   	CharFieldSESSION_TYPEr=   	DateFieldrQ   rm   ro   rn   rx   r   notespropertyr'   r,   r&   rs   rt   ru   rv   rw   r|   r~   r*   r*   r*   r+   r<   u   s$   
r<   c       
      C   sn   t | trX| jj}|jj}|j}|j}|j}|dkr8d}dj	|||}t
jjd||}nd}t
jj||}	|	S )NarG   z	{0}{1}{2}coursesunknown)
isinstanceMeetingFiler9   r8   r'   rl   r-   r.   r0   r&   ospathjoin)
objfilenamecreccnamer-   r.   r5   Zysemfolder	full_pathr*   r*   r+   FileLocation   s    
r   c               @   s   e Zd ZejddddZejddZeje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$   )auto_now_add)	upload_tostorage   )ri   r$   r#   r1   r   )ri   rj   defaultc             C   s.   | j rt| j dkr| j n| j}dj| 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 )N)release_timerelease_until)r)   r*   r*   r+   
has_window   s    zSecureCourseFile.has_windowc             C   s:   t j jtj}| jr"|| jk r"dS | jr6|| jkr6dS dS )NFT)rU   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 | j S )NTr   	ClaremontF)useris_authenticatedaccesssessionr   )r)   r:   r*   r*   r+   
has_access   s    


zSecureCourseFile.has_accessc             C   sp   t jjt| jd }|dd  j }dddddddddddd	d
}y|| }W n   d}Y nX dj|}|S )Nr1   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jdj| jS )Nz/c/private/{0})urllibparsequoter&   r   )r)   r*   r*   r+   url  s    zSecureCourseFile.urlc             C   sr   | j rt| j dkr| j n| j}|jj r.dnd}| j rN|| j rHdnd7 }d}|j| 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)   r:   descklassrp   r   r*   r*   r+   link!  s    zSecureCourseFile.linkN)rb   rc   rd   r   re   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      s"   
r   c               @   s   e Zd ZejeZdS )r   N)rb   rc   rd   r   re   r<   r9   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)rl   i  )r'   r-   )r'   ZTRr   )rX   rY   )r   r2   getr   r"   ra   )r   r   Zcsiter*   r*   r+   test3  s    r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r    r!   )r   r   r   r   )'rU   r   r   urllib.parser   	django.dbr   django.template.loaderr   mezzanine.core.fieldsr   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"   r<   r   r   r   r   r*   r*   r*   r+   <module>   s<   	       G65L