
    撾i                         d 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mZmZmZ dd	lmZ  ej$                  e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)zM
DRF API views for face registration, identification, listing, and deletion.
    Nstatus)Response)APIView   )
FaceRecord)FaceRecordSerializerIdentifyResultSerializerIdentifySerializerRegisterSerializer)FaceRecognitionServicec                       e Zd ZdZd Zy)RegisterFaceViewzR
    POST /api/faces/register/
    Register a new face with a name and photo.
    c                 0   t        |j                        }|j                         s(t        d|j                  dt
        j                        S 	 t               }|j                  |j                  d   |j                  d   |j                  j                  di             }t        |      }t        |j                  t
        j                        S # t        $ rP}t        j                  d	t!        |             t        t!        |      d
dt
        j                        cY d }~S d }~wt"        $ rc}t        j%                  dt!        |      t'        j(                                t        dt!        |      dt
        j*                        cY d }~S d }~ww xY w)NdataInvalid input data.errordetailr   namephotometadata)r   image_inputr   zFace registration failed: %s+Face detection failed during preprocessing.zFace registration error: %s
%sz4An internal error occurred during face registration.)r   r   is_validr   errorsr   HTTP_400_BAD_REQUESTr   register_facevalidated_datagetr	   HTTP_201_CREATED
ValueErrorloggerwarningstr	Exceptionr   	traceback
format_excHTTP_500_INTERNAL_SERVER_ERROR)selfrequest
serializerservicerecordresponse_serializeres          (/var/www/face-recognition/faces/views.pypostzRegisterFaceView.post   sZ   'W\\:
""$2(// 22 	,.G**..v6&55g>#2266z2F + F
 #7v">/44V=T=TUU 	NN93q6B VK 22   	LL:CFIDXDXDZ[S!!f << 	s3   BC 	FAD& F&F2AF
FFN__name__
__module____qualname____doc__r3        r2   r   r      s    
'r:   r   c                       e Zd ZdZd Zy)IdentifyFaceViewzc
    POST /api/faces/identify/
    Identify a face by comparing against registered embeddings.
    c                    t        |j                        }|j                         s(t        d|j                  dt
        j                        S 	 t               }|j                  |j                  d         }t        |      }t        |j                  t
        j                        S # t        $ rP}t        j                  dt        |             t        t        |      ddt
        j                        cY d }~S d }~wt         $ rc}t        j#                  d	t        |      t%        j&                                t        d
t        |      dt
        j(                        cY d }~S d }~ww xY w)Nr   r   r   r   r   )r   zFace identification failed: %sr   z Face identification error: %s
%sz6An internal error occurred during face identification.)r   r   r   r   r   r   r   r   identify_facer    r
   HTTP_200_OKr#   r$   r%   r&   r'   r   r(   r)   r*   )r+   r,   r-   r.   resultr0   r1   s          r2   r3   zIdentifyFaceView.postN   s6   'W\\:
""$2(// 22 	,.G**&55g> + F #;6"B/44V=O=OPP 	NN;SVD VK 22   	LL<c!fiFZFZF\]U!!f << 	s3   AB) )	E,2AC=7E,=E,	AE'!E,'E,Nr4   r9   r:   r2   r<   r<   H   s    
%r:   r<   c                       e Zd ZdZd Zy)FaceListViewzM
    GET /api/faces/
    List all registered faces (without embeddings).
    c                 L   	 t               }|j                         }t        |t        j                        S # t
        $ rc}t        j                  dt        |      t        j                                t        dt        |      dt        j                        cY d }~S d }~ww xY w)Nr   zFace listing error: %s
%sz/An internal error occurred while listing faces.r   )r   list_registered_facesr   r   r?   r'   r$   r   r&   r(   r)   r*   )r+   r,   r.   facesr1   s        r2   r!   zFaceListView.get|   s    	,.G113EE&*<*<== 	LL5s1vy?S?S?UVN!!f << 	s   47 	B# ABB#B#Nr5   r6   r7   r8   r!   r9   r:   r2   rB   rB   v   s    
r:   rB   c                       e Zd ZdZd Zy)FaceDeleteViewzN
    DELETE /api/faces/<int:face_id>/
    Delete a registered face by ID.
    c                    	 t               }|j                  |      }|rt        t        j                        S t        dd| ddt        j
                        S # t        $ rc}t        j                  dt        |      t        j                                t        dt        |      dt        j                        cY d }~S d }~ww xY w)Nr   zFace record not found.zNo face record with id=z exists.r   zFace deletion error: %s
%sz3An internal error occurred while deleting the face.)r   delete_facer   r   HTTP_204_NO_CONTENTHTTP_404_NOT_FOUNDr'   r$   r   r&   r(   r)   r*   )r+   r,   face_idr.   deletedr1   s         r2   deletezFaceDeleteView.delete   s    	,.G))'2Gv'A'ABB!9$;G9H"M "44   	LL6A	@T@T@VWR!!f << 	s#   6A !A 	C$AC<CCN)r5   r6   r7   r8   rO   r9   r:   r2   rH   rH      s    
r:   rH   c                       e Zd ZdZd Zy)HealthCheckViewzg
    GET /api/health/
    Health check endpoint returning server status and registered face count.
    c                 6   	 t         j                  j                         }t        d|dt        j
                        S # t        $ rQ}t        j                  dt        |             t        ddt        |      dt        j                        cY d }~S d }~ww xY w)Nok)r   registered_facesr   zHealth check error: %sr   r   )r   rT   r   )r   objectscountr   r   r?   r'   r$   r   r&   r*   )r+   r,   rV   r1   s       r2   r!   zHealthCheckView.get   s    	&&,,.E"(- ))   		LL13q6:%() V
 << 		s   ;> 	BABBBNrF   r9   r:   r2   rQ   rQ      s    
r:   rQ   )r8   loggingr(   rest_frameworkr   rest_framework.responser   rest_framework.viewsr   modelsr   serializersr	   r
   r   r   services.recognitionr   	getLoggerr5   r$   r   r<   rB   rH   rQ   r9   r:   r2   <module>r_      s~      ! , (   9			8	$-w -`+w +\7 .W @g r:   