o
    ˷ew,                     @   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	m
Z
mZmZ G dd dZG d	d
 d
ZG dd deZG dd dZdS )    )FieldDoesNotExist)models   )BaseLinkColumn)ManyToManyColumn)AAttributeDictcall_with_appropriatecomputed_valuesc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	CellAccessorzI
    Allows accessing cell contents on a row object (see `BoundRow`)
    c                 C   s
   || _ d S N)row)selfr    r   J/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/rows.py__init__   s   
zCellAccessor.__init__c                 C      | j |S r   r   get_cellr   keyr   r   r   __getitem__      zCellAccessor.__getitem__c                 C   r   r   r   r   namer   r   r   __getattr__   r   zCellAccessor.__getattr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   	   s
    r   c                   @   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 ) BoundRoway  
    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.cells[0] to retrieve a specific cell::

        >>> row.cells[0]
        1
        >>> row.cells[1]
        '<input type="checkbox" name="my_chkbox" value="2" />'
        >>> row.cells[2]
        ...
        IndexError: list index out of range

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

        >>> row.cells.a
        1
        >>> row.cells.b
        '<input type="checkbox" name="my_chkbox" value="2" />'
        >>> row.cells.c
        ...
        KeyError: "Column with name 'c' does not exist; choices are: ['a', 'b']"

    If you have the column name in a variable, you can also treat the `cells`
    property like a `dict`::

        >>> key = 'a'
        >>> row.cells[key]
        1

    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| _t| | _d S r   )_record_tablenext_counterrow_counterr   cells)r   recordtabler   r   r   r   S   s   zBoundRow.__init__c                 C      | j S )z!The `.Table` this row is part of.)r"   r   r   r   r   r(   \      zBoundRow.tablec                 C   s   | j d rdS dS )z
        Return css class, alternating for odd and even records.

        Return:
            string: `even` for even records, `odd` otherwise.
           oddeven)r%   r*   r   r   r   get_even_odd_css_classa   s   zBoundRow.get_even_odd_css_classc                 C   s`   |   }t| jjt| j| jdd}d|v r(|d r(|d  d| 7  < t|S ||d< t|S )z(Return the attributes for a certain row.)r(   r'   kwargsclass )r/   r
   r"   	row_attrsdictr!   r   )r   cssClassr4   r   r   r   attrsj   s   zBoundRow.attrsc                 C   r)   )zRThe data record from the data source which is used to populate this row with data.)r!   r*   r   r   r   r'   z   r+   zBoundRow.recordc                 c   s    |   D ]\}}|V  qd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__   s   zBoundRow.__iter__c                 C   s   d }t |j}|j}|| j\}}t|tjr?z|| j}	t	|d| d }
t	|	ddr4|
r4|
 }d }W n	 t
y>   Y nw |rcz|| j}W n tyb   t|tr`|jd ur`|| Y S Y nw t|t}||jv ss|ru| su|S |||S )Nzget_%s_displaychoicesr   )r   accessorr9   penultimater'   
isinstancer   Model	get_fieldgetattrr   resolve	Exceptionr   textr   empty_valuesexists)r   bound_columnrender_funcdefaultr:   r=   r9   r>   	remainderfield
display_fnis_manytomanycolumnr   r   r   _get_and_render_with   s6   


zBoundRow._get_and_render_withc                 C   s   || j |j|| | jdS )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'   r9   rH   	bound_rowr(   )r'   r9   r"   r   rH   r:   r   r   r   _optional_cell_arguments   s   z!BoundRow._optional_cell_argumentsc                 C   s    | j j| }| j|| j|jdS )zl
        Returns the final rendered html for a cell in the row, given the name
        of a column.
        rI   rJ   )r(   columnsrO   _call_renderrJ   )r   r   rH   r   r   r   r      s   
zBoundRow.get_cellNc                 C   s4   |  ||}t|j|}|jr|j|fi |S |S )zI
        Call the column's render method with appropriate kwargs
        )rR   r	   renderlink)r   rH   r:   render_kwargscontentr   r   r   rU      s   zBoundRow._call_renderc                 C   s   | j | jj| | 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.
        NrS   )rO   r(   rT   _call_valuer   r   r   r   get_cell_value   s   zBoundRow.get_cell_valuec                 C   s   t |j| ||S )zH
        Call the column's value method with appropriate kwargs
        )r	   r:   rR   rQ   r   r   r   rZ      s   zBoundRow._call_valuec                 C   s   |t |tr| jjv S | v S )z;
        Check by both row object and column name.
        )r?   strr(   rT   )r   itemr   r   r   __contains__   s   zBoundRow.__contains__c                 c   s6    | j jD ]}| |j|_| j|_||jfV  qd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(   rT   r   r   current_valuer'   current_record)r   r9   r   r   r   r8      s   zBoundRow.itemsr   )r   r   r   r   r   propertyr(   r/   r7   r'   r;   rO   rR   r   rU   r[   rZ   r^   r8   r   r   r   r   r       s&    :	
	

$
	
	r    c                   @   s   e Zd ZdZedd ZdS )BoundPinnedRowz/
    Represents a *pinned* row in a table.
    c                 C   sB   t | jjd| jid}d|  d|ddg}||d< t|S )z
        Return the attributes for a certain pinned row.
        Add CSS classes `pinned-row` and `odd` or `even` to `class` attribute.

        Return:
            AttributeDict: Attributes for pinned rows.
        r'   r0   r3   z
pinned-rowr2    )r
   r"   pinned_row_attrsr!   joinr/   getr   )r   r4   	css_classr   r   r   r7      s   	zBoundPinnedRow.attrsN)r   r   r   r   ra   r7   r   r   r   r   rb      s    rb   c                   @   s:   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 r   datar(   pinned_data)r   rj   r(   rk   r   r   r   r   "  s   zBoundRows.__init__c                 c   sB    |durt |ddu rtd|D ]}t|| jdV  qdS dS )a  
        Top and bottom pinned rows generator.

        Arguments:
            data: Iterable data 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 iterabler(   )hasattr
ValueErrorrb   r(   )r   rj   pinned_recordr   r   r   generator_pinned_row'  s   
zBoundRows.generator_pinned_rowc                 c   s^    |  | jdD ]}|V  q
| jD ]
}t|| jdV  q|  | jdD ]}|V  q'd S )Ntoprl   bottom)rp   rk   rf   rj   r    r(   )r   ro   r'   r   r   r   r;   8  s   
zBoundRows.__iter__c                 C   s\   t | j}| jd}| jd}||d u rdnt |7 }||d u r'd7 }|S t |7 }|S )Nrq   rr   r   )lenrj   rk   rf   )r   length
pinned_toppinned_bottomr   r   r   __len__D  s   

zBoundRows.__len__c                 C   s6   t |trt| j| | j| jdS t| j| | jdS )zx
        Slicing returns a new `~.BoundRows` instance, indexing returns a single
        `~.BoundRow` instance.
        ri   )r'   r(   )r?   slicerh   rj   r(   rk   r    r   r   r   r   r   L  s   
zBoundRows.__getitem__r   )	r   r   r   r   r   rp   r;   rw   r   r   r   r   r   rh     s    
rh   N)django.core.exceptionsr   	django.dbr   columns.linkcolumnr   columns.manytomanycolumnr   utilsr   r   r	   r
   r   r    rb   rh   r   r   r   r   <module>   s     b