B
    b*7                 @   s  d dl mZ d dlZd dlZd dlZd dlZyd dlZW n ek
rP   dZY nX ye W n e	k
rv   dd ZY nX d dl
mZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZ yd dlZW n ek
r   d dlZY nX d dlmZ d
d ZeZG dd deZdS )    )
generatorsNc             C   s   x| D ]}|rdS qW dS )NTF )iterableelementr   r   -G:\easytimepro\master/mysite/base\emitters.pyany   s    
r   )QuerySet)Model	permalink)	smart_str)reverseNoReverseMatch)HttpResponsec             C   s8   y(t d}t d| |}|d| iS    | S d S )N zvalue|value)templateZParserZFilterExpressionZresolve)r   filterpZfer   r   r   	do_filter1   s    
r   c            
   @   s   e Zd ZdZi Zedddddddd	d
g	Zd!ddZdd Zdd Z	dd Z
dd Zd"ddZedd Zed#ddZedd Zd S )$Emittera  
    Super emitter. All other emitters should subclass
    this one. It has the `construct` method which
    conveniently returns a serialized `dict`. This is
    usually the only method you want to use in your
    emitter. See below for examples.

    `RESERVED_FIELDS` was introduced when better resource
    method detection came, and we accidentially caught these
    as the methods on the handler. Issue58 says that's no good.
    readupdatecreatedeletemodel	anonymousZallowed_methodsfieldsexcluder   Tc             C   s<   || _ || _|| _|| _|| _|| _|| _t| jtr8 d S )N)	admin
typemapperdatahandlerr   headr   
isinstance	Exception)selfr   Zpayloadr   r!   r   r"   r   r   r   r   __init__O   s    zEmitter.__init__c             C   s   |si S d S )Nr   )r%   r!   r   r   r   r   method_fields[   s    zEmitter.method_fieldsc                s   d fdd	  fdddfdd	dfdd		d fd
d	d fdd	 fdd fdd j jS )z
        Recursively serialize a lot of types, and
        in cases where it doesn't recognize the type,
        it will fall back to Django's `smart_unicode`.

        Returns `dict`.
        r   c                s  d}t | tr| |d}nt | ttfr4| }nt | trH| }nt | tjr^t| }nt | trv| |d}nt	
| rt	| d s |  }ndt| dr| j}t	|rtt	|d dkr | }n(t| dr |  }nt| dd}|S )	zF
            Dispatch, all types are routed through here.
            N)r   r   __emittable__   z/<django.db.models.fields.related.RelatedManagerT)Zstrings_only)r#   r   tuplelistdictdecimalZDecimalstrr	   inspectZ
isfunction
getargspechasattrr(   Zismethodlenrepr
startswithallr   )thingr   retf)_any_dict_list_model_qsr   r   r9   h   s,    







 zEmitter.construct.<locals>._anyc                sl   t | |jd }d|j|f }tds.i _j|d}|dkr^tt | |j}|j|<  ||dS )z'
            Foreign keys.
            _idz%s_%s	_fk_cacheN)idverbose)getattrnamer1   r?   getr   )r    fieldr@   Zid_keyr   )r9   r%   r   r   _fk   s    

zEmitter.construct.<locals>._fkc                s    fdd|   D S )z'
            Foreign keys.
            c                s   g | ]} |qS r   r   ).0m)r<   r   r   r   
<listcomp>   s    z7Emitter.construct.<locals>._related.<locals>.<listcomp>)iterator)r    r   )r<   )r   r   _related   s    z#Emitter.construct.<locals>._relatedc                s     fddt | |j D S )z>
            Many to many (re-route to `_model`.)
            c                s   g | ]} |qS r   r   )rG   rH   )r<   r   r   r   rI      s    z3Emitter.construct.<locals>._m2m.<locals>.<listcomp>)rB   rC   rJ   )r    rE   r   )r<   )r   r   _m2m   s    zEmitter.construct.<locals>._m2mc          
      sB  i }	 t 	j}d}|s$r fdd}sވ	 t 	j}t|j}t|j|}d|krld}|stdd  jjD }xZ|D ]L}	t|	t	r|
|	 qt|	tjrx$| D ]}
|	|
r|
|
 qW qW n}tdd t|D }| }	||x jjD ]}|jrtfd	d|j|jgD s|js|j|kr||jd
rt||||j ||j< n@d|j }t |rt | ||j< n||||j< ||j n0|jd
d |kr |||j< ||j qW xN jjD ]B}|jr|jkr|j|kr |||j< ||j qW x|D ]z}d|kr|dd\}}
t |d
}|r||
f|
 ||< nd||< qbt|tt fr@|\}t |d
}|rt|dr|||< nBt!|r0t"t#$|d dkr>| ||< n|||< n|kr`|  ||< n|t |d
}|rt!|rt"t#$|d dkr| ||< n|||< n*t|p	j%|d
}|rb| ||< qbW ntx( jjD ]}t |j||j< qW t& j'|  fddt& D }x |D ]}t |||< q<W 	 t 	jrڈ	 t 	j}t|dr|( \yt)fdd |d< W n$ t*k
r } zW d
d
}~X Y nX t drd|kry + |d< W n   Y nX t dr>|r>y , |d< W n   Y nX |S )z{
            Models. Will respect the `fields` and/or
            `exclude` on the handler (see `typemapper`.)
            Fc                s   t  | jS )N)rB   attname)r8   )r    r   r   <lambda>       z3Emitter.construct.<locals>._model.<locals>.<lambda>Zabsolute_uriTc             S   s   g | ]}|j d ddqS )r>   r   r)   )rM   replace)rG   r8   r   r   r   rI      s   z5Emitter.construct.<locals>._model.<locals>.<listcomp>c             S   sD   g | ]<}d |kr8|d| d  || d d d fp>|dfqS )|Nr)   r   )index)rG   r8   r   r   r   rI      s    c                s   g | ]}| kqS r   r   )rG   r   )
