本文整理汇总了Python中ggrc.models.reflection.AttributeInfo.gather_aliases方法的典型用法代码示例。如果您正苦于以下问题:Python AttributeInfo.gather_aliases方法的具体用法?Python AttributeInfo.gather_aliases怎么用?Python AttributeInfo.gather_aliases使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ggrc.models.reflection.AttributeInfo
的用法示例。
在下文中一共展示了AttributeInfo.gather_aliases方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_reserved_names
# 需要导入模块: from ggrc.models.reflection import AttributeInfo [as 别名]
# 或者: from ggrc.models.reflection.AttributeInfo import gather_aliases [as 别名]
def _get_reserved_names(cls, definition_type):
"""Get a list of all attribute names in all objects.
On first call this function computes all possible names that can be used by
any model and stores them in a static frozen set. All later calls just get
this set.
Returns:
frozen set containing all reserved attribute names for the current
object.
"""
# pylint: disable=protected-access
# The _inflector is a false positive in our app.
with benchmark("Generate a list of all reserved attribute names"):
if not cls._reserved_names.get(definition_type):
definition_map = {model._inflector.table_singular: model
for model in ggrc.models.all_models.all_models}
definition_map.update({model._inflector.model_singular: model
for model in ggrc.models.all_models.all_models})
definition_model = definition_map.get(definition_type)
if not definition_model:
raise ValueError("Invalid definition type")
aliases = AttributeInfo.gather_aliases(definition_model)
cls._reserved_names[definition_type] = frozenset(
(value["display_name"] if isinstance(
value, dict) else value).lower()
for value in aliases.values() if value
)
return cls._reserved_names[definition_type]
示例2: _set_attr_name_map
# 需要导入模块: from ggrc.models.reflection import AttributeInfo [as 别名]
# 或者: from ggrc.models.reflection.AttributeInfo import gather_aliases [as 别名]
def _set_attr_name_map(self):
""" build a map for attributes names and display names
Dict containing all display_name to attr_name mappings
for all objects used in the current query
Example:
{ Program: {"Program URL": "url", "Code": "slug", ...} ...}
"""
self.attr_name_map = {}
for object_query in self.query:
object_name = object_query["object_name"]
object_class = self.object_map[object_name]
tgt_class = object_class
if object_name == "Snapshot":
child_type = self._get_snapshot_child_type(object_query)
tgt_class = getattr(models.all_models, child_type, object_class)
aliases = AttributeInfo.gather_aliases(tgt_class)
self.attr_name_map[tgt_class] = {}
for key, value in aliases.items():
filter_by = None
if isinstance(value, dict):
filter_name = value.get("filter_by", None)
if filter_name is not None:
filter_by = getattr(tgt_class, filter_name, None)
name = value["display_name"]
else:
name = value
if name:
self.attr_name_map[tgt_class][name.lower()] = (key.lower(),
filter_by)
示例3: test_gather_aliases
# 需要导入模块: from ggrc.models.reflection import AttributeInfo [as 别名]
# 或者: from ggrc.models.reflection.AttributeInfo import gather_aliases [as 别名]
def test_gather_aliases(self):
"""Test gather all aliases."""
class Child(object):
# pylint: disable=too-few-public-methods
_aliases = {
"child_normal": "normal",
"child_extended": {
"display_name": "Extended",
},
"child_filter_only": {
"display_name": "Extended",
"filter_only": True,
},
}
class Parent(Child):
# pylint: disable=too-few-public-methods
_aliases = {
"parent_normal": "normal",
"parent_extended": {
"display_name": "Extended",
},
"parent_filter_only": {
"display_name": "Extended",
"filter_only": True,
},
}
self.assertEqual(
AttributeInfo.gather_aliases(Parent),
{
"parent_normal": "normal",
"parent_extended": {
"display_name": "Extended",
},
"parent_filter_only": {
"display_name": "Extended",
"filter_only": True,
},
"child_normal": "normal",
"child_extended": {
"display_name": "Extended",
},
"child_filter_only": {
"display_name": "Extended",
"filter_only": True,
},
}
)
示例4: _get_model_names
# 需要导入模块: from ggrc.models.reflection import AttributeInfo [as 别名]
# 或者: from ggrc.models.reflection.AttributeInfo import gather_aliases [as 别名]
def _get_model_names(cls, model):
"""Get tuple of all attribute names for model.
Args:
model: Model class.
Returns:
Tuple of all attributes for provided model.
"""
if not model:
raise ValueError("Invalid definition type")
aliases = AttributeInfo.gather_aliases(model)
return (
(value["display_name"] if isinstance(value, dict) else value).lower()
for value in aliases.values() if value
)
示例5: attributes_map
# 需要导入模块: from ggrc.models.reflection import AttributeInfo [as 别名]
# 或者: from ggrc.models.reflection.AttributeInfo import gather_aliases [as 别名]
def attributes_map(cls):
if cls.CACHED_ATTRIBUTE_MAP:
return cls.CACHED_ATTRIBUTE_MAP
aliases = AttributeInfo.gather_aliases(cls)
cls.CACHED_ATTRIBUTE_MAP = {}
for key, value in aliases.items():
if isinstance(value, dict):
name = value["display_name"]
filter_by = None
if value.get("filter_by"):
filter_by = getattr(cls, value["filter_by"], None)
else:
name = value
filter_by = None
if not name:
continue
tmp = getattr(cls, "PROPERTY_TEMPLATE", "{}")
name = tmp.format(name)
key = tmp.format(key)
cls.CACHED_ATTRIBUTE_MAP[name.lower()] = (key.lower(), filter_by)
return cls.CACHED_ATTRIBUTE_MAP