3
(h                 @   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	 dd Z
G dd	 d	eZG d
d deZG dd deZG dd deZdS )    )unicode_literals)OrderedDict)FieldDoesNotExist)
smart_text)pretty_namec             C   sx   t | |}t|dr|jS t|trPt|drPt|jdrB|jjS t|jjS n$t|rp|jdkrfdS t|jS |S d S )Nshort_descriptionfgetz<lambda>z--)	getattrhasattrr   
isinstancepropertyr   r   __name__callable)owner	attr_nameattr r   G/tmp/pip-install-q3hcpn_q/django-material/material/frontend/datalist.py_get_attr_label
   s    

r   c               @   s,   e Zd ZdZdd Zdd Zedd ZdS )	
ModelFieldz[Retrive a field value from the model.

    Field verbose name would be use as a label.
    c             C   s
   || _ d S )N)field)selfr   r   r   r   __init__   s    zModelField.__init__c             C   s   t || jjS )N)r	   r   name)r   objr   r   r   	get_value"   s    zModelField.get_valuec             C   s*   y| j jS  tk
r$   | j jjjS X d S )N)r   verbose_nameAttributeErrorZrelated_model_meta)r   r   r   r   label%   s    zModelField.labelN)r   
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s   r   c               @   s.   e Zd ZdZd	ddZdd Zedd ZdS )
	ModelAttrzRetrive attribute value from the model instance.

    If model attribute is a callable, to get the value it would be
    called without any arguments.
    Nc             C   s   || _ || _|| _d S )N)modelr   _label)r   r$   r   r   r   r   r   r   5   s    zModelAttr.__init__c             C   s   t || j}t|r| S |S )N)r	   r   r   )r   r   r   r   r   r   r   :   s    zModelAttr.get_valuec             C   s   | j r| j S t| j| jS )N)r%   r   r$   r   )r   r   r   r   r   @   s    zModelAttr.label)N)r   r    r!   r"   r   r   r   r   r   r   r   r   r#   .   s   
r#   c               @   s,   e Zd ZdZdd Zdd Zedd ZdS )	DataSourceAttrzRetrieve attribute value from extenal data source.

    Data source attribute could be a property or callable.

    For a callable, to get the value it would be called with model
    instance.
    c             C   s   || _ || _d S )N)data_sourcer   )r   r'   r   r   r   r   r   P   s    zDataSourceAttr.__init__c             C   s"   t | j| j}t|r||S |S )N)r	   r'   r   r   )r   r   r   r   r   r   r   T   s    zDataSourceAttr.get_valuec             C   s   t | j| jS )a{  Retrive the label for the data source attibute.

        Label could be specifiend in `.short_description` or the
        attribue name would be used.

        Example::

        class SampleDataSource(object):
            def description(self, model):
                return "Sample for {}".format(model)
            description.short_description = "Model Description"

        )r   r'   r   )r   r   r   r   r   Z   s    zDataSourceAttr.labelN)r   r    r!   r"   r   r   r   r   r   r   r   r   r&   G   s   r&   c               @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )DataListz$Queryset to datatables data adapter.Nc             C   sJ   || _ || _|r|ng | _|r"|nd| _|r4|| _nt| jdd }dS )zInstantiate a datalist.

        :keyword data_source: List of classes that used as source to
        resole `list_display` fields values, in case if model have no
        such field.
        __str__N   )r)   )r$   querysetdata_sourceslist_displaylist_display_linkslist)r   r$   r+   r,   r-   r.   r   r   r   r   o   s    zDataList.__init__c             C   s   | j j}yt|j|S  tk
rf   |dkr>t| j ||jS x"| jD ]}t||rFt	||S qFW Y nX t
dj|| j jjdS )zData getter for an attribute.

        Data could comes from the model field or extrnal `data_source`
        method call.
        r)   zUnable to lookup '{}' on {}N)r$   r   r   	get_fieldr   r#   r   r,   r
   r&   r   formatZobject_name)r   r   optsr'   r   r   r   get_data_attr   s    
zDataList.get_data_attrc             C   s
   | j j S )zTotal dataset size.)r+   count)r   r   r   r   total   s    zDataList.totalc             C   s
   | j j S )z"Dataset size with filter appllied.)r+   r4   )r   r   r   r   total_filtered   s    zDataList.total_filteredc             C   s   dS )z:
        Add a filter to queryset.

        TODO.
        Nr   )r   r   r   r   
set_filter   s    zDataList.set_filterc             C   s   dS )z1
        Set result order.

        TODO
        Nr   )r   r   r   r   set_ordering   s    zDataList.set_orderingc             c   s*   x$| j D ]}| j|}||jfV  qW dS )zReadable column titles.N)r-   r3   r   )r   
field_namer   r   r   r   get_headers_data   s    
zDataList.get_headers_datac             c   sd   x^| j |||  D ]H}t }x2t| jD ]$\}}| j|}t|j|||< q*W ||fV  qW dS )zGet a page for datatable.N)r+   r   	enumerater-   r3   r   r   )r   startlengthitemcolumnsnr9   r   r   r   r   get_data   s    
zDataList.get_data)NNN)r   r    r!   r"   r   r3   r5   r6   r7   r8   r:   rA   r   r   r   r   r(   l   s   
r(   N)
__future__r   collectionsr   Zdjango.core.exceptionsr   Zdjango.utils.encodingr   Zdjango.forms.formsr   r   objectr   r#   r&   r(   r   r   r   r   <module>   s   %