
    yi+                        d dl 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 dd
l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 G d dej,                        Z G d dej,                        ZeZy)    )serializers   )CheckInTemplateCheckInTemplateFieldStructureCheckInTemplate)generate_random_slug)slugify)DEFAULT_FIELD_TYPE_MAP)	Structure)timezone)transaction)generate_unique_slugc                        e Zd Z G d d      Zy)$CheckInTemplateFieldCreateSerializerc                       e Zd ZeZdZy))CheckInTemplateFieldCreateSerializer.Meta)	sluglabel	help_text
field_typetarget
is_enabledis_requiredmetaorderN__name__
__module____qualname__r   modelfields     /backend/checkin/serializers.pyMetar      s    $

r#   r%   N)r   r   r   r%   r"   r#   r$   r   r      s    
 
r#   r   c                   p    e Zd Z ej                  dd      Z ej                         Z G d d      Zd Z	y)CheckInTemplateListSerializerzfields.countT)source	read_onlyc                       e Zd ZeZdZy)"CheckInTemplateListSerializer.Meta)idnamer   description	is_activefields_count
structures
created_atN)r   r   r   r   r    r!   r"   r#   r$   r%   r+      s    	
r#   r%   c                     |j                   j                         D cg c]/  }|j                  j                  |j                  j                  d1 c}S c c}w )zD
        Returns list of structures linked to this template
        )r,   r-   )structure_linksall	structurer,   r-   )selfobjlinks      r$   get_structuresz,CheckInTemplateListSerializer.get_structures,   sX     ++//1

 2 nn''++ 2
 	
 
s   4AN)
r   r   r   r   IntegerFieldr0   SerializerMethodFieldr1   r%   r:   r"   r#   r$   r'   r'      s5    +;++>TRL2224J
 


r#   r'   c                       e Zd Z ej                         Z ej                         Z ej                  d      Zd Z	e
j                  d        Zy)!LinkTemplateToStructureSerializerTdefaultc                 .   |d   }|d   }t         j                  j                  |      j                         st	        j
                  ddi      t        j                  j                  |      j                         st	        j
                  ddi      |S )Nstructure_idtemplate_idr,   Structure not found.zCheck-in template not found.)r   objectsfilterexistsr   ValidationErrorr   )r7   attrsrB   rC   s       r$   validatez*LinkTemplateToStructureSerializer.validate=   s    ^,M*  ''<'8??A-- 6/   &&---=DDF--=/   r#   c                    |d   }|d   }|d   }t         j                  j                  |      j                         }|rb|j                  |k7  }||_        ||_        |rt        j                         |_        ddg}|r|j                  d       |j                  |       n5t         j                  j                  |||t        j                                |||dS )	NrB   rC   r/   rB   assigned_at)update_fields)rB   rC   r/   rN   )rB   rC   r/   )r   rF   rG   firstrC   r/   r   nowrN   appendsavecreate)r7   validated_datarB   rC   r/   r9   template_changedfields_to_updates           r$   rT   z(LinkTemplateToStructureSerializer.createM   s    %n5$]3";/	'//66% 7 

%' 	
 #//;>*D&DN#+<<>  -{; ''6II$4I5 %,,33)'#$LLN	 4  )&"
 	
r#   N)r   r   r   r   r;   rB   rC   BooleanFieldr/   rK   r   atomicrT   r"   r#   r$   r>   r>   8   sS    +;++-L*+**,K(((6I  &
 &
r#   r>   c                       e Zd Z ej                         Z ej                         Z ej                         Z ej                         Z	 ej                         Z
 ej                         Z ej                  d      Z ej                         ZyStructureCheckInFormSerializerT)
allow_nullN)r   r   r   r   r;   rB   	CharFieldstructure_namestructure_typestreet_addresszip_codecountry	DictFieldcheckin_formrX   r/   r"   r#   r$   r\   r\   v   s    +;++-L*[**,N*[**,N*[**,N${$$&H#k##%G(;((D9L(((*Ir#   r\   c                   P    e Zd Z ej                         Z ej                         Zy)StructureMiniSerializerN)r   r   r   r   r;   r,   r^   r-   r"   r#   r$   rg   rg      s"    	!	!	!	#B ;  "Dr#   rg   c                       e Zd Z ej                  d      Z ej                  d      Z ej                         Z	 G d d      Z
d Zy)CheckInFormFieldSerializerr   )r(   r   c                       e Zd ZeZdZy)CheckInFormFieldSerializer.Meta)r   r   typerequiredcategoryNr   r"   r#   r$   r%   rk      s    $
r#   r%   c                 R    |j                   sy |j                   j                  d      S )Nrn   )r   get)r7   r8   s     r$   get_categoryz'CheckInFormFieldSerializer.get_category   s    xxxx||J''r#   N)r   r   r   r   r^   rl   rX   rm   r<   rn   r%   rq   r"   r#   r$   ri   ri      sE     ;  5D'{''}=H0{002H
 
(r#   ri   c                       e Zd Z ej                         Z ej                         Z ej                  d      Z	 ej                         Zyr[   )r   r   r   r   r;   rB   r^   r_   rd   re   rX   r/   r"   r#   r$   r\   r\      sD    +;++-L*[**,N(;((D9L(((*Ir#   c                   X    e Zd Z ej                         Zd Zej                  d        Z	y)%UnlinkTemplateFromStructureSerializerc                 $   |d   }t         j                  j                  |      j                         st	        j
                  ddi      t        j                  j                  |      j                         st	        j
                  ddi      |S )NrB   rD   rE   rM   z1No check-in template is linked to this structure.)r   rF   rG   rH   r   rI   r   )r7   rJ   rB   s      r$   rK   z.UnlinkTemplateFromStructureSerializer.validate   s    ^,  ''<'8??A-- 6/   (//66% 7 

