
    Ƿi~$                     
   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	e          Z G d
 de          Z G d de          Z G d de          Z G d de          Z G 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                   D     e Zd Z ed          Z fdZd Z fdZ xZS )	SlugFieldzString (up to %(max_length)s)c                 ~    |                     dd          |d<   d|vrd|d<    t                      j        |i | d S )N
max_length2   db_indexT)getsuper__init__)selfargskwargs	__class__s      a/srv/django_bis/venv311/lib/python3.11/site-packages/django_extensions/mongodb/fields/__init__.pyr   zSlugField.__init__   sR    %zz,;;|V##!%F:$)&)))))    c                     dS Nr
    r   s    r   get_internal_typezSlugField.get_internal_type       {r   c                 |    dt           j        i}|                    |            t                      j        di |S )N
form_classr   )r   r
   updater   	formfield)r   r   defaultsr   s      r   r!   zSlugField.formfield!   s>     %/2 uww ,,8,,,r   )	__name__
__module____qualname___descriptionr   r   r!   __classcell__r   s   @r   r
   r
      sp        !344K* * * * *  - - - - - - - - -r   r
   c                   F     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
 x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:
    http://www.djangosnippets.org/snippets/690/
    c                    |                     dd           |                     dd           |                    dd           }|t          d          || _        |                    dt                    | _        |                    dt          d	                    | _        |                    d
d          | _         t                      j
        |i | d S )NblankTeditableFpopulate_fromz missing 'populate_from' argumentslugify_function	separator-	overwrite)
setdefaultpop
ValueError_populate_fromr   r0   strr1   r3   r   r   )r   r   r   r/   r   s       r   r   zAutoSlugField.__init__>   s    '4(((*e,,,

?D99 ?@@@"/D &

+=w G GKS::K77$)&)))))r   c                     dt          j        | j                  z  }t          j        d|z  | j        |          }t          j        d|d|d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+|z+$ )reescaper1   sub)r   valuere_seps      r   _slug_stripzAutoSlugField._slug_stripM   sV     bi777uv~t~u==vvffffff5r5AAAr   c                 ,    |                      |          S N)r0   )r   contents     r   slugify_funczAutoSlugField.slugify_funcY   s    $$W---r   c                     t           j        t          t          f          s j        f _        j                             j                  }|s j        r6 fd} j        	                    t          | j                            }d}n                     t           j                            }|                     j                  d         }|                                rJ j        	                    |                     j                  d d                   }t          |          }nd}|j        }|r
|d |         }                     |          }|}j        j                                        }	j        r|	                    j                  }	i }
j        j        D ]$} j        |v r|D ]}t          |d           |
|<   %||
 j        <   |r |	j        di |
rz|} j        |}t1          |          }|r8t1          |          |z   |k    r"|d ||z
           }                     |          }||}||
 j        <   |dz  }|m |	j        di |
z|S )Nc                 V                                             |                     S rC   )rE   get_slug_fields)lookup_valuemodel_instancer   s    r   <lambda>z+AutoSlugField.create_slug.<locals>.<lambda>d   s'    $2C2CDDXDXYgiuDvDv2w2w r      )pk   r   )
isinstancer7   listtuple_meta	get_fieldattnamer3   r1   joinmaprA   getattrsplitisdigitintr   r   _default_managerallrN   excludeunique_togetherfilterlen)r   rJ   add
slug_fieldslug_for_fieldslugnextslug_lenoriginal_slugquerysetr   paramsparamendend_lens   ``             r   create_slugzAutoSlugField.create_slug\   s   $-e}== 	:#'#6"9D#)33DLAA
 	$. 	wwwwwN>&&s>4;N'O'OPPDDD ##GNDL$I$IJJD::dn--b1D||~~ ~**4::dn+E+Ecrc+JKK4yy ( 	#		?D%% "+<@@BB 	>''>+<'==H $*: 	I 	IF|v%%# I IE$+NE4$H$HF5MM#t|  		/(/33F33 		 D NNDD1C#hhG .CII/(::/X//0''--!T33'D#'F4< AID  		/(/33F33 		 r   c           
         |                     t                    }|}|D ]F}	 t          ||          }# t          $ r& t          d                    ||||                    w xY wt          |          rd |            z  S |S )Nzbvalue {} in AutoSlugField's 'populate_from' argument {} returned an error - {} has no attribute {}z%s)rY   r   rX   AttributeErrorformatcallable)r   rJ   rI   lookup_value_pathattrelems         r   rH   zAutoSlugField.get_slug_fields   s    (..z::% 	9 	9D9tT**! 9 9 9$xlD$8 89 9 99
 D>> 	!$$&&= s	   30A#c                 x    t          |                     ||                    }t          || j        |           |S rC   )r8   rn   setattrrU   )r   rJ   rb   r?   s       r   pre_savezAutoSlugField.pre_save   s8    D$$^S99::e444r   c                     dS r   r   r   s    r   r   zAutoSlugField.get_internal_type   r   r   )r#   r$   r%   __doc__r   rA   rE   rn   rH   rx   r   r(   r)   s   @r   r+   r+   '   s         ,* * * * *
