
^Q\.                 @   sr   d  d l  m Z d d l m Z m Z m Z Gd d   d e  Z Gd d   d e  Z Gd d	   d	 e  Z	 d
 S)    )cached_property   )OrderByOrderByTuplesegmentc                   s   e  Z d  Z d Z   f d d   Z d d   Z d d   Z d d	   Z e d
 d    Z	 e d d    Z
 e d d    Z e d d    Z   S)	TableDataz/
    Base class for table data containers.
    c                s)   | |  _  | |  _ t t |   j   d  S)N)datatablesuperr   __init__)selfr   r	   )	__class__ H/var/www/dbchiro/venv/lib/python3.4/site-packages/django_tables2/data.pyr   
   s    		zTableData.__init__c             C   s   |  j  | S)zh
        Slicing returns a new `.TableData` instance, indexing returns a
        single record.
        )r   )r   keyr   r   r   __getitem__   s    zTableData.__getitem__c             C   s   t  |  j  S)z
        for ... in ... default to using this. There's a bug in Django 1.3
        with indexing into querysets, so this side-steps that problem (as well
        as just being a better way to iterate).
        )iterr   )r   r   r   r   __iter__   s    zTableData.__iter__c             C   s   t  |  j d d   S)Nmodel)getattrr   )r   r   r   r   	get_model   s    zTableData.get_modelc             C   s   d  S)Nr   )r   r   r   r   ordering"   s    zTableData.orderingc             C   s   d S)Nitemr   )r   r   r   r   verbose_name&   s    zTableData.verbose_namec             C   s   d S)Nitemsr   )r   r   r   r   verbose_name_plural*   s    zTableData.verbose_name_pluralc             C   s`   t  j |   r t  |  |  St j |   r> t t |   |  St d j t |   j    d  S)Nz`data must be QuerySet-like (have count() and order_by()) or support list(data) -- {} has neither)TableQuerysetDatavalidateTableListDatalist
ValueErrorformattype__name__)r   r	   r   r   r   	from_data.   s    zTableData.from_data)r#   
__module____qualname____doc__r   r   r   r   propertyr   r   r   staticmethodr$   r   r   )r   r   r      s   r   c                   sp   e  Z d  Z d Z e d d    Z d d   Z e   f d d    Z e   f d d	    Z	 d
 d   Z
   S)r   a  
    Table data container for a list of dicts, for example::

    [
        {'name': 'John', 'age': 20},
        {'name': 'Brian', 'age': 25}
    ]

    .. note::

        Other structures might have worked in the past, but are not explicitly
        supported or tested.
    c             C   s+   t  |  d  p* t  |  d  o* t  |  d  S)z<
        Validates `data` for use in this container
        r   __len__r   )hasattr)r   r   r   r   r   J   s    zTableListData.validatec             C   s   t  |  j  S)N)lenr   )r   r   r   r   r*   T   s    zTableListData.__len__c                s   t  |  j d t t |   j  S)Nr   )r   r   r
   r   r   )r   )r   r   r   r   W   s    zTableListData.verbose_namec                s   t  |  j d t t |   j  S)Nr   )r   r   r
   r   r   )r   )r   r   r   r   [   s    z!TableListData.verbose_name_pluralc             C   s   g  } x^ | D]V } |  j  j t |  j } | d | j d k rV | | j j 7} q | | j 7} q W|  j j d t	 |  j
  d S)aG  
        Order the data based on order by aliases (prefixed column names) in the
        table.

        Arguments:
            aliases (`~.utils.OrderByTuple`): optionally prefixed names of
                columns ('-' indicates descending order) in order of
                significance with regard to data ordering.
        r   r   N)r	   columnsr   bareorder_by_aliasorder_byoppositer   sortr   r   )r   aliases	accessorsaliasbound_columnr   r   r   r0   _   s    
zTableListData.order_by)r#   r%   r&   r'   r)   r   r*   r(   r   r   r0   r   r   )r   r   r   ;   s   
r   c               @   sv   e  Z d  Z d Z e d d    Z d d   Z e d d    Z d d	   Z	 e
 d
 d    Z e
 d d    Z d S)r   z.
    Table data container for a queryset.
    c             C   s:   t  |  d  o9 t |  j  o9 t  |  d  o9 t |  j  S)z<
        Validates `data` for use in this container
        countr0   )r+   callabler7   r0   )r   r   r   r   r   }   s    zTableQuerysetData.validatec             C   s+   t  |  d  s$ |  j j   |  _ n  |  j S)N_length)r+   r   r7   r9   )r   r   r   r   r*      s    zTableQuerysetData.__len__c             C   sf   i  } x$ |  j  j D] } | j | | j <q Wy  t t |  j j j |   SWn t k
 ra Yn Xd S)a]  
        Returns the list of order by aliases that are enforcing ordering on the
        data.

        If the data is unordered, an empty sequence is returned. If the
        ordering can not be determined, `None` is returned.

        This works by inspecting the actual underlying data. As such it's only
        supported for querysets.
        N)	r	   r-   r0   r/   nextr   r   queryStopIteration)r   r3   r6   r   r   r   r      s     zTableQuerysetData.orderingc       	      C   s   d } g  } x | D] } |  j  j t |  j } | d | j d k r\ | | j j 7} n | | j 7} | r | j |  j | d d k  \ } } | r | |  _ d } q q q W| r d S| r d d   | D } |  j j |   |  _ n  d S)aG  
        Order the data based on order by aliases (prefixed column names) in the
        table.

        Arguments:
            aliases (`~.utils.OrderByTuple`): optionally prefixed names of
                columns ('-' indicates descending order) in order of
                significance with regard to data ordering.
        Fr   -TNc             s   s   |  ] } | j    Vq d  S)N)Zfor_queryset).0ar   r   r   	<genexpr>   s    z-TableQuerysetData.order_by.<locals>.<genexpr>)	r	   r-   r   r.   r/   r0   r1   orderr   )	r   r3   Zmodified_anyr4   r5   r6   ZquerysetmodifiedZorder_by_accessorsr   r   r   r0      s"    
%	zTableQuerysetData.order_byc             C   s   |  j  j j j S)zz
        The full (singular) name for the data.

        Model's `~django.db.Model.Meta.verbose_name` is honored.
        )r   r   _metar   )r   r   r   r   r      s    zTableQuerysetData.verbose_namec             C   s   |  j  j j j S)zx
        The full (plural) name for the data.

        Model's `~django.db.Model.Meta.verbose_name` is honored.
        )r   r   rC   r   )r   r   r   r   r      s    z%TableQuerysetData.verbose_name_pluralN)r#   r%   r&   r'   r)   r   r*   r(   r   r0   r   r   r   r   r   r   r   r   x   s   
	&	r   N)
Zdjango.utils.functionalr   utilsr   r   r   objectr   r   r   r   r   r   r   <module>   s   5=