Exemple #1
0
 def get_default(self):
     """Returns the default value for the widget. If the default is a funtion
     that it can be called without arguments it will be called on each
     render to retrieve a value"""
     if callable_wo_args(self.default):
         value = self.default()
     else:
         value = self.default
     return value
Exemple #2
0
 def get_default(self):
     """Returns the default value for the widget. If the default is a funtion
     that it can be called without arguments it will be called on each
     render to retrieve a value"""
     if callable_wo_args(self.default):
         value = self.default()
     else:
         value = self.default
     return value
Exemple #3
0
    def update_params(self, d):
        """
        Updates the dict sent to the template for the current request.

        It is called when displaying or rendering a widget with all keyword
        arguments passed stuffed inside dict.

        Widget subclasses can call super cooperatively to avoid
        boiler-plate code as `Widget.update_params` takes care of pre-populating
        this dict with all attributes from self listed at `params`
        (copying them if mutable) and preparing arguments for child widgets.

        Any parameter sent to `display` or `render` will override those fetched
        from the instance or the class.

        Any function listed at `params` which can be called without arguments
        will be automatically called to fetch fresh results on every request.
        Parameters not found either on the class, the instance or the keyword
        args to `display` or `render` will be set to None.

        .. sourcecode:: python

            >>> class MyWidget(Widget):
            ...     params = ["foo", "bar", "null"]
            ...     foo = "foo"
            ...
            >>> w = MyWidget('test', bar=lambda: "bar")
            >>> d = {}
            >>> w.update_params(d)
            >>> d['bar']
            'bar'
            >>> d['foo']
            'foo'
            >>> d['null'] is None
            True
            >>> d = {'foo':'overriden'}
            >>> w.update_params(d)
            >>> d['foo']
            'overriden'

        """
        # Populate dict with attrs from self listed at params
        for k in ifilterfalse(d.__contains__, self.params):
            attr = getattr(self, k, None)
            if attr is not None:
                if isinstance(attr, (list, dict)):
                    attr = copy(attr)
                # Variables that are callable with no args are automatically
                # called here
                elif not isinstance(attr, Widget) and callable_wo_args(attr):
                    log.debug("Autocalling param '%s'", k)
                    attr = attr()
            d[k] = attr
Exemple #4
0
    def update_params(self, d):
        """
        Updates the dict sent to the template for the current request.

        It is called when displaying or rendering a widget with all keyword
        arguments passed stuffed inside dict.

        Widget subclasses can call super cooperatively to avoid
        boiler-plate code as `Widget.update_params` takes care of pre-populating
        this dict with all attributes from self listed at `params`
        (copying them if mutable) and preparing arguments for child widgets.

        Any parameter sent to `display` or `render` will override those fetched
        from the instance or the class.

        Any function listed at `params` which can be called without arguments
        will be automatically called to fetch fresh results on every request.
        Parameters not found either on the class, the instance or the keyword
        args to `display` or `render` will be set to None.

        .. sourcecode:: python

            >>> class MyWidget(Widget):
            ...     params = ["foo", "bar", "null"]
            ...     foo = "foo"
            ...
            >>> w = MyWidget('test', bar=lambda: "bar")
            >>> d = {}
            >>> w.update_params(d)
            >>> d['bar']
            'bar'
            >>> d['foo']
            'foo'
            >>> d['null'] is None
            True
            >>> d = {'foo':'overriden'}
            >>> w.update_params(d)
            >>> d['foo']
            'overriden'

        """
        # Populate dict with attrs from self listed at params
        for k in ifilterfalse(d.__contains__, self.params):
            attr = getattr(self, k, None)
            if attr is not None:
                if isinstance(attr, (list, dict)):
                    attr = copy(attr)
                # Variables that are callable with no args are automatically
                # called here
                elif not isinstance(attr, Widget) and callable_wo_args(attr):
                    log.debug("Autocalling param '%s'", k)
                    attr = attr()
            d[k] = attr