o
    嘡h                     @   s   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
 d
ejZG dd dejZG dd dejZdS )    )get_object_or_404)genericsstatus)Response)AllowAny)MultiPartParser
FormParser
JSONParser   )Course)CourseDetailSerializerCourseCreateSerializerc                   @   s8   e Zd ZdZeZegZdZe	j
 Zdd Zdd ZdS )CourseDetailViewz
    Retrieve detailed information for a specific course by its slug.
    GET: Retrieve course details
    PUT/PATCH: Update course details
    slugc                 C   s   | j d}tt|dS )zI
        Retrieve the course by slug, returning 404 if not found
        r   )r   )kwargsgetr   r   )selfr    r   "/var/www/apihucms/courses/views.py
get_object   s   zCourseDetailView.get_objectc                 C   s    | 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   r   r   r   get_serializer_class   s   z%CourseDetailView.get_serializer_classN)__name__
__module____qualname____doc__r   serializer_classr   permission_classeslookup_fieldr   objectsallquerysetr   r   r   r   r   r   r   
   s    
r   c                   @   s:   e Zd ZdZej ZegZ	e
eegZdd Zdd ZdS )CourseListCreateViewzj
    GET: List all courses with basic information
    POST: Create a new course with all related data
    c                 C   s   | j jdkrtS tS )NPOST)r   r   r   r   )r   r   r   r   r   /   s   z)CourseListCreateView.get_serializer_classc                 O   sR   | j |jd}| r| }t|}td|jdtjdS td|jdtj	dS )z=
        Create a new course with comprehensive data
        dataCourse created successfully)messagecourser   zCourse creation failed)r+   errors)
get_serializerr)   is_validsaver   r   r   HTTP_201_CREATEDr.   HTTP_400_BAD_REQUEST)r   r   argsr   
serializerr,   response_serializerr   r   r   create4   s    zCourseListCreateView.createN)r   r   r   r   r   r#   r$   r%   r   r!   r   r   r	   parser_classesr   r7   r   r   r   r   r&   &   s    

r&   c                   @   s6   e Zd ZdZej ZeZ	e
gZeeegZdd ZdS )CourseCreateViewzd
    POST: Create a new course with all related data
    Dedicated endpoint for course creation
    c              
   O   s   | j |jd}| rDz| }t|}tdd|j|j|jdtj	dW S  t
yC } ztddt|dtjdW  Y d	}~S d	}~ww tdd
|jdtjdS )zP
        Create a new course with comprehensive validation and response
        r(   Tr*   )successr+   	course_idcourse_slugr,   r-   Fz*Course creation failed due to server error)r:   r+   errorNz/Course creation failed due to validation errors)r:   r+   r.   )r/   r)   r0   r1   r   r   idr   r   r2   	ExceptionstrHTTP_500_INTERNAL_SERVER_ERRORr.   r3   )r   r   r4   r   r5   r,   r6   er   r   r   r7   W   s>   
zCourseCreateView.createN)r   r   r   r   r   r#   r$   r%   r   r    r   r!   r   r   r	   r8   r7   r   r   r   r   r9   M   s    

r9   N)django.shortcutsr   rest_frameworkr   r   rest_framework.responser   rest_framework.permissionsr   rest_framework.parsersr   r   r	   modelsr   r   r   r   RetrieveUpdateAPIViewr   ListCreateAPIViewr&   CreateAPIViewr9   r   r   r   r   <module>   s    '