
    g$                         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y)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                   >     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                 `    |j                  dd      |d<   d|vrd|d<   t        |   |i | y )N
max_length2   db_indexT)getsuper__init__)selfargskwargs	__class__s      f/var/www/django_project/virt/lib/python3.12/site-packages/django_extensions/mongodb/fields/__init__.pyr   zSlugField.__init__   s<    %zz,;|V#!%F:$)&)    c                      yNr
    r   s    r   get_internal_typezSlugField.get_internal_type       r   c                 h    dt         j                  i}|j                  |       t        |   di |S )N
form_classr   )r   r
   updater   	formfield)r   r   defaultsr   s      r   r!   zSlugField.formfield!   s0     %//2w ,8,,r   )	__name__
__module____qualname___descriptionr   r   r!   __classcell__r   s   @r   r
   r
      s"    3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:
    https://www.djangosnippets.org/snippets/690/
    c                 \   |j                  dd       |j                  dd       |j                  dd       }|t        d      || _        |j                  dt              | _        |j                  dt        d	            | _        |j                  d
d      | _        t        | (  |i | y )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 ?@@"/D &

+=w GKS:K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   sM     bii77uv~t~~u=vvff5r5AAr   c                 $    | j                  |      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                   j                        }|s j                  r8 fd} j                  j                  t        | j                              }d}n j                  t         j                              }|j                   j                        d   }|j                         rC j                  j                  |j                   j                        d d       }t        |      }nd}|j                   }|r|d | } j                  |      }|}j"                  j$                  j'                         }	j(                  r|	j+                  j(                        }	i }
j                  j,                  D ](  } j                  |v s|D ]  }t        |d       |
|<    * ||
 j                  <   |r |	j.                  di |
ry|} j                  |}t1        |      }|r*t1        |      |z   |kD  r|d ||z
   } j                  |      }||}||
 j                  <   |dz  }|sf |	j.                  di |
ry|S )Nc                 F    j                  j                  |             S rC   )rE   get_slug_fields)lookup_valuemodel_instancer   s    r   <lambda>z+AutoSlugField.create_slug.<locals>.<lambda>d   s!    $2C2CDDXDXYgiuDv2w 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#6"9D#))33DLLA
$..wN>>&&s>4;N;N'OPDD ##GNDLL$IJD::dnn-b1D||~~~**4::dnn+Ecr+JK4y ((	?D% "++<<@@B''>+<+<'=H $**:: 	IF||v%# IE$+NE4$HF5MI	I  $t|| /(//3F3 D NND1C#hGCI/(:/X/0''-!3'D#'F4<< AID /(//3F3 r   c           
          |j                  t              }|}|D ]  }	 t        ||      } t        |      r
d |       z  S |S # t        $ r t        dj	                  ||||            w xY w)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:% 	9D9tT*	9 D>$&=  " 9$xlD$89 99s   A'A*c                 j    t        | j                  ||            }t        || j                  |       |S rC   )r8   rn   setattrrU   )r   rJ   rb   r?   s       r   pre_savezAutoSlugField.pre_save   s.    D$$^S9:e4r   c                      y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.8t 
r   r+   c                       e Zd ZdZd Zd Zy)CreationDateTimeFieldzf
    CreationDateTimeField

    By default, sets editable=False, blank=True, default=datetime.now
    c                     |j                  dt        j                  j                         t        j                  | g|i | y )Ndefault)r4   datetimenowr   r   )r   r   r   s      r   r   zCreationDateTimeField.__init__   s6    )X%6%6%:%:;t5d5f5r   c                      y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r   r|   c                       e Zd ZdZd Zd Zy)ModificationDateTimeFieldz
    ModificationDateTimeField

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

    Sets value to datetime.now() on each save of the model.
    c                 p    t         j                   j                         }t        || j                  |       |S rC   )r   r   rw   rU   )r   modelrb   r?   s       r   rx   z"ModificationDateTimeField.pre_save   s+    !!%%'t||U+r   c                      y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y)UUIDVersionErrorN)r#   r$   r%   r   r   r   r   r      s    r   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: https://docs.python.org/lib/module-uuid.html
    c                     d|d<   || _         || _        |dk(  r||c| _        | _        n|dk(  s|dk(  r||c| _        | _        t        j                  | ||fi | y )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__   se    !|	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                   rp|j                  j                  r'J d| d|d|d|j                  j                         t        |   ||       d|j                  _        | |j                  _        y t        |   ||       y )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    yy//  [txz}  @D  FI  FO  FO  FZ  FZ  2[  [/G'T2'+CII$#'CII G'T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    ||t||q0::<\\Q::dii88\\Q"#EFF\\Q::dnndii88\\Q::dnndii88"#BT\\#QRRr   c                 $   | j                   r4|r2t        | j                               }t        || j                  |       |S t
        |   ||      }| j                   r2|s0t        | j                               }t        || j                  |       |S rC   )r   r8   r   rw   rU   r   rx   )r   rJ   rb   r?   r   s       r   rx   zUUIDField.pre_save  sv    99((*+ENDLL%8LG$^S9EyyD,,./e<r   )NNTrO   NNN)
r#   r$   r%   rz   r   r   r   r   rx   r(   r)   s   @r   r   r      s(    A$3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      sp    
   1 2 6 9 - -&EI EPM  5 $	y 	8 8r   