U
    J|e                     @   s   d dl mZ d dlmZ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 G dd deZG d	d
 d
eeZG dd deZG dd deeZdS )    )ImproperlyConfigured)InvalidPage	Paginator)QuerySet)Http404)gettext)ContextMixinTemplateResponseMixinViewc                       s   e Zd ZdZdZdZdZdZdZdZ	e
ZdZdZdd Zdd	 Zd
d Zdd ZdddZdd Zdd Zdd Zdd fdd
Z  ZS )MultipleObjectMixinz0A mixin for views manipulating multiple objects.TNr   pagec                 C   sz   | j dk	r$| j }t|trP| }n,| jdk	r<| jj }ntdd| jji | 	 }|rvt|t
rl|f}|j| }|S )z
        Return the list of items for this view.

        The return value must be an iterable and may be an instance of
        `QuerySet` in which case `QuerySet` specific behavior will be enabled.
        Nzj%(cls)s is missing a QuerySet. Define %(cls)s.model, %(cls)s.queryset, or override %(cls)s.get_queryset().cls)queryset
isinstancer   allmodel_default_managerr   	__class____name__get_orderingstrorder_by)selfr   ordering r   V/var/www/website-v5/atlas_env/lib/python3.8/site-packages/django/views/generic/list.pyget_queryset   s"    






z MultipleObjectMixin.get_querysetc                 C   s   | j S )z<Return the field or fields to use for ordering the queryset.)r   r   r   r   r   r   1   s    z MultipleObjectMixin.get_orderingc              
   C   s   | j |||  |  d}| j}| j|p<| jj|p<d}zt|}W n0 t	k
rz   |dkrj|j
}nttdY nX z||}|||j| fW S  tk
r } zttd|t|d W 5 d}~X Y nX dS )z!Paginate the queryset, if needed.orphansallow_empty_first_page   lastu:   Page is not “last”, nor can it be converted to an int.z+Invalid page (%(page_number)s): %(message)s)page_numbermessageN)get_paginatorget_paginate_orphansget_allow_empty
page_kwargkwargsgetrequestGETint
ValueErrorZ	num_pagesr   _r   object_listZhas_other_pagesr   r   )r   r   	page_size	paginatorr(   r   r#   er   r   r   paginate_queryset5   s2    

z%MultipleObjectMixin.paginate_querysetc                 C   s   | j S )zX
        Get the number of items to paginate by, or ``None`` for no pagination.
        )paginate_by)r   r   r   r   r   get_paginate_byQ   s    z#MultipleObjectMixin.get_paginate_byc                 K   s   | j ||f||d|S )z2Return an instance of the paginator for this view.r   )paginator_class)r   r   Zper_pager   r    r)   r   r   r   r%   W   s    z!MultipleObjectMixin.get_paginatorc                 C   s   | j S )zg
        Return the maximum number of orphans extend the last page by when
        paginating.
        )paginate_orphansr   r   r   r   r&   c   s    z(MultipleObjectMixin.get_paginate_orphansc                 C   s   | j S )z
        Return ``True`` if the view should display empty lists and ``False``
        if a 404 should be raised instead.
        )allow_emptyr   r   r   r   r'   j   s    z#MultipleObjectMixin.get_allow_emptyc                 C   s,   | j r| j S t|dr$d|jjj S dS dS )z3Get the name of the item to be used in the context.r   z%s_listN)context_object_namehasattrr   _meta
model_name)r   r0   r   r   r   get_context_object_nameq   s
    
z+MultipleObjectMixin.get_context_object_name)r0   c          
         s   |dk	r|n| j }| |}| |}|rN| ||\}}}}||||d}	nddd|d}	|dk	rl||	|< |	| t jf |	S )zGet the context for this view.N)r2   Zpage_objis_paginatedr0   F)r0   r6   r>   r4   updatesuperget_context_data)
r   r0   r)   r   r1   r:   r2   r   r?   contextr   r   r   rB   z   s,    

 
z$MultipleObjectMixin.get_context_data)r   T)r   
__module____qualname____doc__r9   r   r   r5   r8   r:   r   r7   r(   r   r   r   r4   r6   r%   r&   r'   r>   rB   __classcell__r   r   rD   r   r   	   s*      
	r   c                   @   s   e Zd ZdZdd ZdS )BaseListViewz-A base view for displaying a list of objects.c                 O   sv   |   | _|  }|sd| | jd k	r@t| jdr@| j  }n| j }|rdttdd| jj	i | 
 }| |S )Nexistsu9   Empty list and “%(class_name)s.allow_empty” is False.
class_name)r   r0   r'   r6   r;   rJ   r   r/   r   r   rB   render_to_response)r   r+   argsr)   r9   is_emptyrC   r   r   r   r*      s&    
  zBaseListView.getN)r   rE   rF   rG   r*   r   r   r   r   rI      s   rI   c                       s$   e Zd ZdZdZ fddZ  ZS )#MultipleObjectTemplateResponseMixinz9Mixin for responding with a template and list of objects.Z_listc                    sv   zt   }W n tk
r&   g }Y nX t| jdrZ| jjj}|d|j|j	| j
f  n|srtdd| jji |S )z
        Return a list of template names to be used for the request. Must return
        a list. May not be called if render_to_response is overridden.
        r   z%s/%s%s.htmlzg%(cls)s requires either a 'template_name' attribute or a get_queryset() method that returns a QuerySet.r   )rA   get_template_namesr   r;   r0   r   r<   append	app_labelr=   template_name_suffixr   r   )r   namesoptsrD   r   r   rP      s(    

 z6MultipleObjectTemplateResponseMixin.get_template_names)r   rE   rF   rG   rS   rP   rH   r   r   rD   r   rO      s   rO   c                   @   s   e Zd ZdZdS )ListViewz
    Render some list of objects, set by `self.model` or `self.queryset`.
    `self.queryset` can actually be any iterable of items, not just a queryset.
    N)r   rE   rF   rG   r   r   r   r   rV      s   rV   N)django.core.exceptionsr   Zdjango.core.paginatorr   r   django.db.modelsr   django.httpr   django.utils.translationr   r/   django.views.generic.baser   r	   r
   r   rI   rO   rV   r   r   r   r   <module>   s    &