def _get_properties(self, obj): """Get indexable properties and values. Properties should be returned in the following format: { property1: { subproperty1: value1, subproperty2: value2, ... }, ... } If there is no subproperty - empty string is used as a key """ if obj.type == "Snapshot": # Snapshots do not have any indexable content. The object content for # snapshots is stored in the revision. Snapshots can also be made for # different models so we have to get fulltext attrs for the actual child # that was snapshotted and get data for those from the revision content. tgt_class = getattr(ggrc.models.all_models, obj.child_type, None) if not tgt_class: return {} attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return {attr: {"": obj.revision.content.get(attr)} for attr in attrs} return {attr: {"": getattr(obj, attr)} for attr in self._fulltext_attrs}
def _get_model_properties(): """Get indexable properties for all snapshottable objects Args: None Returns: tuple(class_properties dict, custom_attribute_definitions dict) - Tuple of dictionaries, first one representing a list of searchable attributes for every model and second one representing dictionary of custom attribute definition attributes. """ # pylint: disable=protected-access from ggrc.models import all_models class_properties = dict() klass_names = Types.all cadef_klass_names = { getattr(all_models, klass)._inflector.table_singular for klass in klass_names } cad_query = db.session.query( models.CustomAttributeDefinition.id, models.CustomAttributeDefinition.title, ).filter( models.CustomAttributeDefinition.definition_type.in_(cadef_klass_names) ) for klass_name in klass_names: model_attributes = AttributeInfo.gather_attrs( getattr(all_models, klass_name), '_fulltext_attrs') class_properties[klass_name] = model_attributes return class_properties, cad_query.all()
def get_fulltext_attrs(cls): # type: () -> Dict[unicode, fulltext.attributes.FullTextAttr] """Get all fulltext attributes represented as FullTextAttribute objects """ raw_attrs = AttributeInfo.gather_attrs(cls, '_fulltext_attrs') # Convert attrs represented as string into FullTextAttr objects attrs = [attr if isinstance(attr, fulltext.attributes.FullTextAttr) else fulltext.attributes.FullTextAttr(attr, attr) for attr in raw_attrs] return attrs
def _get_properties(self, obj): """Get indexable properties and values.""" if obj.type == "Snapshot": # Snapshots do not have any indexable content. The object content for # snapshots is stored in the revision. Snapshots can also be made for # different models so we have to get fulltext attrs for the actual child # that was snapshotted and get data for those from the revision content. tgt_class = getattr(ggrc.models.all_models, obj.child_type, None) if not tgt_class: return {} attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return {attr: obj.revision.content.get(attr) for attr in attrs} return {attr: getattr(obj, attr) for attr in self._fulltext_attrs}
def _get_properties(self, obj): """Get indexable properties and values. Properties should be returned in the following format: { property1: { subproperty1: value1, subproperty2: value2, ... }, ... } If there is no subproperty - empty string is used as a key """ if obj.type == "Snapshot": # Snapshots do not have any indexable content. The object content for # snapshots is stored in the revision. Snapshots can also be made for # different models so we have to get fulltext attrs for the actual child # that was snapshotted and get data for those from the revision content. tgt_class = getattr(ggrc.models.all_models, obj.child_type, None) if not tgt_class: return {} attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return { attr: { "": obj.revision.content.get(attr) } for attr in attrs } if isinstance(obj, Indexed): property_tmpl = obj.PROPERTY_TEMPLATE else: property_tmpl = u"{}" properties = {} for attr in self._fulltext_attrs: if isinstance(attr, basestring): properties[property_tmpl.format(attr)] = { "": getattr(obj, attr) } elif isinstance(attr, CustomRoleAttr): properties.update(attr.get_properties(obj)) elif isinstance(attr, FullTextAttr): if attr.with_template: property_name = property_tmpl.format(attr.alias) else: property_name = attr.alias properties[property_name] = attr.get_property_for(obj) return properties
def get_fulltext_parsed_value(klass, key): """Get fulltext parser if it's exists """ attrs = AttributeInfo.gather_attrs(klass, '_fulltext_attrs') if not issubclass(klass, Indexed): return for attr in attrs: if isinstance(attr, FullTextAttr) and attr.with_template: attr_key = klass.PROPERTY_TEMPLATE.format(attr.alias) elif isinstance(attr, FullTextAttr): attr_key = attr.alias else: attr_key = klass.PROPERTY_TEMPLATE.format(attr) attr = FullTextAttr(key, key) if attr_key == key: return attr
def get_fulltext_parsed_value(klass, key): """Get fulltext parser if it's exists """ attrs = AttributeInfo.gather_attrs(klass, '_fulltext_attrs') if not issubclass(klass, Indexed): return for attr in attrs: if isinstance(attr, FullTextAttr) and attr.with_template: attr_key = klass.PROPERTY_TEMPLATE.format(attr.alias) elif isinstance(attr, FullTextAttr): attr_key = attr.alias else: attr_key = klass.PROPERTY_TEMPLATE.format(attr) attr = FullTextAttr(key, key) if attr_key == key: return attr
def _get_class_properties(): """Get indexable properties for all models Args: None Returns: class_properties dict - representing a list of searchable attributes for every model """ class_properties = defaultdict(list) for klass_name in Types.all: full_text_attrs = AttributeInfo.gather_attrs( getattr(all_models, klass_name), '_fulltext_attrs') for attr in full_text_attrs: if not isinstance(attr, FullTextAttr): attr = FullTextAttr(attr, attr) class_properties[klass_name].append(attr) return class_properties
def _get_class_properties(): """Get indexable properties for all models Args: None Returns: class_properties dict - representing a list of searchable attributes for every model """ class_properties = defaultdict(list) for klass_name in Types.all: full_text_attrs = AttributeInfo.gather_attrs( getattr(all_models, klass_name), '_fulltext_attrs' ) for attr in full_text_attrs: if not isinstance(attr, FullTextAttr): attr = FullTextAttr(attr, attr) class_properties[klass_name].append(attr) return class_properties
def __table_args__(cls): # pylint: disable=no-self-argument extra_table_args = AttributeInfo.gather_attrs(cls, '_extra_table_args') table_args = [] table_dict = {} for table_arg in extra_table_args: if callable(table_arg): table_arg = table_arg() if isinstance(table_arg, (list, tuple, set)): if isinstance(table_arg[-1], (dict,)): table_dict.update(table_arg[-1]) table_args.extend(table_arg[:-1]) else: table_args.extend(table_arg) elif isinstance(table_arg, (dict,)): table_dict.update(table_arg) else: table_args.append(table_arg) if len(table_dict) > 0: table_args.append(table_dict) return tuple(table_args,)
def _get_properties(self, obj): """Get indexable properties and values. Properties should be returned in the following format: { property1: { subproperty1: value1, subproperty2: value2, ... }, ... } If there is no subproperty - empty string is used as a key """ if obj.type == "Snapshot": # Snapshots do not have any indexable content. The object content for # snapshots is stored in the revision. Snapshots can also be made for # different models so we have to get fulltext attrs for the actual child # that was snapshotted and get data for those from the revision content. tgt_class = getattr(all_models, obj.child_type, None) if not tgt_class: return {} attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return {attr: {"": obj.revision.content.get(attr)} for attr in attrs} if isinstance(obj, Indexed): property_tmpl = obj.PROPERTY_TEMPLATE else: property_tmpl = u"{}" properties = {} for attr in self._fulltext_attrs: if isinstance(attr, basestring): properties[property_tmpl.format(attr)] = {"": getattr(obj, attr)} elif isinstance(attr, FullTextAttr): properties.update(attr.get_property_for(obj)) return properties
def _get_properties(self, obj): """Get indexable properties and values. Properties should be returned in the following format: { property1: { subproperty1: value1, subproperty2: value2, ... }, ... } If there is no subproperty - empty string is used as a key """ if obj.type == "Snapshot": # Snapshots do not have any indexable content. The object content for # snapshots is stored in the revision. Snapshots can also be made for # different models so we have to get fulltext attrs for the actual child # that was snapshotted and get data for those from the revision content. tgt_class = getattr(ggrc.models.all_models, obj.child_type, None) if not tgt_class: return {} attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return { attr: { "": obj.revision.content.get(attr) } for attr in attrs } return { attr: { "": getattr(obj, attr) } for attr in self._fulltext_attrs }
def __init__(self, tgt_class): self._fulltext_attrs = AttributeInfo.gather_attrs( tgt_class, '_fulltext_attrs')
def __init__(self, tgt_class, indexer): self._fulltext_attrs = AttributeInfo.gather_attrs( tgt_class, '_fulltext_attrs') self.indexer = indexer
def model_is_indexed(tgt_class): fulltext_attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return len(fulltext_attrs) > 0
def model_is_indexed(tgt_class): fulltext_attrs = AttributeInfo.gather_attrs(tgt_class, '_fulltext_attrs') return len(fulltext_attrs) > 0