
    {i)                        d dl mZ d dlmZ d dlmZmZ ddlmZ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  G d dej.                        Z G d dej.                        Z e edddd      g       G d dej.                               Z e edg ddd      g       G d dej6                               Z G d dej.                        Z e ed d!dd"d#      g       G d$ d%ej.                               Z G d& d'ej.                        Z e ed(d)d*d+d,d,d-      g       G d. d/ej6                               Z  G d0 d1ej.                        Z!y2)3    )serializers)User)extend_schema_serializerOpenApiExample   )	StructureStructureUser
Invitation)	send_mail)settings)render_to_string)
strip_tags)*handle_structure_invoicing_settings_change)send_invitation_emailc                   >     e Zd Z G d d      Zd Z fdZd Z xZS )StructureSerializerc                        e Zd ZeZg dZg dZy)StructureSerializer.Meta)idname	image_urlstructure_typeinternal_reference_codestatus
base_price	occupancyratingtotal_unitsstreet_addresszip_codecountrylegal_entity_nametax_id_vat_number
istat_codedefault_currencydefault_language	time_zonedefault_check_in_timedefault_check_out_timedefault_tax_rateinvoicing_enabledreceipt_prefixinvoice_prefixdefault_vat_rate
created_at
updated_at)r   r/   r0   N)__name__
__module____qualname__r   modelfieldsread_only_fields     "/backend/structures/serializers.pyMetar      s    
< >r8   r:   c                     | j                   j                  d      }|r7t        |d      r+t        j                  j
                  dd|j                  i|S t        j                  d      )NrequestuserzUser context is missing.r7   )	contextgethasattrr   objectscreater=   r   ValidationError)selfvalidated_datar<   s      r9   rB   zStructureSerializer.create/   sW    ,,""9-ww/$$++PPPP))*DEEr8   c                     |j                   }|j                  }d|_        d|_        	 t        |   ||      }d|_        t        |||       |S # d|_        w xY w)NTF)previous_invoicing_enabledprevious_default_vat_rate)r+   r.   _skip_receipt_sync_skip_receipt_backfill_signalsuperupdater   )rD   instancerE   rG   rH   updated_instance	__class__s         r9   rL   zStructureSerializer.update5   sn    %-%?%?"$,$=$=!&*#15.	0$w~hG*/H'2'A&?	

   +0H's   A 	Ac                     |S )z2
        Check-in / Check-out validation.
        r7   rD   attrss     r9   validatezStructureSerializer.validateF   s	     r8   )r1   r2   r3   r:   rB   rL   rS   __classcell__)rO   s   @r9   r   r      s     >  >DF "r8   r   c                       e Zd ZdZ ej
                  d      Z ej                  d      Z ej                  dd      Z
 ej                  dd      Z G d d	      Zd
 Zd Zd Zy)StructureUserSerializerz+Serializer for structure-user relationshipsT
write_only	read_onlystructure.namesourcerZ   zcreated_by.usernamec                        e Zd ZeZg dZg dZy)StructureUserSerializer.Meta)	r   user_idr=   	structurestructure_nameroler/   r0   created_by_username)r   r/   r0   ra   N)r1   r2   r3   r	   r4   r5   r6   r7   r8   r9   r:   r_   S   s    

 Kr8   r:   c                    |j                   j                  |j                   j                  |j                   j                  |j                   j                  |j                   j
                  |j                   j                   d|j                   j
                   j                         xs |j                   j                  dS )zReturn user details )r   usernameemail
