o
    ˷e@                     @   sV   d dl mZ d dlmZ d dlmZmZ ddlmZm	Z	m
Z
 e
jG dd deZdS )	    )models	force_str)conditional_escape	mark_safe   )ColumnLinkTransformlibraryc                       sH   e Zd ZdZ	d fdd	Zdd Zdd	 Zd
d Zedd Z	  Z
S )ManyToManyColumna  
    Display the list of objects from a `ManyRelatedManager`

    Ordering is disabled for this column.

    Arguments:
        transform: callable to transform each item to text, it gets an item as argument
            and must return a string-like representation of the item.
            By default, it calls `~django.utils.force_str` on each item.
        filter: callable to filter, limit or order the QuerySet, it gets the
            `ManyRelatedManager` as first argument and must return a filtered QuerySet.
            By default, it returns `all()`
        separator: separator string to join the items with. default: ``", "``
        linkify_item: callable, arguments to reverse() or `True` to wrap items in a ``<a>`` tag.
            For a detailed explanation, see ``linkify`` argument to ``Column``.

    For example, when displaying a list of friends with their full name::

        # models.py
        class Person(models.Model):
            first_name = models.CharField(max_length=200)
            last_name = models.CharField(max_length=200)
            friends = models.ManyToManyField(Person)
            is_active = models.BooleanField(default=True)

            @property
            def name(self):
                return '{} {}'.format(self.first_name, self.last_name)

        # tables.py
        class PersonTable(tables.Table):
            name = tables.Column(order_by=("last_name", "first_name"))
            friends = tables.ManyToManyColumn(transform=lambda user: u.name)

    If only the active friends should be displayed, you can use the `filter` argument::

        friends = tables.ManyToManyColumn(filter=lambda qs: qs.filter(is_active=True))

    N, c                    s   | dd t j|i | |d ur|| _|d ur|| _|| _d }t|r,t|d}nt|tt	fr9t|d}n|du r@t }|d urUt
dd| jdi i|| _d S d S )	N	orderableF)url)reverse_argsTattrsa )
setdefaultsuper__init__	transformfilter	separatorcallabledict
isinstancetupler	   r   getlinkify_item)selfr   r   r   r   argskwargslink_kwargs	__class__r   ^/var/www/ideatree/venv/lib/python3.10/site-packages/django_tables2/columns/manytomanycolumn.pyr   2   s"   "zManyToManyColumn.__init__c                 C   s   t |S )zh
        Transform is applied to each item of the list of objects from the ManyToMany relation.
        r   )r   objr   r   r%   r   I   s   zManyToManyColumn.transformc                 C   s   |  S )z
        Filter is called on the ManyRelatedManager to allow ordering, filtering or limiting
        on the set of related objects.
        )all)r   qsr   r   r%   r   O   s   zManyToManyColumn.filterc                 C   sX   g }|  |D ]}t| |}t| dr| j||d}|| qtt| j|S )Nr   )contentrecord)	r   r   r   hasattrr   appendr   r   join)r   valueitemsitemr)   r   r   r%   renderV   s   
zManyToManyColumn.renderc                 K   s   t |tjr| di |S d S )Nr   )r   r   ManyToManyField)clsfieldr!   r   r   r%   
from_fielda   s   zManyToManyColumn.from_field)NNr   N)__name__
__module____qualname____doc__r   r   r   r1   classmethodr5   __classcell__r   r   r#   r%   r      s    )r   N)	django.dbr   django.utils.encodingr   django.utils.htmlr   r   baser   r	   r
   registerr   r   r   r   r%   <module>   s    