U
    8ÄT_ü  ã                   @   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ÚmodelZ_default_managerr   Ú	__class__Ú__name__Úget_orderingÚstrZorder_by)Úselfr   Úordering© r   ú=/tmp/pip-unpacked-wheel-dv63caxw/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   2   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_queryset6   s,      þ
þ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_byM   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#   S   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$   Z   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%   a   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   r.   r   r   r   Úget_context_object_nameh   s
    
z+MultipleObjectMixin.get_context_object_name)r.   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)r0   Zpage_objÚis_paginatedr.   F)r.   r4   r<   r2   ÚupdateÚsuperÚget_context_data)
r   r.   r'   r   r/   r8   r0   r   r=   Úcontext©r   r   r   r@   q   s&    

üü
z$MultipleObjectMixin.get_context_data)r   T)r   Ú
__module__Ú__qualname__Ú__doc__r7   r   r   r3   r6   r8   r   r5   r&   r   r   r   r2   r4   r#   r$   r%   r<   r@   Ú__classcell__r   r   rB   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   r.   r%   r4   r9   rH   r   r-   r   r   r@   Zrender_to_response)r   r)   Úargsr'   r7   Zis_emptyrA   r   r   r   r(   �   s    
 ÿzBaseListView.getN)r   rC   rD   rE   r(   r   r   r   r   rG   ‹   s   rG   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   )r?   Úget_template_namesr   r9   r.   r   r:   ÚappendZ	app_labelr;   Útemplate_name_suffixr   r   )r   ÚnamesÚoptsrB   r   r   rL   ¥   s     

 ÿÿÿz6MultipleObjectTemplateResponseMixin.get_template_names)r   rC   rD   rE   rN   rL   rF   r   r   rB   r   rK   ¡   s   rK   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   rC   rD   rE   r   r   r   r   rQ   Â   s   rQ   N)Zdjango.core.exceptionsr   Zdjango.core.paginatorr   r   Zdjango.db.modelsr   Zdjango.httpr   Zdjango.utils.translationr   r-   Zdjango.views.generic.baser   r	   r
   r   rG   rK   rQ   r   r   r   r   Ú<module>   s    !