a
    ÆZEg€$  ã                   @   sÀ   d 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 ddlmZmZ ddl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dS )zà
MongoDB model fields emulating Django Extensions' additional model fields

These fields are essentially identical to existing Extensions fields, but South hooks have been removed (since mongo requires no schema migration)

é    N)Úforms)Ú
LOOKUP_SEP)Úslugify)Úgettext_lazy)ÚStringFieldÚDateTimeFieldc                       s8   e Zd ZedƒZ‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )Ú	SlugFieldzString (up to %(max_length)s)c                    s6   |  dd¡|d< d|vr d|d< tƒ j|i |¤Ž d S )NÚ
max_lengthé2   Zdb_indexT)ÚgetÚsuperÚ__init__©ÚselfÚargsÚkwargs©Ú	__class__© úb/var/www/sistema_ama/venv/lib/python3.9/site-packages/django_extensions/mongodb/fields/__init__.pyr      s    zSlugField.__init__c                 C   s   dS ©Nr   r   ©r   r   r   r   Úget_internal_type   s    zSlugField.get_internal_typec                    s&   dt ji}| |¡ tƒ jf i |¤ŽS )NZ
form_class)r   r   Úupdater   Ú	formfield)r   r   Údefaultsr   r   r   r   !   s    

zSlugField.formfield)	Ú__name__Ú
__module__Ú__qualname__Ú_Údescriptionr   r   r   Ú__classcell__r   r   r   r   r      s   r   c                       sP   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
‡  ZS )ÚAutoSlugFieldañ  
    AutoSlugField, adapted for MongoDB

    By default, sets editable=False, blank=True.

    Required arguments:

    populate_from
        Specifies which field or list of fields the slug is populated from.

    Optional arguments:

    separator
        Defines the used separator (default: '-')

    overwrite
        If set to True, overwrites the slug on every save (default: False)

    Inspired by SmileyChris' Unique Slugify snippet:
    https://www.djangosnippets.org/snippets/690/
    c                    s€   |  dd¡ |  dd¡ | dd ¡}|d u r6tdƒ‚n|| _| dt¡| _| dtd	ƒ¡| _| d
d¡| _t	ƒ j
|i |¤Ž d S )NZblankTÚeditableFÚpopulate_fromz missing 'populate_from' argumentÚslugify_functionÚ	separatorú-Ú	overwrite)Ú
setdefaultÚpopÚ
ValueErrorÚ_populate_fromr   r%   Ústrr&   r(   r   r   )r   r   r   r$   r   r   r   r   >   s    
zAutoSlugField.__init__c                 C   s:   dt  | j¡ }t  d| | j|¡}t  d||f d|¡S )a	  
        Clean up a slug by removing slug separator characters that occur at
        the beginning or end of a slug.

        If an alternate separator is used, it will also replace any instances
        of the default '-' separator with the new separator.
        z(?:-|%s)z%s+z	^%s+|%s+$Ú )ÚreÚescaper&   Úsub)r   ÚvalueZre_sepr   r   r   Ú_slug_stripM   s    zAutoSlugField._slug_stripc                 C   s
   |   |¡S ©N)r%   )r   Úcontentr   r   r   Úslugify_funcY   s    zAutoSlugField.slugify_funcc                    s¾  t ˆjttfƒsˆjfˆ_ˆ j ˆj¡}|s2ˆjrZ‡ ‡fdd„}ˆj 	t
|ˆjƒ¡}d}nTˆ tˆ ˆjƒ¡}| ˆj¡d }| ¡ rªˆj 	| ˆj¡d d… ¡}t|ƒ}nd}|j}|rÄ|d |… }ˆ |¡}|}ˆ jj ¡ }	ˆ jrò|	jˆ jd�}	i }
ˆ jjD ]*}ˆj|v rþ|D ]}tˆ |d ƒ|
|< �qqþ||
ˆj< |�rL|	jf i |
¤Ž�rº|}dˆj|f }t|ƒ}|�r˜t|ƒ| |k�r˜|d || … }ˆ |¡}d||f }||
ˆj< |d7 }�q4|S )Nc                    s   ˆ  ˆ ˆ | ¡¡S r4   )r6   Úget_slug_fields)Úlookup_value©Úmodel_instancer   r   r   Ú<lambda>d   ó    z+AutoSlugField.create_slug.<locals>.<lambda>é   éÿÿÿÿ)Úpkz%s%sé   )Ú
isinstancer,   ÚlistÚtupleÚ_metaÚ	get_fieldÚattnamer(   r&   ÚjoinÚmapr3   ÚgetattrÚsplitÚisdigitÚintr	   r   Z_default_managerÚallr?   ÚexcludeZunique_togetherÚfilterÚlen)r   r:   ÚaddZ
slug_fieldZslug_for_fieldZslugÚnextZslug_lenZoriginal_slugZquerysetr   ÚparamsÚparamÚendZend_lenr   r9   r   Úcreate_slug\   sL    







