î
ä^Q\Ý*  ã               @   s¾   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
 d d l m Z m Z m Z m Z Gd d	 „  d	 e ƒ Z Gd
 d „  d e ƒ Z Gd d „  d e ƒ Z d S)é    )Úabsolute_importÚunicode_literals)Úmodels)ÚFieldDoesNotExist)Úsixé   )ÚBaseLinkColumn)ÚAÚAttributeDictÚcall_with_appropriateÚcomputed_valuesc               @   sÖ   e  Z d  Z d Z d d „  Z e d d „  ƒ Z d d „  Z e d d	 „  ƒ Z e d
 d „  ƒ Z	 d d „  Z
 d d „  Z d d „  Z d d „  Z d d d „ Z d d „  Z d d d „ Z d d „  Z d d „  Z d S)ÚBoundRowaµ  
    Represents a *specific* row in a table.

    `.BoundRow` objects are a container that make it easy to access the
    final 'rendered' values for cells in a row. You can simply iterate over a
    `.BoundRow` object and it will take care to return values rendered
    using the correct method (e.g. :ref:`table.render_FOO`)

    To access the rendered value of each cell in a row, just iterate over it::

        >>> import django_tables2 as tables
        >>> class SimpleTable(tables.Table):
        ...     a = tables.Column()
        ...     b = tables.CheckBoxColumn(attrs={'name': 'my_chkbox'})
        ...
        >>> table = SimpleTable([{'a': 1, 'b': 2}])
        >>> row = table.rows[0]  # we only have one row, so let's use it
        >>> for cell in row:
        ...     print(cell)
        ...
        1
        <input type="checkbox" name="my_chkbox" value="2" />

    Alternatively you can use row.get_cell() to retrieve a specific cell::

        >>> row.get_cell(0)
        1
        >>> row.get_cell(1)
        u'<input type="checkbox" name="my_chkbox" value="2" />'
        >>> row.get_cell(2)
        ...
        IndexError: list index out of range

    Finally you can also use the column names to retrieve a specific cell::

        >>> row.get_cell('a')
        1
        >>> row.get_cell('b')
        u'<input type="checkbox" name="my_chkbox" value="2" />'
        >>> row.get_cell('c')
        ...
        KeyError: 'c'

    Arguments:
        table: The `.Table` in which this row exists.
        record: a single record from the :term:`table data` that is used to
            populate the row. A record could be a `~django.db.Model` object, a
            `dict`, or something else.

    c             C   s(   | |  _  | |  _ t | j ƒ |  _ d  S)N)Ú_recordÚ_tableÚnextÚ_counterÚrow_counter)ÚselfÚrecordÚtable© r   úH/var/www/dbchiro/venv/lib/python3.4/site-packages/django_tables2/rows.pyÚ__init__?   s    		zBoundRow.__init__c             C   s   |  j  S)z1
        The associated `.Table` object.
        )r   )r   r   r   r   r   E   s    zBoundRow.tablec             C   s   |  j  d r d Sd S)z˜
        Return css class, alternating for odd and even records.

        Return:
            string: `even` for even records, `odd` otherwise.
        é   ZoddZeven)r   )r   r   r   r   Úget_even_odd_css_classL   s    zBoundRow.get_even_odd_css_classc             C   sm   |  j  ƒ  } t |  j j d i |  j d 6ƒ} d | k rY | d rY | d d | 7<n
 | | d <t | ƒ S)z:
        Return the attributes for a certain row.
        Úkwargsr   Úclassú )r   r   r   Ú	row_attrsr   r
   )r   ZcssClassr   r   r   r   ÚattrsU   s    "
zBoundRow.attrsc             C   s   |  j  S)zl
        The data record from the data source which is used to populate this row
        with data.
        )r   )r   r   r   r   r   e   s    zBoundRow.recordc             c   s&   x |  j  ƒ  D] \ } } | Vq Wd S)z°
        Iterate over the rendered values for cells in the row.

        Under the hood this method just makes a call to
        `.BoundRow.__getitem__` for each cell.
        N)Úitems)r   ÚcolumnÚvaluer   r   r   Ú__iter__m   s    zBoundRow.__iter__c             C   sH  |  j  j | } d  } t | j ƒ } | j |  j ƒ \ } } t | t j ƒ r½ yV | j	 |  j ƒ }	 t
 | d | d  ƒ }
 t
 |	 d f  ƒ r¤ |
 r¤ |
 ƒ  } d  } n  Wq½ t k
 r¹ Yq½ Xn  | r%y | j |  j ƒ } Wq%t k
 r!t | j t ƒ } | r| j j d  k	 r| | ƒ SYq%Xn  | | j j k r;| S| | | ƒ S)Nzget_%s_displayÚchoices)r   Úcolumnsr	   ÚaccessorÚpenultimater   Ú
isinstancer   ZModelÚ	get_fieldÚgetattrr   ÚresolveÚ	Exceptionr!   r   ÚtextZempty_values)r   ÚnameÚrender_funcÚdefaultÚbound_columnr"   r&   r'   Ú	remainderÚfieldZ
display_fnZis_linkcolumnr   r   r   Ú_get_and_render_withy   s0    		zBoundRow._get_and_render_withc             C   s7   i | d 6|  j  d 6| j d 6| d 6|  d 6|  j d 6S)zÂ
        Defines the arguments that will optionally be passed while calling the
        cell's rendering or value getter if that function has one of these as a
        keyword argument.
        r"   r   r!   r1   Z	bound_rowr   )r   r!   r   )r   r1   r"   r   r   r   Ú_optional_cell_argumentsŸ   s    

