
    嘡h                         d dl mZ d dlmZ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mZ  G d	 d
ej$                        Z G d dej(                        Z G d dej,                        Zy)    )get_object_or_404)genericsstatus)Response)AllowAny)MultiPartParser
FormParser
JSONParser   )Course)CourseDetailSerializerCourseCreateSerializerc                   ^    e Zd ZdZeZegZdZe	j                  j                         Zd Zd Zy)CourseDetailViewz
    Retrieve detailed information for a specific course by its slug.
    GET: Retrieve course details
    PUT/PATCH: Update course details
    slugc                 Z    | j                   j                  d      }t        t        |      S )zI
        Retrieve the course by slug, returning 404 if not found
        r   )r   )kwargsgetr   r   )selfr   s     "/var/www/apihucms/courses/views.py
get_objectzCourseDetailView.get_object   s#     {{v& d33    c                 N    | j                   j                  dv rddlm} |S t        S )zC
        Use CourseCreateSerializer for PUT/PATCH requests
        )PUTPATCHr   )r   )requestmethodserializersr   r   )r   r   s     r   get_serializer_classz%CourseDetailView.get_serializer_class   s&     <<"22;))%%r   N)__name__
__module____qualname____doc__r   serializer_classr   permission_classeslookup_fieldr   objectsallquerysetr   r    r   r   r   r   
   s8    
 ."L~~!!#H4&r   r   c                   `    e Zd ZdZej
                  j                         ZegZ	e
eegZd Zd Zy)CourseListCreateViewzj
    GET: List all courses with basic information
    POST: Create a new course with all related data
    c                 L    | j                   j                  dk(  rt        S t        S )NPOST)r   r   r   r   )r   s    r   r   z)CourseListCreateView.get_serializer_class/   s     <<&())%%r   c                 0   | j                  |j                        }|j                         rC|j                         }t	        |      }t        d|j                  dt        j                        S t        d|j                  dt        j                        S )z=
        Create a new course with comprehensive data
        dataCourse created successfully)messagecourser   zCourse creation failed)r3   errors)
get_serializerr1   is_validsaver   r   r   HTTP_201_CREATEDr6   HTTP_400_BAD_REQUEST)r   r   argsr   
serializerr4   response_serializers          r   createzCourseListCreateView.create4   s     ((gll(;
 __&F"8"@<166 ..  3$++ ..
 	
r   N)r    r!   r"   r#   r   r'   r(   r)   r   r%   r   r	   r
   parser_classesr   r?   r*   r   r   r,   r,   &   s9     ~~!!#H"%z:>N&

r   r,   c                   ^    e Zd ZdZej
                  j                         ZeZ	e
gZeeegZd Zy)CourseCreateViewzd
    POST: Create a new course with all related data
    Dedicated endpoint for course creation
    c                    | j                  |j                        }|j                         r[	 |j                         }t	        |      }t        dd|j                  |j                  |j                  dt        j                        S t        dd
|j                  dt        j                        S # t        $ r2}t        ddt        |      dt        j                        cY d	}~S d	}~ww xY w)zP
        Create a new course with comprehensive validation and response
        r0   Tr2   )successr3   	course_idcourse_slugr4   r5   Fz*Course creation failed due to server error)rD   r3   errorNz/Course creation failed due to validation errors)rD   r3   r6   )r7   r1   r8   r9   r   r   idr   r   r:   	ExceptionstrHTTP_500_INTERNAL_SERVER_ERRORr6   r;   )r   r   r<   r   r=   r4   r>   es           r   r?   zCourseCreateView.createW   s     ((gll(;
 #* '=V&D##'#@%+YY'-{{"5":": "22	 	*  L$++
 ..
 	
  #(#O!$Q
 "@@ s   AB1 1	C,:'C'!C,'C,N)r    r!   r"   r#   r   r'   r(   r)   r   r$   r   r%   r   r	   r
   r@   r?   r*   r   r   rB   rB   M   s:     ~~!!#H-"%z:>N)
r   rB   N)django.shortcutsr   rest_frameworkr   r   rest_framework.responser   rest_framework.permissionsr   rest_framework.parsersr   r	   r
   modelsr   r   r   r   RetrieveUpdateAPIViewr   ListCreateAPIViewr,   CreateAPIViewrB   r*   r   r   <module>rV      sT    . + , / J J  G&x55 &8$
855 $
N3
x-- 3
r   