zAutoSlugField.create_slugc                 C   sd   |  t¡}|}|D ]:}zt||ƒ}W q tyJ   td ||||¡ƒ‚Y q0 qt|ƒr`d|ƒ  S |S )Nzbvalue {} in AutoSlugField's 'populate_from' argument {} returned an error - {} has no attribute {}z%s)rJ   r   rI   ÚAttributeErrorÚformatÚcallable)r   r:   r8   Zlookup_value_pathÚattrÚelemr   r   r   r7   –   s    
ÿÿ
zAutoSlugField.get_slug_fieldsc                 C   s"   t |  ||¡ƒ}t|| j|ƒ |S r4   )r-   rV   ÚsetattrrF   ©r   r:   rQ   r2   r   r   r   Úpre_save¦   s    zAutoSlugField.pre_savec                 C   s   dS r   r   r   r   r   r   r   «   s    zAutoSlugField.get_internal_type)r   r   r   Ú__doc__r   r3   r6   rV   r7   r^   r   r!   r   r   r   r   r"   '   s   :r"   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCreationDateTimeFieldzf
    CreationDateTimeField

    By default, sets editable=False, blank=True, default=datetime.now
    c                 O   s,   |  dtjj¡ tj| g|¢R i |¤Ž d S )NÚdefault)r)   ÚdatetimeÚnowr   r   r   r   r   r   r   ¶   s    zCreationDateTimeField.__init__c                 C   s   dS ©Nr   r   r   r   r   r   r   º   s    z'CreationDateTimeField.get_internal_typeN)r   r   r   r_   r   r   r   r   r   r   r`   ¯   s   r`   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚModificationDateTimeFieldz§
    ModificationDateTimeField

    By default, sets editable=False, blank=True, default=datetime.now

    Sets value to datetime.now() on each save of the model.
    c                 C   s   t j  ¡ }t|| j|ƒ |S r4   )rb   rc   r\   rF   )r   ÚmodelrQ   r2   r   r   r   r^   Ç   s    
z"ModificationDateTimeField.pre_savec                 C   s   dS rd   r   r   r   r   r   r   Ì   s    z+ModificationDateTimeField.get_internal_typeN)r   r   r   r_   r^   r   r   r   r   r   re   ¾   s   re   c                   @   s   e Zd ZdS )ÚUUIDVersionErrorN)r   r   r   r   r   r   r   rg   Ð   s   rg   c                       sF   e Zd ZdZddd„Zdd„ Z‡ fd	d
„Zdd„ Z‡ fdd„Z‡  Z	S )Ú	UUIDFielda  
    UUIDField

    By default uses UUID version 1 (generate from host ID, sequence number and current time)

    The field support all uuid versions which are natively supported by the uuid python module.
    For more information see: https://docs.python.org/lib/module-uuid.html
    NTr@   c           	      K   sd   d|d< || _ || _|dkr,|| | _| _n|dks<|dkrJ|| | _| _tj| ||fi |¤Ž d S )Né$   r	   r@   é   é   )ÚautoÚversionÚnodeÚ	clock_seqÚ	namespaceÚnamer   r   )	r   Zverbose_namerq   rl   rm   rn   ro   rp   r   r   r   r   r   Þ   s    zUUIDField.__init__c                 C   s   t jS r4   )r   r   r   r   r   r   r   è   s    zUUIDField.get_internal_typec                    sX   | j rF|jjr&J d| |||jjf ƒ‚tƒ  ||¡ d|j_| |j_ntƒ  ||¡ d S )Nz=A model can't have more than one AutoField: %s %s %s; have %sT)Zprimary_keyrD   Zhas_auto_fieldZ
auto_fieldr   Úcontribute_to_class)r   Úclsrq   r   r   r   rr   ë   s     
zUUIDField.contribute_to_classc                 C   sŒ   | j r| j dkrt ¡ S | j dkr2t | j| j¡S | j dkrFtdƒ‚nB| j dkr`t | j| j	¡S | j dkrzt 
| j| j	¡S td| j  ƒ‚d S )Né   r@   r=   z UUID version 2 is not supported.rj   rk   zUUID version %s is not valid.)rm   ÚuuidÚuuid4Úuuid1rn   ro   rg   Úuuid3rp   rq   Úuuid5r   r   r   r   Úcreate_uuidô   s    




zUUIDField.create_uuidc                    s^   | j r(|r(t|  ¡ ƒ}t|| j|ƒ |S tƒ  ||¡}| j rZ|sZt|  ¡ ƒ}t|| j|ƒ |S r4   )rl   r-   rz   r\   rF   r   r^   r]   r   r   r   r^     s    

zUUIDField.pre_save)NNTr@   NNN)
r   r   r   r_   r   r   rr   rz   r^   r!   r   r   r   r   rh   Ô   s   	

	rh   )r_   r/   rb   Zdjangor   Zdjango.db.models.constantsr   Zdjango.template.defaultfiltersr   Zdjango.utils.translationr   r   Zmongoengine.fieldsr   r   ru   r   r"   r`   re   Ú	Exceptionrg   rh   r   r   r   r   Ú<module>   s    	