z!BoundRow._optional_cell_argumentsc             C   s)   |  j  | d |  j d |  j j | j ƒS)zl
        Returns the final rendered html for a cell in the row, given the name
        of a column.
        r/   r0   )r4   Ú_call_renderr   r%   r0   )r   r.   r   r   r   Úget_cell®   s    	zBoundRow.get_cellNc             C   s   t  | j |  j | | ƒ ƒ S)zI
        Call the column's render method with appropriate kwargs
        )r   Zrenderr5   )r   r1   r"   r   r   r   r6   ¹   s    zBoundRow._call_renderc             C   s   |  j  | d |  j d d ƒS)z‚
        Returns the final rendered value (excluding any html) for a cell in the
        row, given the name of a column.
        r/   r0   N)r4   Ú_call_value)r   r.   r   r   r   Úget_cell_valueÂ   s    	zBoundRow.get_cell_valuec             C   s   t  | j |  j | | ƒ ƒ S)zH
        Call the column's value method with appropriate kwargs
        )r   r"   r5   )r   r1   r"   r   r   r   r8   Í   s    zBoundRow._call_valuec             C   s0   t  | t j ƒ r" | |  j j k S| |  k Sd S)z;
        Check by both row object and column name.
        N)r(   r   Zstring_typesr   r%   )r   Úitemr   r   r   Ú__contains__Ö   s    zBoundRow.__contains__c             c   sJ   xC |  j  j D]5 } |  j | j ƒ | _ |  j | _ | | j f Vq Wd S)z½
        Returns iterator yielding ``(bound_column, cell)`` pairs.

        *cell* is ``row[name]`` -- the rendered unicode value that should be
        ``rendered within ``<td>``.
        N)r   r%   r7   r.   Zcurrent_valuer   Zcurrent_record)r   r!   r   r   r   r    ß   s    zBoundRow.items)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r#   r4   r5   r7   r6   r9   r8   r;   r    r   r   r   r   r      s   2	&			r   c               @   s(   e  Z d  Z d Z e d d „  ƒ Z d S)ÚBoundPinnedRowzL
    Represents a *pinned* row in a table.
    Inherited from BoundRow.
    c             C   s`   t  |  j j d i |  j d 6ƒ} d j |  j ƒ  d | j d d ƒ g ƒ } | | d <t | ƒ S)zÃ
        Return the attributes for a certain pinned row.
        Add css clases `pinned-row` to `class` attribute.

        Return:
            AttributeDict: Attributes for pinned rows.
        r   r   r   z
pinned-rowr   Ú )r   r   Zpinned_row_attrsr   Újoinr   Úgetr
   )r   r   Z	css_classr   r   r   r   õ   s    	"	
zBoundPinnedRow.attrsN)r<   r=   r>   r?   r@   r   r   r   r   r   rA   ï   s   rA   c               @   sU   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d d „  Z d S)Ú	BoundRowsaÉ  
    Container for spawning `.BoundRow` objects.

    Arguments:
        data: iterable of records
        table: the `~.Table` in which the rows exist
        pinned_data: dictionary with iterable of records for top and/or
         bottom pinned rows.

    Example:
        >>> pinned_data = {
        ...    'top': iterable,      # or None value
        ...    'bottom': iterable,   # or None value
        ... }

    This is used for `~.Table.rows`.
    Nc             C   s%   | |  _  | |  _ | p i  |  _ d  S)N)Údatar   Úpinned_data)r   rF   r   rG   r   r   r   r     s    		zBoundRows.__init__c             c   s\   | d k	 rX t  | d ƒ d k r0 t d ƒ ‚ qX x% | D] } t | d |  j ƒVq7 Wn  d S)a  
        Top and bottom pinned rows generator.

        Arguments:
            data: Iterable datas for all records for top or bottom pinned rows.

        Yields:
            BoundPinnedRow: Top or bottom BoundPinnedRow object for single pinned record.
        Nr#   Fz)The data for pinned rows must be iterabler   )ÚhasattrÚ
ValueErrorrA   r   )r   rF   Úpinned_recordr   r   r   Úgenerator_pinned_row  s
    
zBoundRows.generator_pinned_rowc             c   s‚   x( |  j  |  j j d ƒ ƒ D] } | Vq Wx% |  j D] } t | d |  j ƒVq5 Wx( |  j  |  j j d ƒ ƒ D] } | Vqo Wd  S)NÚtopr   Úbottom)rK   rG   rD   rF   r   r   )r   rJ   r   r   r   r   r#   1  s    "	"zBoundRows.__iter__c             C   s{   t  |  j ƒ } |  j j d ƒ } |  j j d ƒ } | | d  k rH d n	 t  | ƒ 7} | | d  k rj d n	 t  | ƒ 7} | S)NrL   rM   r   )ÚlenrF   rG   rD   )r   ÚlengthZ
pinned_topZpinned_bottomr   r   r   Ú__len__=  s    ""zBoundRows.__len__c             C   sV   t  | t ƒ r5 t d |  j | d |  j d |  j ƒ St d |  j | d |  j ƒ Sd S)zx
        Slicing returns a new `~.BoundRows` instance, indexing returns a single
        `~.BoundRow` instance.
        rF   r   rG   r   N)r(   ÚslicerE   rF   r   rG   r   )r   Úkeyr   r   r   Ú__getitem__E  s    	
zBoundRows.__getitem__)	r<   r=   r>   r?   r   rK   r#   rP   rS   r   r   r   r   rE     s   rE   N)Ú
__future__r   r   Z	django.dbr   Zdjango.db.models.fieldsr   Zdjango.utilsr   Zcolumns.linkcolumnr   Úutilsr	   r
   r   r   Úobjectr   rA   rE   r   r   r   r   Ú<module>   s   "ã