o
    i	                     @   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	 d dl
mZ d dlmZmZ d dlmZ G d	d
 d
eZG dd dejZG dd dejZdS )    )viewsetsgenerics)Responsestatus)get_object_or_404)College)CollegeSerializer)ProgramProgramsSection)APIViewc                   @   s   e Zd ZdZdd ZdS )FindAProgramViewz
    Read-only API to fetch all colleges and their programs in the required format.
    Colleges are ordered by priority_order (ascending) then by name.
    c           	   
   C   s   t j dd}g }|D ]G}t|dd }g }|rK|j D ],}|j}||j|r,|j	nd |r2|j
nd |r?|jr?d|j dnd |rE|jnd d q||j|d qt|tjdS )	Npriority_ordernameprograms_sectionz/academics/programs/courses//)r   	course_idcourse_titlecourse_linkduration)r   programsr   )r   objectsallorder_bygetattrr   courseappendr   idtitleslugr   r   r   HTTP_200_OK)	selfrequestcollegesresultcolleger   programs_listprogramr    r(   #/var/www/apihucms/colleges/views.pyget   s(   

zFindAProgramView.getN)__name__
__module____qualname____doc__r*   r(   r(   r(   r)   r   
   s    r   c                   @   s"   e Zd ZdZej ZeZ	dZ
dS )CollegeViewSetz2
    A viewset for viewing college instances.
    r   N)r+   r,   r-   r.   r   r   r   querysetr	   serializer_classlookup_fieldr(   r(   r(   r)   r/   %   s
    
r/   c                   @   s*   e Zd ZdZej ZeZ	dZ
dd ZdS )CollegeDetailViewz)
    Retrieve a college by its slug.
    r   c                 O   sT   | d}ztt|d}| |}t|jW S  tjy)   tdditjd Y S w )Nr   )r   errorzCollege not foundr   )	r*   r   r   get_serializerr   dataDoesNotExistr   HTTP_404_NOT_FOUND)r!   r"   argskwargsr   r%   
serializerr(   r(   r)   r*   5   s   


zCollegeDetailView.getN)r+   r,   r-   r.   r   r   r   r0   r	   r1   r2   r*   r(   r(   r(   r)   r3   -   s    
r3   N)rest_frameworkr   r   rest_framework.responser   r   django.shortcutsr   colleges.modelsr   colleges.serializersr	   r
   r   rest_framework.viewsr   r   ReadOnlyModelViewSetr/   RetrieveAPIViewr3   r(   r(   r(   r)   <module>   s    