o
    bw,                     @   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   R/var/www/secure340b-portal/env/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.
           ZoddZ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   ZcssClassr2   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   accessorr6   penultimater'   
isinstancer   ZModel	get_fieldgetattrr   resolve	Exceptionr   textr   Zempty_valuesexists)r   bound_columnrender_funcdefaultr7   r:   r6   r;   	remainderfieldZ
display_fnZ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.
        )r7   r'   r6   rC   Z	bound_rowr(   )r'   r6   r"   r   rC   r7   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.
        rD   rE   )r(   columnsrH   _call_renderrE   )r   r   rC   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
        )rJ   r	   renderlink)r   rC   r7   Zrender_kwargscontentr   r   r   rM      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.
        NrK   )rH   r(   rL   _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	   r7   rJ   rI   r   r   r   rQ      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(   rL   )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(   rL   r   r   current_valuer'   Zcurrent_record)r   r6   r   r   r   r5      s   zBoundRow.itemsr   )r   r   r   r   r   propertyr(   r-   r4   r'   r8   rH   rJ   r   rM   rR   rQ   rU   r5   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'   r.   r1   z
pinned-rowr0    )r
   r"   Zpinned_row_attrsr!   joinr-   getr   )r   r2   Z	css_classr   r   r   r4      s   	zBoundPinnedRow.attrsN)r   r   r   r   rW   r4   r   r   r   r   rX      s    rX   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   r^   r(   r_   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.
        Nr8   Fz)The data for pinned rows must be iterabler(   )hasattr
ValueErrorrX   r(   )r   r^   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topr`   bottom)rd   r_   r[   r^   r    r(   )r   rc   r'   r   r   r   r8   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 )Nre   rf   r   )lenr^   r_   r[   )r   lengthZ
pinned_topZ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.
        r]   )r'   r(   )r<   slicer\   r^   r(   r_   r    r   r   r   r   r   L  s   
zBoundRows.__getitem__r   )	r   r   r   r   r   rd   r8   ri   r   r   r   r   r   r\     s    
r\   N)Zdjango.core.exceptionsr   Z	django.dbr   Zcolumns.linkcolumnr   Zcolumns.manytomanycolumnr   utilsr   r   r	   r
   r   r    rX   r\   r   r   r   r   <module>   s     b