ó
¸9—^c           @   s  d  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 d d l m Z m Z d d l Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sà   
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)

iÿÿÿÿN(   t   forms(   t
   LOOKUP_SEP(   t   slugify(   t   ugettext_lazy(   t   StringFieldt   DateTimeFieldt	   SlugFieldc           B   s/   e  Z e d  ƒ Z d „  Z d „  Z d „  Z RS(   s   String (up to %(max_length)s)c         O   sL   | j  d d ƒ | d <d | k r/ t | d <n  t t |  ƒ j | | Ž  d  S(   Nt
   max_lengthi2   t   db_index(   t   gett   Truet   superR   t   __init__(   t   selft   argst   kwargs(    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR      s    c         C   s   d S(   NR   (    (   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   get_internal_type   s    c         K   s3   i t  j d 6} | j | ƒ t t |  ƒ j |   S(   Nt
   form_class(   R    R   t   updateR   t	   formfield(   R   R   t   defaults(    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   "   s    (   t   __name__t
   __module__t   _t   descriptionR   R   R   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR      s   		t   AutoSlugFieldc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sð  
    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         O   s»   | j  d t ƒ | j  d t ƒ | j d d  ƒ } | d  k rM t d ƒ ‚ n	 | |  _ | j d t ƒ |  _ | j d t	 j
 d ƒ ƒ |  _ | j d t ƒ |  _ t t |  ƒ j | | Ž  d  S(	   Nt   blankt   editablet   populate_froms    missing 'populate_from' argumentt   slugify_functiont	   separatort   -t	   overwrite(   t
   setdefaultR
   t   Falset   popt   Nonet
   ValueErrort   _populate_fromR   R   t   sixt   uR   R    R   R   R   (   R   R   R   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   ?   s    	c         C   sO   d t  j |  j ƒ } t  j d | |  j | ƒ } t  j d | | f d | ƒ S(   s	  
        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.
        s   (?:-|%s)s   %s+s	   ^%s+|%s+$t    (   t   ret   escapeR   t   sub(   R   t   valuet   re_sep(    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   _slug_stripN   s    c         C   s   |  j  | ƒ S(   N(   R   (   R   t   content(    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   slugify_funcZ   s    c            sk  t  ˆ j t t f ƒ s* ˆ j f ˆ _ n  ˆ  j j ˆ j ƒ } | sN ˆ j r‡ ‡  ‡ f d †  } ˆ j j	 t
 | ˆ j ƒ ƒ } d } nt ˆ j t ˆ  ˆ j ƒ ƒ } | j ˆ j ƒ d } | j ƒ  rõ ˆ j j	 | j ˆ j ƒ d  ƒ } t | ƒ } n d } | j } | r| |  } n  ˆ j | ƒ } | } ˆ  j j j ƒ  }	 ˆ  j r_|	 j d ˆ  j ƒ }	 n  i  }
 xM ˆ  j j D]? } ˆ j | k rrx' | D] } t ˆ  | d  ƒ |
 | <qŽWqrqrW| |
 ˆ j <x¢ | sÛ|	 j |
   rf| } d ˆ j | f } t | ƒ } | r<t | ƒ | | k r<| | |  } ˆ j | ƒ } n  d | | f } | |
 ˆ j <| d 7} qÅW| S(   Nc            s   ˆ j  ˆ j ˆ  |  ƒ ƒ S(   N(   R1   t   get_slug_fields(   t   lookup_value(   t   model_instanceR   (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   <lambda>e   R)   i   iÿÿÿÿt   pks   %s%si   (   t
   isinstanceR&   t   listt   tuplet   _metat	   get_fieldt   attnameR    R   t   joint   mapR/   t   getattrt   splitt   isdigitt   intR   t	   __class__t   _default_managert   allR6   t   excludet   unique_togetherR$   t   filtert   len(   R   R4   t   addt
   slug_fieldt   slug_for_fieldt   slugt   nextt   slug_lent   original_slugt   querysetR   t   paramst   paramt   endt   end_len(    (   R4   R   sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   create_slug]   sL    	"		!c         C   s†   | j  t ƒ } | } xS | D]K } y t | | ƒ } Wq t k
 rf t d j | | | | ƒ ƒ ‚ q Xq Wt | ƒ r‚ d | ƒ  S| S(   Nsb   value {} in AutoSlugField's 'populate_from' argument {} returned an error - {} has no attribute {}s   %s(   R@   R   R?   t   AttributeErrort   formatt   callable(   R   R4   R3   t   lookup_value_patht   attrt   elem(    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR2   —   s    c         C   s2   t  j |  j | | ƒ ƒ } t | |  j | ƒ | S(   N(   R'   R(   RV   t   setattrR<   (   R   R4   RJ   R-   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   pre_save§   s    c         C   s   d S(   NR   (    (   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   ¬   s    (
   R   R   t   __doc__R   R/   R1   RV   R2   R^   R   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   (   s   				:		t   CreationDateTimeFieldc           B   s    e  Z d  Z d „  Z d „  Z RS(   sf   
    CreationDateTimeField

    By default, sets editable=False, blank=True, default=datetime.now
    c         O   s-   | j  d t j j ƒ t j |  | | Ž d  S(   Nt   default(   R!   t   datetimet   nowR   R   (   R   R   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   ·   s    c         C   s   d S(   NR   (    (   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   »   s    (   R   R   R_   R   R   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR`   °   s   	t   ModificationDateTimeFieldc           B   s    e  Z d  Z d „  Z d „  Z RS(   s§   
    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  j ƒ  } t | |  j | ƒ | S(   N(   Rb   Rc   R]   R<   (   R   t   modelRJ   R-   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR^   È   s    c         C   s   d S(   NR   (    (   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   Í   s    (   R   R   R_   R^   R   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyRd   ¿   s   	t   UUIDVersionErrorc           B   s   e  Z RS(    (   R   R   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyRf   Ñ   s   t	   UUIDFieldc           B   sP   e  Z d  Z d d e d d d d d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(   s  
    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
    i   c   	      K   s†   d | d <| |  _  | |  _ | d k r> | | |  _ |  _ n. | d k sV | d k rl | | |  _ |  _ n  t j |  | | |  d  S(   Ni$   R   i   i   i   (   t   autot   versiont   nodet	   clock_seqt	   namespacet   nameR   R   (	   R   t   verbose_nameRm   Rh   Ri   Rj   Rk   Rl   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   ß   s    
		c         C   s   t  j S(   N(   R   R   (   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR   é   s    c         C   s‰   |  j  rl | j j s8 t d |  | | | j j f ƒ ‚ t t |  ƒ j | | ƒ t | j _ |  | j _ n t t |  ƒ j | | ƒ d  S(   Ns=   A model can't have more than one AutoField: %s %s %s; have %s(	   t   primary_keyR:   t   has_auto_fieldt   AssertionErrort
   auto_fieldR   Rg   t   contribute_to_classR
   (   R   t   clsRm   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyRs   ì   s    	/c         C   sÇ   |  j  s |  j  d k r# t j ƒ  S|  j  d k rH t j |  j |  j ƒ S|  j  d k rf t d ƒ ‚ n] |  j  d k r‹ t j |  j |  j	 ƒ S|  j  d k r° t j
 |  j |  j	 ƒ St d |  j  ƒ ‚ d  S(   Ni   i   i   s    UUID version 2 is not supported.i   i   s   UUID version %s is not valid.(   Ri   t   uuidt   uuid4t   uuid1Rj   Rk   Rf   t   uuid3Rl   Rm   t   uuid5(   R   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   create_uuidõ   s    
c         C   s•   |  j  r; | r; t j |  j ƒ  ƒ } t | |  j | ƒ | St t |  ƒ j | | ƒ } |  j  r‘ | r‘ t j |  j ƒ  ƒ } t | |  j | ƒ n  | S(   N(	   Rh   R'   R(   Rz   R]   R<   R   Rg   R^   (   R   R4   RJ   R-   (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyR^     s    N(
   R   R   R_   R$   R
   R   R   Rs   Rz   R^   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyRg   Õ   s   
				(   R_   R*   R'   Rb   t   djangoR    t   django.db.models.constantsR   t   django.template.defaultfiltersR   t   django.utils.translationR   R   t   mongoengine.fieldsR   R   Ru   R   R   R`   Rd   t	   ExceptionRf   Rg   (    (    (    sK   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/mongodb/fields/__init__.pyt   <module>   s   ˆ