o
    Ti*                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z d dlmZmZmZmZmZmZ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mZmZ d
Z G dd dZ!G dd de!eZ"G dd de"Z#G dd de!eZ$G dd deZ%G dd de%Z&G dd de%Z'G dd de%Z(G dd de%Z)G dd de%Z*ej+re)ej,ej+j)< e*ej,ej+j*< e&ej,ej+j&< e(ej,ej+j(< e-ej+dre'ej,ej+j'< G dd  d eZ.dS )!    N)ValidationError)SimpleUploadedFile)gettext_lazy)		DateFieldDateTimeFieldDecimalField	DictField
EmailField	FileField
FloatField
ImageFieldIntegerField)ModelSerializer)html)compat)	DateRangeDateTimeTZRangeNumericRangezapplication/octet-streamc                       sv   e Zd Zddg di fZedd Zedd Zedd	 Z fd
dZ fddZ	dd Z
dd Z fddZ  ZS )Base64FieldMixinN  c                 C      t NNotImplementedErrorselfr   r   W/var/www/html/evchargy.com/venv/lib/python3.10/site-packages/drf_extra_fields/fields.pyALLOWED_TYPES!      zBase64FieldMixin.ALLOWED_TYPESc                 C   r   r   r   r   r   r   r   INVALID_FILE_MESSAGE%   r   z%Base64FieldMixin.INVALID_FILE_MESSAGEc                 C   r   r   r   r   r   r   r   INVALID_TYPE_MESSAGE)   r   z%Base64FieldMixin.INVALID_TYPE_MESSAGEc                    s2   | dd| _| dd| _t j|i | d S )Ntrust_provided_content_typeFrepresent_in_base64)popr"   r#   super__init__)r   argskwargs	__class__r   r   r&   -   s   zBase64FieldMixin.__init__c           	   
      s   || j v rd S t|trbd }d|v r"|d\}}| jr"|dd}zt|}W n tt	j
tfy9   t| jw | |}| ||}|| jvrOt| j|d | }t|||d}t |S ttdt| )Nz;base64,zdata:r   .)namecontentcontent_typez,Invalid type. This is not an base64 string: )EMPTY_VALUES
isinstancestrsplitr"   replacebase64	b64decode	TypeErrorbinasciiError
ValueErrorr   r    get_file_nameget_file_extensionr   r!   r   r%   to_internal_value_type)	r   Zbase64_dataZfile_mime_typeheaderdecoded_file	file_nameZfile_extensionZcomplete_file_namedatar)   r   r   r<   2   s2   





z"Base64FieldMixin.to_internal_valuec                 C   r   r   r   r   filenamer@   r   r   r   r;   Z   s   z#Base64FieldMixin.get_file_extensionc                 C   s   t t S r   )r1   uuiduuid4)r   r@   r   r   r   r:   ]   s   zBase64FieldMixin.get_file_namec                    st   | j r4|sdS z!| }t|  W  d    W S 1 s!w   Y  W d S  ty3   tdw t 	|S )Nr   zError encoding file)
r#   openr4   	b64encodereaddecode	ExceptionOSErrorr%   to_representation)r   filefr)   r   r   rM   `   s   
(z"Base64FieldMixin.to_representation)__name__
__module____qualname__r/   propertyr   r    r!   r&   r<   r;   r:   rM   __classcell__r   r   r)   r   r      s    


(r   c                   @   s,   e Zd ZdZdZedZedZdd ZdS )Base64ImageFieldz
    A django-rest-framework field for handling image-uploads through raw post data.
    It uses base64 for en-/decoding the contents of the file.
    )jpegjpgZpngZgifZwebpzPlease upload a valid image.z-The type of the image couldn't be determined.c              	   C   sj   t |}|d u r-zddlm} |t|}W n ttfy'   t	| j
w |j }|dkr3dS |S )Nr   )ImagerV   rW   )filetypeguess_extensionZPILrX   rG   ioBytesIOImportErrorrL   r   r    formatlower)r   rD   r@   	extensionrX   imager   r   r   r;      s   


z#Base64ImageField.get_file_extensionN)	rP   rQ   rR   __doc__r   r=   r    r!   r;   r   r   r   r   rU   r   s    rU   c                   @   s   e Zd ZdZdd ZdS )HybridImageFieldz
    A django-rest-framework field for handling image-uploads through
    raw post data, with a fallback to multipart form data.
    c                 C   s.   zt | |W S  ty   t| | Y S w )z
        Try Base64Field first, and then try the ImageField
        ``to_internal_value``, MRO doesn't work here because
        Base64FieldMixin throws before ImageField can run.
        )r   r<   r   r   r   rB   r   r   r   r<      s
   z"HybridImageField.to_internal_valueN)rP   rQ   rR   rb   r<   r   r   r   r   rc      s    rc   c                   @   s4   e Zd ZdZedd ZedZedZdd Z	dS )	Base64FileFieldz
    A django-rest-framework field for handling file-uploads through raw post data.
    It uses base64 for en-/decoding the contents of the file.
    c                 C      t d)NzList allowed file extensionsr   r   r   r   r   r      s   zBase64FileField.ALLOWED_TYPESzPlease upload a valid file.z,The type of the file couldn't be determined.c                 C   rf   )Nz8Implement file validation and return matching extension.r   rC   r   r   r   r;      s   z"Base64FileField.get_file_extensionN)
