î

_ÝV  ã            
   @   s{  d  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 m Z m Z d d l m Z m Z d d	 l m Z d d
 l m Z m Z m Z d d l m Z d d l m Z d d l m Z d d l m Z d e d d e e d d d „ Z  d e d d e e e d d d „ Z! d d „  Z" d d „  Z# d d „  Z$ d d „  Z% d d „  Z& d S)zµ
This module collects helper functions and classes that "span" multiple levels
of MVC. In other words, these functions/classes introduce controlled coupling
for convenience's sake.
é    N)Úurlresolvers)Ú	ModelBase)ÚManager)ÚQuerySet)ÚHttp404ÚHttpResponseÚHttpResponsePermanentRedirectÚHttpResponseRedirect)ÚRequestContextÚloader)Ú_current_app_undefined)Ú_context_instance_undefinedÚ_dictionary_undefinedÚ_dirs_undefined)Úsix)ÚRemovedInDjango110Warning)Ú
force_text)ÚPromisec       	      C   sp   | t  k r? | t k r? | t k r? t j |  | d | ƒ} n! t j |  | | | | d | ƒ} t | | | ƒ S)zœ
    Returns a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    Úusing)r   r   r   r   Úrender_to_stringr   )	Útemplate_nameÚcontextÚcontext_instanceÚcontent_typeÚstatusÚdirsÚ
dictionaryr   Úcontent© r   ú2/tmp/pip-build-ghmbqnp_/Django/django/shortcuts.pyÚrender_to_response   s    	r    c
             C   sï   | t  k rQ | t k rQ | t k rQ | t k rQ t j | | d |  d |	 ƒ}
 nŽ | t  k	 r{ | t k	 r¾ t d ƒ ‚ q¾ nC t |  ƒ } | t k	 r¾ t j	 d t
 d d ƒ| |  _ | | _ n  t j | | | | | d |	 ƒ}
 t |
 | | ƒ S)zÂ
    Returns a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    Uses a RequestContext by default.
    Úrequestr   zVIf you provide a context_instance you must set its current_app before calling render()zcThe current_app argument of render is deprecated. Set the current_app attribute of request instead.Ú
stacklevelé   )r   r   r   r   r   r   Ú
ValueErrorr
   ÚwarningsÚwarnr   Úcurrent_appZ_current_appr   )r!   r   r   r   r   r   r'   r   r   r   r   r   r   r   Úrender2   s(    
		r(   c             O   s7   | j  d d ƒ r t } n t } | t |  | | Ž ƒ S)að  
    Returns an HttpResponseRedirect to the appropriate URL for the arguments
    passed.

    The arguments could be:

        * A model: the model's `get_absolute_url()` function will be called.

        * A view name, possibly with arguments: `urlresolvers.reverse()` will
          be used to reverse-resolve the name.

        * A URL, which will be used as-is for the redirect location.

    By default issues a temporary redirect; pass permanent=True to issue a
    permanent redirect
    Z	permanentF)Úpopr   r	   Úresolve_url)ÚtoÚargsÚkwargsZredirect_classr   r   r   Úredirect^   s    	r.   c             C   s‡   t  |  t ƒ r |  St  |  t ƒ r+ |  } nR t  |  t ƒ rF |  j } n7 t  |  t ƒ ra |  j } n |  j j } t d | ƒ ‚ | j	 ƒ  S)zÌ
    Returns a QuerySet from a Model, Manager, or QuerySet. Created to make
    get_object_or_404 and get_list_or_404 more DRY.

    Raises a ValueError if klass is not a Model, Manager, or QuerySet.
    zHObject is of type '%s', but must be a Django Model, Manager, or QuerySet)
Ú
isinstancer   r   r   Z_default_managerÚtypeÚ__name__Ú	__class__r$   Úall)ÚklassÚmanagerZklass__namer   r   r   Ú_get_querysetw   s    	
r6   c             O   sX   t  |  ƒ } y | j | | Ž  SWn1 | j j k
 rS t d | j j j ƒ ‚ Yn Xd S)a_  
    Uses get() to return an object, or raises a Http404 exception if the object
    does not exist.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the get() query.

    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
    object is found.
    zNo %s matches the given query.N)r6   ÚgetÚmodelZDoesNotExistr   Ú_metaÚobject_name)r4   r,   r-   Úquerysetr   r   r   Úget_object_or_404Ž   s
    r<   c             O   sJ   t  |  ƒ } t | j | | Ž  ƒ } | sF t d | j j j ƒ ‚ n  | S)zø
    Uses filter() to return a list of objects, or raise a Http404 exception if
    the list is empty.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the filter() query.
    zNo %s matches the given query.)r6   ÚlistÚfilterr   r8   r9   r:   )r4   r,   r-   r;   Zobj_listr   r   r   Úget_list_or_404    s
    r?   c             O   sÈ   t  |  d ƒ r |  j ƒ  St |  t ƒ r7 t |  ƒ }  n  t |  t j ƒ r_ |  j d ƒ r_ |  Sn  y t j	 |  d | d | ƒSWnE t j
 k
 rÃ t |  ƒ r¡ ‚  n  d |  k r¿ d |  k r¿ ‚  n  Yn X|  S)	aU  
    Return a URL appropriate for the arguments passed.

    The arguments could be:

        * A model: the model's `get_absolute_url()` function will be called.

        * A view name, possibly with arguments: `urlresolvers.reverse()` will
          be used to reverse-resolve the name.

        * A URL, which will be returned as-is.
    Úget_absolute_urlú./ú../r,   r-   ú/Ú.)rA   rB   )Úhasattrr@   r/   r   r   r   Ústring_typesÚ
startswithr   ÚreverseZNoReverseMatchÚcallable)r+   r,   r-   r   r   r   r*   ¯   s    
r*   )'Ú__doc__r%   Zdjango.corer   Zdjango.db.models.baser   Zdjango.db.models.managerr   Zdjango.db.models.queryr   Zdjango.httpr   r   r   r	   Zdjango.templater
   r   Zdjango.template.contextr   Zdjango.template.enginer   r   r   Zdjango.utilsr   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   Zdjango.utils.functionalr   r    r(   r.   r6   r<   r?   r*   r   r   r   r   Ú<module>   s6   "		(