&( -- S/   r#   c                 p    |d   }t         j                  j                  |      j                          d|iS )NrB   rM   )r   rF   rG   delete)r7   rU   rB   s      r$   rT   z,UnlinkTemplateFromStructureSerializer.create   s:    %n5 ((//% 	0 	

&(--r#   N)
r   r   r   r   r;   rB   rK   r   rY   rT   r"   r#   r$   rt   rt      s1    +;++-L" . .r#   rt   c                       e Zd ZdZ ej
                         Z ej                  dd      Z ej
                  dd      Z	 ej                  d      Z ej                         Zd Zd Zd Zd	 Zd
 Zy)CheckInTemplateUpsertSerializerz
    Unified serializer for both create and update operations.
    Accepts sections-based structure matching the Get API response.
    FT)rm   allow_blankr?   c                     |st        j                  d      t        d |j                         D              }|dk(  rt        j                  d      |S )z'Ensure sections have at least one fieldz-At least one section with fields is required.c              3   2   K   | ]  }t        |        y w)N)len).0r!   s     r$   	<genexpr>zDCheckInTemplateUpsertSerializer.validate_sections.<locals>.<genexpr>   s     D^63v;^s   r   z9At least one field is required to create a check-in form.)r   rI   sumvalues)r7   valuetotal_fieldss      r$   validate_sectionsz1CheckInTemplateUpsertSerializer.validate_sections   sX    --?  DU\\^DD1--K  r#   c                    |j                  d      }|j                  d      }|s]t        |      }| j                  r1| j                  j                  r| j                  j                  |d<   |S t	        t
        |      }||d<   |S )z,Auto-generate slug from name if not providedr-   r   )rp   r	   instancer   r   r   )r7   rJ   r-   r   	base_slugunique_slugs         r$   rK   z(CheckInTemplateUpsertSerializer.validate   su    yy yy I }}!3!3 $ 2 2f  3?IN +fr#   c                 >   g }d}|j                         D ]  \  }}|D ]{  }||j                  dd      d}d|v r|d   |d<   |j                  dd      }|j                  |d   |d	   ||j                  d
d      ||dd|j                  dd      d	       |dz  }}  |S )z
        Convert sections structure to flat list of fields.
        Matches the format from GetStructureCheckInFormAPIView response.
        r   rn    )sectionrn   choicesrl   textr   r   rm   FbothTr   )	r   r   r   r   r   r   r   r   r   )itemsrp   rR   )	r7   sectionsfields_datar   r   r!   fieldr   r   s	            r$   _flatten_sections_to_fieldsz;CheckInTemplateUpsertSerializer._flatten_sections_to_fields   s    
 '~~/OGV  ' %		*b 9 %&+I&6DO #YYvv6
""!&M"7^",#(99Z#? "$"&!&;!;
$ 
 
3    08 r#   c           
         |j                  d      }t        j                  j                  di |}| j	                  |      }t
        j                  j                  |D cg c]  }t        dd|i| c}       |S c c}w )z-Create new template with fields from sectionsr   templater"   )popr   rF   rT   r   r   bulk_create)r7   rU   r   r   r   r   s         r$   rT   z&CheckInTemplateUpsertSerializer.create  s    !%%j1 #**11CNC 66x@$$00$2
$ !<(<e<$2
 	
 2
s   A<c                    |j                  d      }|j                         D ]  \  }}t        |||        |j                          | j	                  |      }|j
                  j                         D ci c]1  }|j                   d|j                  xs i j                  d       |3 }}t               }	|D ]  }
|
d    d|
d   d    }|	j                  |       ||v r;||   }|
j                         D ]  \  }}t        |||        |j                          ct        j                  j                  dd|i|
  |j                         D ]  \  }}||	vs|j                           |S c c}w )z-Update template and sync fields from sectionsr   :r   r   r   r   r"   )r   r   setattrrS   r   r!   r5   r   r   rp   setaddr   rF   rT   rw   )r7   r   rU   r   attrr   r   fexisting_fieldsincoming_slugs
field_datakeyr8   	field_objs                 r$   updatez&CheckInTemplateUpsertSerializer.update   s   !%%j1 *//1KD%HdE* 2 66x@
 __((*
* vvha2**9567:* 	 
  &J'(*V*<Y*G)HICs#o%%c*#-#3#3#5KD%Cu- $6
 %,,33 %  &" .335NC.(  " 6 =
s   46E-N)r   r   r   __doc__r   r^   r-   	SlugFieldr   r.   rX   r/   rd   r   r   rK   r   rT   r   r"   r#   r$   ry   ry      s}     !;  "D ;  %TBD'+''DIK(((6I${$$&H$$L +r#   ry   N)rest_frameworkr   modelsr   r   r   utilsr   django.utils.textr	   	constantsr
   structures.modelsr   django.utilsr   	django.dbr   r   ModelSerializerr   r'   
Serializerr>   r\   rg   ri   rt   ry   CheckInTemplateCreateSerializerr"   r#   r$   <module>r      s    & S S ' % - ' ! ! '
;+F+F 

K$?$? 
:<
(>(> <
|	+[%;%; 	+#k44 #(!<!< (*+[%;%; +.K,B,B .<Lk&<&< L` #B r#   