o
    ˷e/                     @   s   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	 G dd dZ
G d	d
 d
e
ZG dd deeZG dd de
ZdS )    )count)ImproperlyConfigured)ListView   )tables)RequestConfigc                   @   s(   e Zd ZdZdZdZdd Zdd ZdS )TableMixinBasezF
    Base mixin for the Single- and MultiTable class based views.
    tableNc                 C      | j S )zH
        Get the name to use for the table's template variable.
        )context_table_name)selfr	    r   K/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/views.pyget_context_table_name      z%TableMixinBase.get_context_table_namec                 C   s   | j }|du r	dS i }t| dddur| j|d< t| dr"| j|d< t| dddkr/| j|d< | j r8|| j  |sA| j du rAd	S |S )
a  
        Return pagination options passed to `.RequestConfig`:
            - True for standard pagination (default),
            - False for no pagination,
            - a dictionary for custom pagination.

        `ListView`s pagination attributes are taken into account, if `table_pagination` does not
        define the corresponding value.

        Override this method to further customize pagination for a `View`.
        Fpaginate_byNper_pagepaginator_classpaginate_orphansr   orphansT)table_paginationgetattrr   hasattrr   r   update)r   r	   paginater   r   r   get_table_pagination   s   



z#TableMixinBase.get_table_pagination)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   
   s    r   c                       sH   e Zd ZdZdZdZdd Zdd Zdd Zd	d
 Z	 fddZ
  ZS )SingleTableMixina  
    Adds a Table object to the context. Typically used with
    `.TemplateResponseMixin`.

    Attributes:
        table_class: subclass of `.Table`
        table_data: data used to populate the table, any compatible data source.
        context_table_name(str): name of the table's template variable (default:
            'table')
        table_pagination (dict): controls table pagination. If a `dict`, passed as
            the *paginate* keyword argument to `.RequestConfig`. As such, any
            Truthy value enables pagination. (default: enable pagination).

            The `dict` can be used to specify values for arguments for the call to
            `~.tables.Table.paginate`.

            If you want to use a non-standard paginator for example, you can add a key
            `paginator_class` to the dict, containing a custom `Paginator` class.

    This mixin plays nice with the Django's ``.MultipleObjectMixin`` by using
    ``.get_queryset`` as a fall back for the table data source.
    Nc                 C   s2   | j r| j S | jrt| jS tdt| j)z8
        Return the class to use for the table.
        z4You must either specify {0}.table_class or {0}.model)table_classmodelr   table_factoryr   formattyper   r   r   r   r   get_table_classV   s   z SingleTableMixin.get_table_classc                 K   s8   |   }|dd|  i|}t| j| |d|S )zs
        Return a table object to use. The table has automatic support for
        sorting and pagination.
        datar   Nr   )r'   get_table_datar   requestr   	configure)r   kwargsr!   r	   r   r   r   	get_tablec   s
   zSingleTableMixin.get_tablec                 C   sJ   | j dur| j S t| dr| jS t| dr|  S t| j}td|)zQ
        Return the table data that should be used to populate the rows.
        Nobject_listget_querysetz2Table data was not specified. Define {}.table_data)
table_datar   r/   r0   r%   r   r   r$   r   klassr   r   r   r*   n   s   



zSingleTableMixin.get_table_datac                 C   s   i S )ar  
        Return the keyword arguments for instantiating the table.

        Allows passing customized arguments to the table constructor, for example,
        to remove the buttons column, you could define this method in your View::

            def get_table_kwargs(self):
                return {
                    'exclude': ('buttons', )
                }
        r   r&   r   r   r   get_table_kwargs~   s   z!SingleTableMixin.get_table_kwargsc                    s8   t  jdi |}| jdi |  }||| |< |S )zy
        Overridden version of `.TemplateResponseMixin` to inject the table into
        the template's context.
        Nr   )superget_context_datar.   r4   r   )r   r-   contextr	   	__class__r   r   r6      s   z!SingleTableMixin.get_context_data)r   r   r   r   r!   r1   r'   r.   r*   r4   r6   __classcell__r   r   r8   r   r    ;   s    r    c                   @   s   e Zd ZdZdS )SingleTableViewz
    Generic view that renders a template and passes in a `.Table` instances.

    Mixes ``.SingleTableMixin`` with ``django.views.generic.list.ListView``.
    N)r   r   r   r   r   r   r   r   r;      s    r;   c                       s@   e Zd ZdZdZdZdZdZdd Zdd Z	 fd	d
Z
  ZS )MultiTableMixinas  
    Add a list with multiple Table object's to the context. Typically used with
    `.TemplateResponseMixin`.

    The `tables` attribute must be either a list of `.Table` instances or
    classes extended from `.Table` which are not already instantiated. In that
    case, `get_tables_data` must be able to return the tables data, either by
    having an entry containing the data for each table in `tables`, or by
    overriding this method in order to return this data.

    Attributes:
        tables: list of `.Table` instances or list of `.Table` child objects.
        tables_data: if defined, `tables` is assumed to be a list of table
            classes which will be instantiated with the corresponding item from
            this list of `.TableData` instances.
        table_prefix(str): Prefix to be used for each table. The string must
            contain one instance of `{}`, which will be replaced by an integer
            different for each table in the view. Default is 'table_{}-'.
        context_table_name(str): name of the table's template variable (default:
            'tables')

    .. versionadded:: 1.2.3
    Nz	table_{}-r   c                    s   | j du rt| j}td||    du r| j S t t| j kr2t| j}td||t fddt| j D S )zE
        Return an array of table instances containing data.
        Nz*No tables were specified. Define {}.tablesz%len({}.tables_data) != len({}.tables)c                 3   s     | ]\}}| | V  qd S )Nr   ).0iTabler(   r   r   	<genexpr>   s    z-MultiTableMixin.get_tables.<locals>.<genexpr>)	r   r%   r   r   r$   get_tables_datalenlist	enumerater2   r   r@   r   
get_tables   s   


zMultiTableMixin.get_tablesc                 C   r
   )zZ
        Return an array of table_data that should be used to populate each table
        )tables_datar&   r   r   r   rB      r   zMultiTableMixin.get_tables_datac                    sr   t  jdi |}|  }t }|D ]$}|jp| jt||_t| j	| 
|d| t||| |< q|S )Nr)   r   )r5   r6   rF   r   prefixtable_prefixr$   nextr   r+   r   r,   rD   r   )r   r-   r7   r   table_counterr	   r8   r   r   r6      s   z MultiTableMixin.get_context_data)r   r   r   r   r   rG   rI   r   rF   rB   r6   r:   r   r   r8   r   r<      s    r<   N)	itertoolsr   django.core.exceptionsr   django.views.generic.listr    r   configr   r   r    r;   r<   r   r   r   r   <module>   s    1\