| @@ 132-183 (lines=52) @@ | ||
| 129 | ||
| 130 | return attributes |
|
| 131 | ||
| 132 | def lookup(self, name, context, field, default=None): |
|
| 133 | """Check if the context has an override for the given named property |
|
| 134 | ||
| 135 | The context can either define an attribute or a method with the |
|
| 136 | following naming convention (all lower case): |
|
| 137 | ||
| 138 | get_wiget_<fieldname>_<propertyname> |
|
| 139 | ||
| 140 | If an attribute or method is found, this value will be returned, |
|
| 141 | otherwise the lookup will return the default value |
|
| 142 | ||
| 143 | :param name: The name of a method to lookup |
|
| 144 | :param context: The current context of the field |
|
| 145 | :param field: The current field of the widget |
|
| 146 | :param default: The default property value for the given name |
|
| 147 | :returns: New value for the named property |
|
| 148 | """ |
|
| 149 | ||
| 150 | # check if the current context defines an attribute or a method for the |
|
| 151 | # given property following our naming convention |
|
| 152 | context_key = "get_widget_{}_{}".format(field.getName(), name).lower() |
|
| 153 | if base_hasattr(context, context_key): |
|
| 154 | attr = getattr(context, context_key, default) |
|
| 155 | if callable(attr): |
|
| 156 | # call the context method with additional information |
|
| 157 | attr = attr(name=name, |
|
| 158 | widget=self, |
|
| 159 | field=field, |
|
| 160 | context=context, |
|
| 161 | default=default) |
|
| 162 | return attr |
|
| 163 | ||
| 164 | # Allow named methods for query/columns |
|
| 165 | if name in ["query", "columns"]: |
|
| 166 | value = getattr(self, name, None) |
|
| 167 | # allow named methods from the context class |
|
| 168 | if api.is_string(value): |
|
| 169 | method = getattr(context, value, None) |
|
| 170 | if callable(method): |
|
| 171 | return method() |
|
| 172 | # allow function objects directly |
|
| 173 | if callable(value): |
|
| 174 | return value() |
|
| 175 | ||
| 176 | # Call custom getter from the widget class |
|
| 177 | getter = "get_{}".format(name) |
|
| 178 | method = getattr(self, getter, None) |
|
| 179 | if callable(method): |
|
| 180 | return method(context, field, default=default) |
|
| 181 | ||
| 182 | # return the widget attribute |
|
| 183 | return getattr(self, name, default) |
|
| 184 | ||
| 185 | def get_api_url(self, context, field, default=None): |
|
| 186 | """JSON API URL to use for this widget |
|
| @@ 130-181 (lines=52) @@ | ||
| 127 | ||
| 128 | return attributes |
|
| 129 | ||
| 130 | def lookup(self, name, context, field, default=None): |
|
| 131 | """Check if the context has an override for the given named property |
|
| 132 | ||
| 133 | The context can either define an attribute or a method with the |
|
| 134 | following naming convention (all lower case): |
|
| 135 | ||
| 136 | get_widget_<fieldname>_<propertyname> |
|
| 137 | ||
| 138 | If an attribute or method is found, this value will be returned, |
|
| 139 | otherwise the lookup will return the default value |
|
| 140 | ||
| 141 | :param name: The name of a method to lookup |
|
| 142 | :param context: The current context of the field |
|
| 143 | :param field: The current field of the widget |
|
| 144 | :param default: The default property value for the given name |
|
| 145 | :returns: New value for the named property |
|
| 146 | """ |
|
| 147 | ||
| 148 | # check if the current context defines an attribute or a method for the |
|
| 149 | # given property following our naming convention |
|
| 150 | context_key = "get_widget_{}_{}".format(field.getName(), name).lower() |
|
| 151 | if base_hasattr(context, context_key): |
|
| 152 | attr = getattr(context, context_key, default) |
|
| 153 | if callable(attr): |
|
| 154 | # call the context method with additional information |
|
| 155 | attr = attr(name=name, |
|
| 156 | widget=self, |
|
| 157 | field=field, |
|
| 158 | context=context, |
|
| 159 | default=default) |
|
| 160 | return attr |
|
| 161 | ||
| 162 | # Allow named methods for query/columns |
|
| 163 | if name in ["query", "columns"]: |
|
| 164 | value = getattr(self, name, None) |
|
| 165 | # allow named methods from the context class |
|
| 166 | if api.is_string(value): |
|
| 167 | method = getattr(context, value, None) |
|
| 168 | if callable(method): |
|
| 169 | return method() |
|
| 170 | # allow function objects directly |
|
| 171 | if callable(value): |
|
| 172 | return value() |
|
| 173 | ||
| 174 | # Call custom getter from the widget class |
|
| 175 | getter = "get_{}".format(name) |
|
| 176 | method = getattr(self, getter, None) |
|
| 177 | if callable(method): |
|
| 178 | return method(context, field, default=default) |
|
| 179 | ||
| 180 | # return the widget attribute |
|
| 181 | return getattr(self, name, default) |
|
| 182 | ||
| 183 | def get_api_url(self, context, field, default=None): |
|
| 184 | """JSON API URL to use for this widget |
|