#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2014 uralbash <root@uralbash.ru>
#
# Distributed under terms of the MIT license.

"""
Any instruments for customizing Models
"""


def get_name(column):
    if 'verbose_name' in column.info:
        return column.info['verbose_name']
    if column.name:
        return column.name
    return ''


def widget(fun):
    def wrapped(*args, **kwargs):
        sacrud_name = ''
        if 'sacrud_name' in kwargs:
            sacrud_name = kwargs['sacrud_name']
        elif 'column' in kwargs:
            if hasattr(kwargs['column'], 'property'):
                sacrud_name = kwargs['column'].property.key
        params = {'sacrud_name': sacrud_name, 'name': sacrud_name}
        response = fun(*args, **kwargs)
        params.update(response)
        return params
    return wrapped


@widget
def widget_horizontal(*args, **kwargs):
    """
    :Examples:

    >>> widget_horizontal(sacrud_name='foo')
    {'info': {'sacrud_list_template': 'sacrud/custom/HorizontalFieldsList.jinja2', 'sacrud_position': 'inline', 'sacrud_template': 'sacrud/custom/HorizontalFieldsDetail.jinja2'}, 'sacrud_name': 'foo', 'name': '', 'horizontal_columns': ()}
    >>> widget_horizontal('a', 'b', sacrud_name='foo')
    {'info': {'sacrud_list_template': 'sacrud/custom/HorizontalFieldsList.jinja2', 'sacrud_position': 'inline', 'sacrud_template': 'sacrud/custom/HorizontalFieldsDetail.jinja2'}, 'sacrud_name': 'foo', 'name': '', 'horizontal_columns': ('a', 'b')}
    >>> widget_horizontal()
    {'info': {'sacrud_list_template': 'sacrud/custom/HorizontalFieldsList.jinja2', 'sacrud_position': 'inline', 'sacrud_template': 'sacrud/custom/HorizontalFieldsDetail.jinja2'}, 'sacrud_name': '', 'name': '', 'horizontal_columns': ()}

    """
    return {'info': {'sacrud_position': 'inline',
                     'sacrud_template': 'sacrud/custom/WidgetHorizontalDetail.jinja2',
                     'sacrud_list_template': 'sacrud/custom/WidgetHorizontalList.jinja2',
                     },
            'horizontal_columns': args,
            }


@widget
def widget_link(*args, **kwargs):
    return {'info': {'sacrud_position': 'inline',
                     'sacrud_list_template': 'sacrud/custom/WidgetLinkList.jinja2',
                     },
            'column': kwargs['column'],
            'name': get_name(kwargs['column']),
            }


@widget
def widget_m2m(*args, **kwargs):
    return {'info': {'sacrud_template': 'sacrud/custom/WidgetM2MDetail.jinja2',
                     },
            'column': kwargs['column'],
            }

@@ -458,83 +458,19 @@
     
-if 'sacrud_name' in kwargs:%0A            sacrud_name = kwargs%5B'sacrud_name'%5D
+params = %7B%7D
 %0A   
@@ -474,18 +474,16 @@
         
-el
 if 'colu
@@ -619,25 +619,170 @@
         
+    
 params
- = 
+.update(%7B'column': kwargs%5B'column'%5D%7D)%0A        if 'sacrud_name' in kwargs:%0A            sacrud_name = kwargs%5B'sacrud_name'%5D%0A        params.update(
 %7B'sacrud
@@ -822,16 +822,17 @@
 ud_name%7D
+)
 %0A       
@@ -2381,48 +2381,8 @@
  %7D,%0A
-            'column': kwargs%5B'column'%5D,%0A
     
@@ -2590,48 +2590,8 @@
  %7D,%0A
-            'column': kwargs%5B'column'%5D,%0A
     

refactor widget