B 
B 
B. . .8 8 8t     
      r   r+   c                       e Zd ZdZd Zd ZdS )CreationDateTimeFieldzf
    CreationDateTimeField

    By default, sets editable=False, blank=True, default=datetime.now
    c                 z    |                     dt          j        j                   t          j        | g|R i | d S )Ndefault)r4   datetimenowr   r   )r   r   r   s      r   r   zCreationDateTimeField.__init__   sE    )X%6%:;;;t5d555f55555r   c                     dS Nr   r   r   s    r   r   z'CreationDateTimeField.get_internal_type       r   N)r#   r$   r%   rz   r   r   r   r   r   r|   r|      s<         6 6 6    r   r|   c                       e Zd ZdZd Z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                 n    t           j                                         }t          || j        |           |S rC   )r   r   rw   rU   )r   modelrb   r?   s       r   rx   z"ModificationDateTimeField.pre_save   s/    !%%''t|U+++r   c                     dS r   r   r   s    r   r   z+ModificationDateTimeField.get_internal_type   r   r   N)r#   r$   r%   rz   rx   r   r   r   r   r   r      s<           
    r   r   c                       e Zd ZdS )UUIDVersionErrorN)r#   r$   r%   r   r   r   r   r      s        Dr   r   c                   @     e Zd ZdZd
dZd Z fdZd Z fd	Z x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: http://docs.python.org/lib/module-uuid.html
    NTrO   c                     d|d<   || _         || _        |dk    r||c| _        | _        n|dk    s|dk    r||c| _        | _        t          j        | ||fi | d S )N$   r   rO         )autoversionnode	clock_seq	namespacenamer   r   )	r   verbose_namer   r   r   r   r   r   r   s	            r   r   zUUIDField.__init__   sy    !|	a<<(,i%DIt~~\\W\\(14%DNDIT<@@@@@@@r   c                     t           j        S rC   )r   r#   r   s    r   r   zUUIDField.get_internal_type   s    ##r   c           	      *   | j         rh|j        j        r J d| d|d|d|j        j                    t	                                          ||           d|j        _        | |j        _        d S t	                                          ||           d S )Nz,A model can't have more than one AutoField:  z; have T)primary_keyrS   has_auto_field
auto_fieldr   contribute_to_class)r   clsr   r   s      r   r   zUUIDField.contribute_to_class   s     	3y/  [  [  [txtxtxz}z}z}  @D  @D  @D  FI  FO  FZ  FZ  2[  [  [/GG''T222'+CI$#'CI   GG''T22222r   c                    | j         r| j         dk    rt          j                    S | j         dk    rt          j        | j        | j                  S | j         dk    rt          d          | j         dk    rt          j        | j        | j	                  S | j         dk    rt          j
        | j        | j	                  S t          d| j         z            )N   rO   rL   z UUID version 2 is not supported.r   r   zUUID version %s is not valid.)r   uuiduuid4uuid1r   r   r   uuid3r   r   uuid5r   s    r   create_uuidzUUIDField.create_uuid   s    | 	St|q00:<<\Q:di888\Q"#EFFF\Q:dndi888\Q:dndi888"#BT\#QRRRr   c                 P   | j         r;|r9t          |                                           }t          || j        |           |S t                                          ||          }| j         r9|s7t          |                                           }t          || j        |           |S rC   )r   r8   r   rw   rU   r   rx   )r   rJ   rb   r?   r   s       r   rx   zUUIDField.pre_save  s    9 	= 	=((**++ENDL%888LGG$$^S99Ey = =D,,..//e<<<r   )NNTrO   NNN)
r#   r$   r%   rz   r   r   r   r   rx   r(   r)   s   @r   r   r      s         A A A A$ $ $3 3 3 3 3S S S
 
 
 
 
 
 
 
 
r   r   )rz   r<   r   djangor   django.db.models.constantsr   django.template.defaultfiltersr   django.utils.translationr   r&   mongoengine.fieldsr   r   r   r
   r+   r|   r   	Exceptionr   r   r   r   r   <module>r      s    
			        1 1 1 1 1 1 2 2 2 2 2 2 6 6 6 6 6 6 9 9 9 9 9 9 9 9 - - - - - - - -&E E E E EI E E EP    M        5   $	 	 	 	 	y 	 	 	8 8 8 8 8 8 8 8 8 8r   