from django.template import RequestContext

from achilles import blocks, actions


register = actions.Library('tables')


def default_accessor(object, name):
    """
    Extract the given field from a model:

    :param object: Object to read the field from
    :param name: Field name to access on the object
    """
    return getattr(object, name)


class Column(object):
    """
    Table column, extract information from objects and render it whitin the
    table
    """
    def __init__(self, verbose_name=None,
                 accessor=default_accessor):
        """
        :param verbose_name: Column human-readable name
        :param accessor: Function to access get data from the object
        """
        self.verbose_name = verbose_name
        self.accessor = accessor

    def contribute_to_class(self, table, name):
        self.name = name
        self.verbose_name = self.verbose_name or name
        self.table = table

    def render(self, obj):
        """
        Render column value for the given object

        :param obj: Object to read the value from
        """
        return str(self.accessor(obj, self.name))


class ActionColumn(Column):
    """
    Action calling column, it will show a button that will can
    the given action on click, the action will get the the table
    and object in the row as arguments
    """
    def __init__(self, action, *args, **kwargs):
        super(ActionColumn, self).__init__(*args, **kwargs)
        self.action = action

    def render(self, obj):
        return ("<a href=\"javascript:achilles.action('tables:call_action', " +
                "['%s', '%s', '%s'])\">%s</a>") % (self.table.register_name,
                                                   self.action, obj.id,
                                                   self.verbose_name)


@register.action
def call_action(request, table, action, id):
    """
    Call table action on the given row

    :param request: The client request
    :param table: Name of the table calling this
    :param action: Name of the action to call
    :param id: Id of the object
    """
    context = RequestContext(request, {})
    table = blocks.get(table, context)
    obj = table.get_object(id)
    action = actions.get(action)
    action(request, table, obj)

from django.template import RequestContext

from achilles import blocks, actions


register = actions.Library('tables')


def default_accessor(object, name):
    """
    Extract the given field from a model:

    :param object: Object to read the field from
    :param name: Field name to access on the object
    """
    return getattr(object, name)


class Column(object):
    """
    Table column, extract information from objects and render it whitin the
    table
    """
    def __init__(self, verbose_name=None,
                 accessor=default_accessor,
                 visible=lambda x: True):
        """
        :param verbose_name: Column human-readable name
        :param accessor: Function to access get data from the object
        :param visible: Function giving visibility flag for the given row
        """
        self.verbose_name = verbose_name
        self.accessor = accessor
        self.visible = visible

    def contribute_to_class(self, table, name):
        self.name = name
        self.verbose_name = self.verbose_name or name
        self.table = table

    def render(self, obj):
        """
        Render column value for the given object

        :param obj: Object to read the value from
        """
        if self.visible(obj):
            return str(self.accessor(obj, self.name))
        else:
            return ''


class ActionColumn(Column):
    """
    Action calling column, it will show a button that will can
    the given action on click, the action will get the the table
    and object in the row as arguments
    """
    def __init__(self, action, *args, **kwargs):
        super(ActionColumn, self).__init__(*args, **kwargs)
        self.action = action

    def render(self, obj):
        return ("<a href=\"javascript:achilles.action('tables:call_action', " +
                "['%s', '%s', '%s'])\">%s</a>") % (self.table.register_name,
                                                   self.action, obj.id,
                                                   self.verbose_name)


@register.action
def call_action(request, table, action, id):
    """
    Call table action on the given row

    :param request: The client request
    :param table: Name of the table calling this
    :param action: Name of the action to call
    :param id: Id of the object
    """
    context = RequestContext(request, {})
    table = blocks.get(table, context)
    obj = table.get_object(id)
    action = actions.get(action)
    action(request, table, obj)

Add visible flag to columns