o
    z,hj                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ 	dddZd	d	d
ddZ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.
    )Http404HttpResponseHttpResponsePermanentRedirectHttpResponseRedirect)loader)NoReverseMatchreverse)PromiseNc                 C   s   t j||| |d}t|||S )z
    Return an HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    )using)r   render_to_stringr   )requesttemplate_namecontextcontent_typestatusr
   content r   F/var/www/html/rh/venv/lib/python3.10/site-packages/django/shortcuts.pyrender   s   r   F)	permanentpreserve_requestc                O   s*   |rt nt}|t| g|R i ||dS )ak  
    Return 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: `urls.reverse()` will be used
          to reverse-resolve the name.

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

    Issues a temporary redirect by default. Set permanent=True to issue a
    permanent redirect. Set preserve_request=True to instruct the user agent
    to preserve the original HTTP method and body when following the redirect.
    )r   )r   r   resolve_url)tor   r   argskwargsredirect_classr   r   r   redirect   s   
r   c                 C   s   t | dr
| j S | S )z
    Return a QuerySet or a Manager.
    Duck typing in action: any class with a `get()` method (for
    get_object_or_404) or a `filter()` method (for get_list_or_404) might do
    the job.
    _default_manager)hasattrr   all)klassr   r   r   _get_queryset8   s   

r!   c                 O   sn   t | }t|dst| tr| jn| jj}td| z	|j|i |W S  |jj	y6   t
d|jjj w )aY  
    Use get() to return an object, or raise an 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.

    Like with QuerySet.get(), MultipleObjectsReturned is raised if more than
    one object is found.
    getzVFirst argument to get_object_or_404() must be a Model, Manager, or QuerySet, not '%s'.No %s matches the given query.)r!   r   
isinstancetype__name__	__class__
ValueErrorr"   modelDoesNotExistr   _metaobject_namer    r   r   querysetklass__namer   r   r   get_object_or_404E   s    
r0   c                    s~   t | }t|dst| tr| jn| jj}td| dz|j|i |I dH W S  |jj	y>   t
d|jjj dw )zSee get_object_or_404().agetzSFirst argument to aget_object_or_404() must be a Model, Manager, or QuerySet, not ''.NNo  matches the given query.)r!   r   r$   r%   r&   r'   r(   r1   r)   r*   r   r+   r,   r-   r   r   r   aget_object_or_404a   s   
r5   c                 O   sd   t | }t|dst| tr| jn| jj}td| t|j|i |}|s0t	d|j
jj |S )z
    Use filter() to return a list of objects, or raise an 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.
    filterzTFirst argument to get_list_or_404() must be a Model, Manager, or QuerySet, not '%s'.r#   )r!   r   r$   r%   r&   r'   r(   listr6   r   r)   r+   r,   r    r   r   r.   r/   obj_listr   r   r   get_list_or_404r   s   
r:   c                    sz   t | }t|dst| tr| jn| jj}td| ddd |j|i |2 I dH }|s;td|j	j
j d|S )	zSee get_list_or_404().r6   zQFirst argument to aget_list_or_404() must be a Model, Manager, or QuerySet, not 'r2   c                    s   g | z3 d H W }|q6 S )Nr   ).0objr   r   r   
<listcomp>   s    z$aget_list_or_404.<locals>.<listcomp>Nr3   r4   )r!   r   r$   r%   r&   r'   r(   r6   r   r)   r+   r,   r8   r   r   r   aget_list_or_404   s   
 r>   c                 O   s   t | dr	|  S t| trt| } t| tr| dr| S zt| ||dW S  tyA   t| r2 d| vr;d| vr> Y | S Y | S w )aM  
    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: `urls.reverse()` will be used
          to reverse-resolve the name.

        * A URL, which will be returned as-is.
    get_absolute_url)z./z../)r   r   /.)	r   r?   r$   r	   str
startswithr   r   callable)r   r   r   r   r   r   r      s$   

r   )NNNN)__doc__django.httpr   r   r   r   django.templater   django.urlsr   r   django.utils.functionalr	   r   r   r!   r0   r5   r:   r>   r   r   r   r   r   <module>   s    