rP   rQ   rR   rb   rS   r   r=   r    r!   r;   r   r   r   r   re      s    
re   c                       s^   e Zd ZdZeejZeededd  fddZ	dd Z
d	d
 Z fddZ  ZS )
RangeFieldNz$Extra content not allowed "{extra}".z<The start of the range must not exceed the end of the range.)too_much_contentbound_orderingc                    s\   t jd u rJ dj| jjd|di | _| jdi | j| j| _	t
 jdi | d S )NFz'psycopg' is required to use {name}. Please install the 'psycopg2' (or 'psycopg' if you are using Django>=4.2) library from 'pip')r,   child_attrsr   )r   postgres_fieldsr^   r*   rP   r$   rj   child_classdefault_child_attrschildr%   r&   )r   r(   r)   r   r   r&      s   
zRangeField.__init__c              	   C   sJ  t |r
t |}t|ts| jdt|jd t| dr,| j	s,t
|dkr,| d tt|h d }|rE| jddtt|d	 i }d
D ]}z|| }W n	 tyZ   Y qIw | j||t|< qI|d|d}}|dur|dur||kr| d dD ]}z|| }W n	 ty   Y qw ||t|< q| jdi |S )zB
        Range instances <- Dicts of primitive datatypes.
        Z
not_a_dict)Z
input_typeallow_emptyr   empty>   boundsupperrp   r_   rh   z, )extra)r_   rr   r_   rr   Nri   )rq   rp   r   )r   Zis_html_inputZparse_html_dictr0   dictZfailr>   rP   hasattrro   lenlistsetjoinmapr1   KeyErrorrn   Zrun_validationget
range_type)r   rB   Zextra_contentZvalidated_dictkeyvaluer_   rr   r   r   r   r<      s8   




zRangeField.to_internal_valuec                 C   s   t |tr|s	|S |d}|d}|d}n|jr ddiS |j}|j}|j}|dur3| j|nd|durA| j||dS d|dS )zB
        Range instances -> dicts of primitive datatypes.
        r_   rr   rq   rp   TN)r_   rr   rq   )	r0   rt   r|   Zisemptyr_   rr   Z_boundsrn   rM   )r   r   r_   rr   rq   r   r   r   rM      s$   


zRangeField.to_representationc                    s   t   }| |S r   )r%   get_initialrM   )r   initialr)   r   r   r     s   

zRangeField.get_initial)rP   rQ   rR   r}   rt   r   Zdefault_error_messagesupdater=   r&   r<   rM   r   rT   r   r   r)   r   rg      s    

(rg   c                   @      e Zd ZeZi ZeZdS )IntegerRangeFieldN)rP   rQ   rR   r   rl   rm   r   r}   r   r   r   r   r   
      r   c                   @   r   )FloatRangeFieldN)rP   rQ   rR   r   rl   rm   r   r}   r   r   r   r   r     r   r   c                   @   s   e Zd ZeZdddZeZdS )DecimalRangeFieldN)Z
max_digitsZdecimal_places)rP   rQ   rR   r   rl   rm   r   r}   r   r   r   r   r     s    
r   c                   @   r   )DateTimeRangeFieldN)rP   rQ   rR   r   rl   rm   r   r}   r   r   r   r   r     r   r   c                   @   r   )DateRangeFieldN)rP   rQ   rR   r   rl   rm   r   r}   r   r   r   r   r   "  r   r   c                       s,   e Zd ZdZ fddZ fddZ  ZS )LowercaseEmailFieldz
    An enhancement over django-rest-framework's EmailField to allow
    case-insensitive serialization and deserialization of e-mail addresses.
    c                       t  |}| S r   )r%   r<   r_   rd   r)   r   r   r<   8     z%LowercaseEmailField.to_internal_valuec                    r   r   )r%   rM   r_   )r   r   r)   r   r   rM   <  r   z%LowercaseEmailField.to_representation)rP   rQ   rR   rb   r<   rM   rT   r   r   r)   r   r   3  s    r   )/r4   r7   r[   rE   rY   Zdjango.core.exceptionsr   Zdjango.core.files.uploadedfiler   Zdjango.utils.translationr   r=   Zrest_framework.fieldsr   r   r   r   r	   r
   r   r   r   Zrest_framework.serializersr   Zrest_framework.utilsr   Zdrf_extra_fieldsr   Zdrf_extra_fields.compatr   r   r   ZDEFAULT_CONTENT_TYPEr   rU   rc   re   rg   r   r   r   r   r   rk   Zserializer_field_mappingru   r   r   r   r   r   <module>   s@    ,TV