
    ?&j                    V   d Z ddl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 G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Zy)zf
alloggiati/serializers.py
=========================
DRF serializers for the Alloggiati Web sync API.
    )annotations)serializers)AlloggiatiCredentialAlloggiatiSyncLogc                  \    e Zd ZdZ ej
                  d      Z ej
                  d      Zy)AlloggiatiFieldErrorSerializerz&A single field-level validation error.z#Guest field that failed validation.	help_textz*Human-readable description of the failure.N)__name__
__module____qualname____doc__r   	CharFieldfieldmessage     "/backend/alloggiati/serializers.pyr   r      s+    0#k##.STE#k##.Z[Gr   r   c                      e Zd ZdZ ej
                  dd      Z ej
                  dd      Z ej                  d      Z	 e
dd	      Zy
)(AlloggiatiGuestValidationErrorSerializerz)All validation errors for a single guest.TzGuest primary key.
allow_nullr
   zBooking primary key.z)Guest display name (no document numbers).r	   z7List of field-level validation failures for this guest.manyr
   N)r   r   r   r   r   IntegerFieldguest_id
booking_idr   
guest_namer   errorsr   r   r   r   r      sR    3)))TEYZH)))TE[\J&&&1\]J/KFr   r   c                      e Zd ZdZ ej
                  d      Z ej                  dgd      Z ej                  dgd      Z	d Z
y	)
AlloggiatiSyncRequestSerializerz5Validates the POST /api/alloggiati/sync request body.z%Primary key of the Structure to sync.r	   z%Y-%m-%dz-Start of sync window (YYYY-MM-DD, inclusive).)input_formatsr
   z+End of sync window (YYYY-MM-DD, inclusive).c                F    |d   |d   kD  rt        j                  d      |S )NdateFromdateToz%dateFrom cannot be later than dateTo.)r   ValidationError)selfdatas     r   validatez(AlloggiatiSyncRequestSerializer.validate6   s0    
d8n,--7  r   N)r   r   r   r   r   r   structureId	DateFieldr$   r%   r)   r   r   r   r!   r!   '   sW    ?*+**9K %{$$!lAH #[""!l?F
r   r!   c                      e Zd ZdZ ej
                  g dd      Z ej                  d      Z ej                  d      Z	 ej                  d      Z ej                  d	d
      Z ed	d      Zy) AlloggiatiSyncResponseSerializerz6Documents the POST /api/alloggiati/sync response body.)	CONNECTEDPARTIALERRORzOutcome of the sync operation.)choicesr
   z,Number of guests accepted by Alloggiati Web.r	   z,Total guests rejected (validation + server).zHuman-readable summary.Tz/UUID of the persisted AlloggiatiSyncLog record.r   zUPer-guest structured validation errors. Empty list when all guests passed validation.r   N)r   r   r   r   r   ChoiceFieldstatusr   sentrejectedr   r   	UUIDFieldsync_log_idr   validation_errorsr   r   r   r-   r-   >   s    @$[$$12F ({''2`aD'{''2`aH${$$/HIG'+''CK A<r   r-   c                  $    e Zd ZdZ G d d      Zy)AlloggiatiSyncLogSerializerz=Read-only serializer for AlloggiatiSyncLog list/detail views.c                      e Zd ZeZg dZeZy) AlloggiatiSyncLogSerializer.Meta)id	structure	date_fromdate_tor3   r   guests_sentguests_rejectedr8   
started_atcompleted_atN)r   r   r   r   modelfieldsread_only_fieldsr   r   r   Metar<   \   s    !
 "r   rH   N)r   r   r   r   rH   r   r   r   r:   r:   Y   s    G" "r   r:   c                      e Zd Zd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	 G d	 d
      Z
d Zd Zd Z	 	 	 	 	 	 ddZy)#AlloggiatiCredentialWriteSerializerz
    Write serializer for creating / updating AlloggiatiCredential.

    Accepts plain-text credential values; encryption is handled by the
    model property setters.
    FTz%Alloggiati Web username (CODES mode).)requiredallow_blank
write_onlyr
   z7Alloggiati Web password / web service key (CODES mode).z:PEM-encoded client certificate (DIGITAL_CERTIFICATE mode).z3PEM-encoded private key (DIGITAL_CERTIFICATE mode).c                      e Zd ZeZg dZy)(AlloggiatiCredentialWriteSerializer.Meta)modeusernamepasswordcertificateprivate_keyN)r   r   r   r   rE   rF   r   r   r   rH   rO      s    $Or   rH   c                F   |j                  dt        | j                  dt        j                              }| j                  d u}|t        j                  k(  rz|j                  d      }||j                         dk(  rt        j                  d      |s;|j                  d      }||j                         dk(  rt        j                  d      |S |t        j                  k(  r=|j                  d      }|j                  d      }|s|r|st        j                  d	      |S )
NrP   rQ    z$username is required for CODES mode.rR   z$password is required for CODES mode.rS   rT   zFcertificate and private_key are required for DIGITAL_CERTIFICATE mode.)	getgetattrinstancer   
MODE_CODESstripr   r&   MODE_DIGITAL_CERTIFICATE)r'   r(   rP   	is_updaterQ   rR   rS   rT   s           r   r)   z,AlloggiatiCredentialWriteSerializer.validate   s   xxDMM6+?+J+JK

 MM-	'222xx
+H 8>>#3r#9!11: 
 88J/#x~~'72'=%55>   )BBB((=1K((=1K "+%55`  r   c                \    | j                   d   }t        |      }| j                  ||      S )Nr>   )r>   )contextr   _apply_fields)r'   validated_datar>   
credentials       r   createz*AlloggiatiCredentialWriteSerializer.create   s-    LL-	)I>
!!*n==r   c                &    | j                  ||      S )N)r`   )r'   rY   ra   s      r   updatez*AlloggiatiCredentialWriteSerializer.update   s    !!(N;;r   c                    |j                  d|j                        |_        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        d|v r
|d   |_        |j                          |S )NrP   rQ   rR   rS   rT   )rW   rP   rQ   rR   rS   rT   save)r'   rb   r(   s      r   r`   z1AlloggiatiCredentialWriteSerializer._apply_fields   s~     ((6:??;
"&z"2J"&z"2JD %)-%8J"D %)-%8J"r   N)rb   r   r(   dictreturnr   )r   r   r   r   r   r   rQ   rR   rS   rT   rH   r)   rc   re   r`   r   r   r   rJ   rJ   r   s     %{$$9	H %{$$K	H (+''N	K (+''G	KP P$L>
<.6:	r   rJ   c                      e Zd ZdZ ej
                         Z ej                         Z ej                  d      Z
 ej                  d      Z ej                  d      Zy)$AlloggiatiCredentialStatusSerializerz9Read-only status payload for credential connection state.T)r   N)r   r   r   r   r   r   structure_idBooleanField	connectedr   rP   DateTimeField
created_at
updated_atr   r   r   rk   rk      s[    C+;++-L+;++-I(;((D9D,;,,=J,;,,=Jr   rk   N)r   
__future__r   rest_frameworkr   alloggiati.modelsr   r   
Serializerr   r   r!   r-   ModelSerializerr:   rJ   rk   r   r   r   <module>rw      s    # & E\[%;%; \{/E/E k&<&< .{'='= 6"+"="= "2`+*E*E `F>;+A+A >r   