first_name	last_name	full_namer=   r   rg   rh   ri   rj   striprD   objs     r9   get_userz StructureUserSerializer.get_userb   s     ((++))XX^^((--++HH//0#((2D2D1EFLLNcRURZRZRcRc
 	
r8   c                     |j                  d      }t        j                  j                  |      }| j                  j                  d      }t        j                  j                  d||r|j                  nd d|S )Nr`   r   r<   )r=   
created_byr7   )popr   rA   r?   r>   r	   rB   r=   )rD   rE   r`   r=   r<   s        r9   rB   zStructureUserSerializer.createm   ss     $$Y/||7+,,""9-$$++ 
'.w||D
 
 	
r8   c                     	 t         j                  j                  |       |S # t         j                  $ r t	        j
                  d      w xY w)zValidate that user existsrr   User not foundr   rA   r?   DoesNotExistr   rC   rD   values     r9   validate_user_idz(StructureUserSerializer.validate_user_idx   sO    	@LL&     	@--.>??	@	    $ )AN)r1   r2   r3   __doc__r   IntegerFieldr`   SerializerMethodFieldr=   	CharFieldrb   rd   r:   rp   rB   r{   r7   r8   r9   rV   rV   L   sp    5&k&&$7G,;,,t<D*[**2BdSN/+//7LX\]K K	
	
r8   rV   zCreate Structure User Request   Editor)r`   rc   )rz   )examplesc                   L    e Zd ZdZ ej
                         Z G d d      Zd Zy)CreateStructureUserSerializerz7Serializer for creating new structure-user relationshipc                       e Zd ZeZddgZy)"CreateStructureUserSerializer.Metar`   rc   Nr1   r2   r3   r	   r4   r5   r7   r8   r9   r:   r      s    V$r8   r:   c                     	 t         j                  j                  |       |S # t         j                  $ r t	        j
                  d      w xY w)Nrr   rv   rw   ry   s     r9   r{   z.CreateStructureUserSerializer.validate_user_id   sO    	@LL&     	@--.>??	@r|   N)	r1   r2   r3   r}   r   r~   r`   r:   r{   r7   r8   r9   r   r      s'     B&k&&(G% %r8   r   zAdd Existing User Request)r   r      Viewer)user_idsrc   c                       e Zd ZdZ ej
                   ej                         d      Z ej                  e	j                  d      Zd Zy)AddExistingUsersSerializerz:Serializer for adding multiple existing users to structurezList of user IDs to add)child	help_textr   )choicesdefaultc                     t         j                  j                  |      j                         }|t	        |      k7  rt        j                  d      |S )zValidate all user IDs exist)id__inzSome users not found)r   rA   filtercountlenr   rC   )rD   rz   existing_userss      r9   validate_user_idsz,AddExistingUsersSerializer.validate_user_ids   sE    ,,E,:@@BSZ'--.DEEr8   N)r1   r2   r3   r}   r   	ListFieldr~   r   ChoiceFieldr	   ROLE_CHOICESrc   r   r7   r8   r9   r   r      sP     E${$$&k&&(+H #;""**D
r8   r   c                   L    e Zd ZdZ ej
                         Z G d d      Zd Zy)StructureUserListSerializerz1Simplified serializer for listing structure usersc                       e Zd ZeZg dZy) StructureUserListSerializer.Meta)r   r=   rc   r/   Nr   r7   r8   r9   r:   r      s    5r8   r:   c                 .   |j                   j                  |j                   j                  |j                   j                  |j                   j                   d|j                   j
                   j                         xs |j                   j                  dS )Nrf   r   rg   rh   rk   rl   rn   s     r9   rp   z$StructureUserListSerializer.get_user   sn    ((++))XX^^HH//0#((2D2D1EFLLNcRURZRZRcRc	
 	
r8   N)	r1   r2   r3   r}   r   r   r=   r:   rp   r7   r8   r9   r   r      s$    ;,;,,.D6 6
r8   r   zSend Invitation Requestznewuser@example.comz9Welcome to our team! Looking forward to working with you.rh   rc   messagec                   *    e Zd ZdZ G d d      Zd Zy)SendInvitationSerializerz"Serializer for sending invitationsc                       e Zd ZeZg dZy)SendInvitationSerializer.Metar   N)r1   r2   r3   r
   r4   r5   r7   r8   r9   r:   r      s    -r8   r:   c                    | j                   j                  d      }| j                   j                  d      }d }|r!	 t        j                  j                  |      }t        j                  j                  d||j                  d|}t        |      }|s	 |S # t        j                  $ r t        j                  d      w xY w)Nr<   structure_idrr   zStructure not found)ra   
invited_byr7   )r>   r?   r   rA   rx   r   rC   r
   rB   r=   r   )rD   rE   r<   r   ra   
invitation
email_sents          r9   rB   zSendInvitationSerializer.create   s    ,,""9-||''7 	I%--11\1B	
  ''.. 
||
 

 +:6
! )) I!112GHHIs    B )CN)r1   r2   r3   r}   r:   rB   r7   r8   r9   r   r      s     -. .r8   r   c                       e Zd ZdZ ej
                  dd      Z ej
                  dd      Z ej                  d      Z	 ej                  d      Z G d d      Zy	)
InvitationSerializerz(Full invitation serializer for responseszinvited_by.usernameTr\   r[   rY   c                        e Zd ZeZg dZg dZy)InvitationSerializer.Meta)r   rh   ra   rb   rc   r   r   invited_by_name
expires_ataccepted_at
is_expireddays_until_expiryr/   )r   r   r   r   r/   N)r1   r2   r3   r
   r4   r5   r6   r7   r8   r9   r:   r      s    

 Wr8   r:   N)r1   r2   r3   r}   r   r   r   rb   BooleanFieldr   r~   r   r:   r7   r8   r9   r   r      sa    2+k++3HTXYO*[**2BdSN)))D9J0004@W Wr8   r   zAccept Invitation RequestjohndoeJohnDoesecurePassword123)rg   ri   rj   passwordconfirm_passwordc                       e Zd ZdZ ej
                  d      Z ej
                  ddd      Z ej
                  ddd      Z ej
                  dd	      Z	 ej
                  d
      Z
d Zd Zy)AcceptInvitationSerializerz$Serializer for accepting invitations   )
max_length   FT)r   requiredallow_blank   )
min_lengthrX   rW   c                     t         j                  j                  |      j                         rt	        j
                  d      |S )N)rg   zUsername already exists)r   rA   r   existsr   rC   ry   s     r9   validate_usernamez,AcceptInvitationSerializer.validate_username  s7    <<.557--.GHHr8   c                 F    |d   |d   k7  rt        j                  d      |S )Nr   r   zPasswords do not match)r   rC   rQ   s     r9   rS   z#AcceptInvitationSerializer.validate   s,    &8 99--.FGGr8   N)r1   r2   r3   r}   r   r   rg   ri   rj   r   r   r   rS   r7   r8   r9   r   r     sx     /${$$4H&&&"uRVWJ%%%uRVWI${$$dCH,{,,=
r8   r   c                       e Zd ZdZ ej
                  dd      Z ej                  dd      Z ej                         Z
 G d d      Zd Zy	)
UserWithStructureSerializerz;Serializer to return users with their structure informationzstructure.idTr\   r[   c                       e Zd ZeZg dZy) UserWithStructureSerializer.Meta)r   r   rb   r=   rc   r/   Nr   r7   r8   r9   r:   r   +  s    
r8   r:   c                 .   |j                   j                  |j                   j                  |j                   j                  |j                   j                   d|j                   j
                   j                         xs |j                   j                  dS )zReturn user informationrf   r   rl   rn   s     r9   rp   z$UserWithStructureSerializer.get_user2  sp     ((++))XX^^HH//0#((2D2D1EFLLNcRURZRZRcRc	
 	
r8   N)r1   r2   r3   r}   r   r~   r   r   rb   r   r=   r:   rp   r7   r8   r9   r   r   %  sM    E+;++>TRL*[**2BdSN,;,,.D
 

r8   r   N)"rest_frameworkr   django.contrib.auth.modelsr   drf_spectacular.utilsr   r   modelsr   r	   r
   django.core.mailr   django.confr   django.template.loaderr   django.utils.htmlr   services.receipt_servicer   utilsr   ModelSerializerr   rV   r   
Serializerr   r   r   r   r   r   r7   r8   r9   <module>r      s   & + J 8 8 &   3 ( O (>+55 >@2k99 2h + 	

K$?$? 
 '% 	

!7!7 
$
+"="= 
  %. V	
	 {::   DW;66 W  '%$"/$7		
!7!7 $
+"="= 
r8   