î
ä^Q\¢  ã               @   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	 d d l
 m Z Gd d	 „  d	 e ƒ Z Gd
 d „  d e ƒ Z Gd d „  d e e ƒ Z Gd d „  d e ƒ Z d S)é    )Úunicode_literals)Úcount)ÚImproperlyConfigured)ÚListViewé   )Útables)ÚRequestConfigc               @   sL   e  Z d  Z d Z d Z d Z d Z d d „  Z d d „  Z d d	 „  Z	 d S)
ÚTableMixinBasez9
    Base mixin for table-related class based views.
    ÚtableNc             C   sQ   |  j  r |  j  S|  j r) t j |  j ƒ St |  ƒ j } t d j | ƒ ƒ ‚ d S)z8
        Return the class to use for the table.
        z3You must either specify {0}.table_class or{0}.modelN)Útable_classZmodelr   Ztable_factoryÚtypeÚ__name__r   Úformat)ÚselfÚklass© r   úI/var/www/dbchiro/venv/lib/python3.4/site-packages/django_tables2/views.pyÚget_table_class   s    		zTableMixinBase.get_table_classc             C   s   |  j  S)zH
        Get the name to use for the table's template variable.
        )Úcontext_table_name)r   r
   r   r   r   Úget_context_table_name$   s    z%TableMixinBase.get_context_table_namec             C   sW   |  j  } t |  d ƒ rC |  j d k	 rC | p0 i  } |  j | d <n  | d k rS d S| S)zž
        Returns pagination options: True for standard pagination (default),
        False for no pagination, and a dictionary for custom pagination.
        Úpaginate_byNZper_pageT)Útable_paginationÚhasattrr   )r   r
   Úpaginater   r   r   Úget_table_pagination*   s    	z#TableMixinBase.get_table_pagination)
r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r	      s   r	   c                   sX   e  Z d  Z d Z d Z d Z d d „  Z d d „  Z d d „  Z ‡  f d	 d
 †  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)

    This mixin plays nice with the Django's`.MultipleObjectMixin` by using
    `.get_queryset`` as a fallback for the table data source.
    Nc             K   sM   |  j  ƒ  } | d |  j ƒ  |  } t |  j d |  j | ƒ ƒj | ƒ | S)zs
        Return a table object to use. The table has automatic support for
        sorting and pagination.
        Údatar   )r   Úget_table_datar   Úrequestr   Ú	configure)r   Úkwargsr   r
   r   r   r   Ú	get_tableQ   s    %zSingleTableMixin.get_tablec             C   sm   |  j  d k	 r |  j  St |  d ƒ r, |  j St |  d ƒ rE |  j ƒ  St |  ƒ j } t d j | ƒ ƒ ‚ d S)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%   r&   r   r   r   r   )r   r   r   r   r   r    [   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_kwargsk   s    z!SingleTableMixin.get_table_kwargsc                sD   t  t |  ƒ j |   } |  j |  j ƒ    } | | |  j | ƒ <| S)zx
        Overriden version of `.TemplateResponseMixin` to inject the table into
        the template's context.
        )Úsuperr   Úget_context_datar$   r(   r   )r   r#   Úcontextr
   )Ú	__class__r   r   r*   y   s    z!SingleTableMixin.get_context_data)
r   r   r   r   r   r'   r$   r    r(   r*   r   r   )r,   r   r   =   s   
r   c               @   s   e  Z d  Z d Z d S)ÚSingleTableViewzR
    Generic view that renders a template and passes in a `.Table` instances.
    N)r   r   r   r   r   r   r   r   r-   „   s   r-   c                   sL   e  Z d  Z d Z d Z d Z d Z d Z d d „  Z ‡  f d d †  Z	 ‡  S)	ÚMultiTableMixinaø  
    Adds a Table object 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, tables_data must be defined, having an entry containing the data for
    each table in `tables`.

    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 instatiated 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             C   s7   |  j  s0 t |  ƒ j } t d j | ƒ ƒ ‚ n  |  j  S)Nz*No tables were specified. Define {}.tables)r   r   r   r   r   )r   r   r   r   r   Ú
get_tables©   s
    	zMultiTableMixin.get_tablesc                s&  t  t |  ƒ j |   } |  j d  k r6 |  j ƒ  } ny |  j ‰  t ˆ  ƒ t |  j ƒ  ƒ k r‡ t |  ƒ j } t d j	 | | ƒ ƒ ‚ n  t
 ‡  f d d †  t |  j ƒ Dƒ ƒ } t ƒ  } xg | D]_ } |  j j	 t | ƒ ƒ | _ t |  j d |  j | ƒ ƒj | ƒ t
 | ƒ | |  j | ƒ <q¿ W| S)Nz%len({}.tables_data) != len({}.tables)c             3   s%   |  ] \ } } | ˆ  | ƒ Vq d  S)Nr   )Ú.0ÚiZTable)r   r   r   ú	<genexpr>¾   s    z3MultiTableMixin.get_context_data.<locals>.<genexpr>r   )r)   r.   r*   Útables_datar/   Úlenr   r   r   r   ÚlistÚ	enumerater   r   Útable_prefixÚnextÚprefixr   r!   r   r"   r   )r   r#   r+   r   r   Ztable_counterr
   )r,   )r   r   r*   ²   s    	(	%z MultiTableMixin.get_context_data)
r   r   r   r   r   r3   r7   r   r/   r*   r   r   )r,   r   r.   Š   s   	r.   N)Ú
__future__r   Ú	itertoolsr   Zdjango.core.exceptionsr   Zdjango.views.generic.listr   Ú r   Úconfigr   Úobjectr	   r   r-   r.   r   r   r   r   Ú<module>   s   0G