met_fieldsr   r   rI      s    Nzget_%s_display.r)   r   r5   r   c                s   g | ]}| kr|qS r   r   )rG   k)r   r   r   rI     s    resource_uric                  s    fS )Nr   r   )r   url_idr   r   rN   "  rO   get_api_urlget_absolute_url)-in_typemappertyper   setr   r   
differenceZ_metar#   Z
basestringdiscardreZ_pattern_typecopymatchr,   r+   keysr'   Zlocal_fieldsZ	serializer   rM   rC   ZrelrD   r   r1   rB   removeZmany_to_manysplitr*   callabler2   r/   r0   r!   dir	__class__rW   reverserr   rY   rZ   )r    r   r7   r!   Zget_absolute_urivZmappedZ
get_fieldsZexclude_fieldsr   rE   Zget_fields_with_filterr8   Zget_dispZmfZmaybe_fieldr   objinstmaybeZ	handler_fZadd_onsrV   e)r9   rF   rL   r<   rK   r%   )r    r   rS   rX   r   r<      s    




( 





z!Emitter.construct.<locals>._modelc                s    fdd| D S )z$
            Querysets.
            c                s   g | ]} |qS r   r   )rG   rj   )r9   r   r   r   rI   9  s    z2Emitter.construct.<locals>._qs.<locals>.<listcomp>r   )r    r   )r9   )r   r   r=   5  s    zEmitter.construct.<locals>._qsc                s    fdd| D S )z 
            Lists.
            c                s   g | ]} |qS r   r   )rG   rj   )r9   r   r   rI   ?  s    z4Emitter.construct.<locals>._list.<locals>.<listcomp>r   )r    )r9   r   r   r;   ;  s    z Emitter.construct.<locals>._listc                s   t  fdd|  D S )z'
            Dictionaries.
            c                s   g | ]\}}| |fqS r   r   )rG   rV   rj   )r9   r   r   rI   E  s    z4Emitter.construct.<locals>._dict.<locals>.<listcomp>)r,   items)r    )r9   r   r   r:   A  s    z Emitter.construct.<locals>._dict)r   )r   )r   )r   )r   )r    r   )r%   r   )	r9   r:   rF   r;   rL   r<   r=   rK   r%   r   	construct_   s    	  zEmitter.constructc             C   s4   x.| j  D ] \}\}}||kr||kr|S qW d S )N)r   ro   )r%   r   r   klassZkmZis_anonr   r   r   r[   J  s    zEmitter.in_typemapperc             C   s   t ddS )zw
        This super emitter does not implement `render`,
        this is a job for the specific emitter below.
        zPlease implement render.N)NotImplementedError)r%   r   r   r   renderO  s    zEmitter.renderc             c   s   |  |V  dS )z
        Tells our patched middleware not to look
        at the contents, and returns a generator
        rather than the buffered string. Should be
        more memory friendly for large datasets.
        N)rs   )r%   Zrequeststreamr   r   r   stream_renderV  s    zEmitter.stream_renderc             C   s&   || j kr| j |S td| dS )zH
        Gets an emitter, returns the class and a content-type.
        zNo emitters found for type %sN)EMITTERSrD   
ValueError)clsformatr   r   r   rD   _  s    
zEmitter.get
text/plainc             C   s   ||f| j |< dS )z
        Register an emitter.

        Parameters::
         - `name`: The name of the emitter ('json', 'xml', 'yaml', ...)
         - `klass`: The emitter class.
         - `content_type`: The content type to serve response as.
        N)rv   )rx   rC   rq   Zcontent_typer   r   r   registerj  s    
zEmitter.registerc             C   s   | j |dS )z
        Remove an emitter from the registry. Useful if you don't
        want to provide output in one of the built-in emitters.
        N)rv   pop)rx   rC   r   r   r   
unregisterv  s    zEmitter.unregisterN)r   r   T)T)rz   )__name__
__module____qualname____doc__rv   r]   ZRESERVED_FIELDSr&   r'   rp   r[   rs   ru   classmethodrD   r{   r}   r   r   r   r   r   >   s    
 l
	r   )Z
__future__r   r-   r`   r/   ra   ZyamlImportErrorr   	NameErrorZdjango.db.models.queryr   Zdjango.db.modelsr	   r
   Zdjango.utils.encodingr   Zdjango.core.urlresolversr   r   Zdjango.httpr   ZcPicklepickleZdjango.templater   r   ri   objectr   r   r   r   r   <module>   s.   

