| |
@@ -28,6 +28,7 @@
|
| |
import operator
|
| |
import os
|
| |
import random
|
| |
+ from collections import defaultdict
|
| |
|
| |
import bleach
|
| |
import sqlalchemy as sa
|
| |
@@ -346,12 +347,18 @@
|
| |
return 12 - self.left_width
|
| |
|
| |
|
| |
- def _widget_config_default(context):
|
| |
- widget_name = context.current_parameters['plugin']
|
| |
- widget = hubs.widgets.registry[widget_name]
|
| |
- return json.dumps(dict([
|
| |
- (param.name, param.default) for param in widget.get_parameters()
|
| |
- ]))
|
| |
+ class SpecificDefaultDict(defaultdict):
|
| |
+ """A more specific version of defaultdict.
|
| |
+
|
| |
+ This class behaves like defaultdict, but calls the ``default_factory`` with
|
| |
+ the key as first argument.
|
| |
+ """
|
| |
+
|
| |
+ def __missing__(self, key):
|
| |
+ if self.default_factory is None:
|
| |
+ return super(SpecificDefaultDict, self).__missing__(key)
|
| |
+ self[key] = self.default_factory(key)
|
| |
+ return self[key]
|
| |
|
| |
|
| |
class Widget(ObjectAuthzMixin, BASE):
|
| |
@@ -364,7 +371,7 @@
|
| |
plugin = sa.Column(sa.String(50), nullable=False)
|
| |
created_on = sa.Column(sa.DateTime, default=datetime.datetime.utcnow)
|
| |
hub_id = sa.Column(sa.String(50), sa.ForeignKey('hubs.name'))
|
| |
- _config = sa.Column(sa.Text, default=_widget_config_default)
|
| |
+ _config = sa.Column(sa.Text, default="{}")
|
| |
|
| |
index = sa.Column(sa.Integer, nullable=False)
|
| |
left = sa.Column(sa.Boolean, nullable=False, default=False)
|
| |
@@ -388,7 +395,17 @@
|
| |
|
| |
@property
|
| |
def config(self):
|
| |
- return json.loads(self._config)
|
| |
+ def get_default(key):
|
| |
+ for param in self.module.get_parameters():
|
| |
+ if key == param.name:
|
| |
+ break
|
| |
+ else:
|
| |
+ raise KeyError("No such parameter")
|
| |
+ return param.default
|
| |
+
|
| |
+ value = SpecificDefaultDict(get_default)
|
| |
+ value.update(json.loads(self._config))
|
| |
+ return value
|
| |
|
| |
@config.setter
|
| |
def config(self, config):
|
| |
:thumbsup: