o
    óTiæ  ã                   @   sv   d dl mZ d dlmZ d dlmZmZmZmZ	 G dd„ dƒZ
G dd„ de
ƒZG dd	„ d	eeƒZG d
d„ deeƒZdS )é    )ÚOrderedDict)Úimport_string)ÚPrimaryKeyRelatedFieldÚSlugRelatedFieldÚMANY_RELATION_KWARGSÚManyRelatedFieldc                       sL   e Zd ZdZG dd„ deƒZ‡ fdd„Ze‡ fdd„ƒZ‡ fdd	„Z	‡  Z
S )
ÚReadSourceMixinzæ
    This mixin override get_attribute method and set read_source attribute
    to source attribute if read_source attribute setted. For the purpose of
    not want to effect of write operation, we don't override bind method.
    c                       s   e Zd Z‡ fdd„Z‡  ZS )z ReadSourceMixin.ManyRelatedFieldc                    s.   | j jr| j j| _|  | j| j¡ tƒ  |¡S ©N)Úchild_relationÚread_sourceÚsourceÚbindÚ
field_nameÚparentÚsuperÚget_attribute©ÚselfÚinstance©Ú	__class__© úZ/var/www/html/evchargy.com/venv/lib/python3.10/site-packages/drf_extra_fields/relations.pyr      s   
z.ReadSourceMixin.ManyRelatedField.get_attribute)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r      s    r   c                    s$   |  dd ¡| _tƒ jdi |¤Ž d S )Nr   r   )Úpopr   r   Ú__init__©r   Úkwargsr   r   r   r      s   zReadSourceMixin.__init__c                    s^   |  dd ¡stƒ j|i |¤ŽS d| |i |¤Ži}|D ]}|tv r&|| ||< q| jdi |¤ŽS )Nr   r
   r   )Úgetr   Ú	many_initr   r   )ÚclsÚargsr    Zlist_kwargsÚkeyr   r   r   r"      s   €zReadSourceMixin.many_initc                    s*   | j r| j | _|  | j| j¡ tƒ  |¡S r	   )r   r   r   r   r   r   r   r   r   r   r   r   )   s   zReadSourceMixin.get_attribute)r   r   r   Ú__doc__ÚDRFManyRelatedFieldr   r   Úclassmethodr"   r   r   r   r   r   r   r   
   s    r   c                       s6   e Zd Z‡ fdd„Zdd„ Zd
dd„Zdd	„ Z‡  ZS )ÚPresentableRelatedFieldMixinc                    sN   |  dd ¡| _|  dtƒ ¡| _| jd usJ | jjd ƒ‚tƒ jdi |¤Ž d S )NÚpresentation_serializerÚpresentation_serializer_kwargsz2 must provide a `presentation_serializer` argumentr   )r   r*   Údictr+   r   r   r   r   r   r   r   r   r   2   s   ÿÿÿz%PresentableRelatedFieldMixin.__init__c                 C   s   dS )aó  
        Instead of sending pk only object, return full object. The object already retrieved from db by drf.
        This doesn't cause an extra query.
        It even might save from making an extra query on serializer.to_representation method.
        Related source codes:
        - https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/relations.py#L41
        - https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/relations.py#L132
        Fr   ©r   r   r   r   Úuse_pk_only_optimization=   s   
z5PresentableRelatedFieldMixin.use_pk_only_optimizationNc                    s>   ˆ   ¡ }|d u r
i S |d ur|d |… }t‡ fdd„|D ƒƒS )Nc                    s   g | ]
}|j ˆ  |¡f‘qS r   )ÚpkZdisplay_value)Ú.0Úitemr-   r   r   Ú
<listcomp>S   s    z<PresentableRelatedFieldMixin.get_choices.<locals>.<listcomp>)Zget_querysetr   )r   ÚcutoffZquerysetr   r-   r   Úget_choicesI   s   z(PresentableRelatedFieldMixin.get_choicesc                 C   s4   t | jtƒrt| jƒ| _| j|fd| ji| j¤ŽjS )NÚcontext)Ú
isinstancer*   Ústrr   r5   r+   Údata)r   r8   r   r   r   Úto_representationU   s   ÿÿÿþz.PresentableRelatedFieldMixin.to_representationr	   )r   r   r   r   r.   r4   r9   r   r   r   r   r   r)   1   s
    
r)   c                   @   ó   e Zd ZdZdS )Ú!PresentablePrimaryKeyRelatedFieldzk
    Override PrimaryKeyRelatedField to represent serializer data instead of a pk field of the object.
    N©r   r   r   r&   r   r   r   r   r;   ^   s    r;   c                   @   r:   )ÚPresentableSlugRelatedFieldzg
    Override SlugRelatedField to represent serializer data instead of a slug field of the object.
    Nr<   r   r   r   r   r=   h   s    r=   N)Úcollectionsr   Zdjango.utils.module_loadingr   Zrest_framework.relationsr   r   r   r   r'   r   r)   r;   r=   r   r   r   r   Ú<module>   s    '
-ÿ
