
    i                     R    d dl mZ ddlmZ d dlmZ  G d dej                        Zy)    )serializers   )BlockedPeriod)Bookingc                   &    e Zd Z G d d      Zd Zy)BlockedPeriodSerializerc                        e Zd ZeZg dZg dZy)BlockedPeriodSerializer.Meta)id	structureproperty_typeproperty
start_dateend_datereasonnotes
created_by
updated_by
created_at
updated_at)r   r   r   r   r   N)__name__
__module____qualname__r   modelfieldsread_only_fields     $/backend/availability/serializers.pyMetar
      s    
 Zr   r    c                 N   |j                  d      }|j                  d      }|j                  d      }|st        j                  d      ||k\  rt        j                  d      t        j                  j                  |||      }| j                  r&|j                  | j                  j                        }|j                         rt        j                  d      t        j                  j                  |||	      j                         }|rt        j                  d
      |S )Nr   r   r   zProperty is required.z"End date must be after start date.)r   start_date__ltend_date__gt)r   z2These dates are already blocked for this property.)r   check_in_date__ltcheck_out_date__gtzBCannot block these dates because the property has active bookings.)getr   ValidationErrorr   objectsfilterinstanceexcluder   existsr   )selfdatastartendproperty_objoverlapping_blockoverlapping_bookings          r   validatez BlockedPeriodSerializer.validate   s   &hhz"xx
+--.EFFC<--.RSS *1188! 9 
 == 1 9 9T]]=M=M 9 N##%--D 
 &oo44!!$ 5 
 &(	 	 --T  r   N)r   r   r   r    r4   r   r   r   r   r      s    Z Z$'r   r   N)rest_frameworkr   modelsr   bookings.modelsr   ModelSerializerr   r   r   r   <module>r9      s     & ! #:k99 :r   