def get_collection_json_attributes(collection): attrs = collection["attributes"] return { key: value for key, value in attrs.items() if issubclass(get_gob_type_from_info(value), gob_types.JSON) }
def _to_gob_value(entity, field, spec, resolve_secure=False): """ Transforms a entity field value into a GOB type value Attention: Resolve secure is normally False as this is all handled by the Authority classes For enhanced views however, this is not possible. These queries are based upon a view and cannot directly be related to a GOB Model If the field names of the view are properly named the decryption will be handled here :param entity: :param field: :param spec: :param resolve_secure: :return: """ entity_value = getattr(entity, field, None) if isinstance(spec, dict): gob_type = get_gob_type_from_info(spec) if resolve_secure and Authority.is_secure_type(spec): # Transform the value into a secure type value secure_type = Authority.get_secure_type(gob_type, spec, entity_value) # Return decrypted value return Authority.get_secured_value(secure_type) return gob_type.from_value(entity_value, **spec) else: gob_type = get_gob_type_from_sql_type(spec) return gob_type.from_value(entity_value)
def _extract_field(row, field, metadata, typeinfo, entity_id_field=None, seqnr_field=None): """ Extract a field from a row given the corresponding metadata :param row: the data row :param metadata: the mapping definition :param typeinfo: the GOB model info :return: the string value of a field specified by the field's metadata, based on the values in row """ field_type = typeinfo['type'] field_source = metadata['source_mapping'] gob_type = get_gob_type_from_info(typeinfo) kwargs = { k: v for k, v in metadata.items() if k not in ['type', 'source_mapping', 'filters'] } if isinstance(field_source, dict): value = _extract_references(row, field_source, field_type, metadata.get('force_list', False)) else: value = _get_value(row, field_source) # Clean all references if field_type in ('GOB.Reference', 'GOB.ManyReference') and value is not None: value = _clean_references(value) value = _apply_field_filters(metadata, value) try: return gob_type.from_value_secure(value, typeinfo, **kwargs) except GOBTypeException: # Convert the raw source row into a GOB-like row report_row = _goblike_row(row, entity_id_field, seqnr_field) report_row[field] = value id = report_row[FIELD.ID] + (f".{report_row[FIELD.SEQNR]}" if seqnr_field else "") logger.error( f"Error importing object with id {id}. Can't extract value for field {field}" ) return gob_type.from_value_secure(None, typeinfo, **kwargs)
def _create_reference_view(entity, field, spec): # Get the dict or array of dicts from a (Many)Reference field embedded = _to_gob_value(entity, field, spec).to_db if embedded is not None and spec['ref'] is not None: catalog, collection = spec['ref'].split(':') if spec['type'] == 'GOB.ManyReference': embedded = [_format_reference(reference, catalog, collection, {}) for reference in embedded] else: ref = _format_reference(embedded, catalog, collection, {}) gob_type = get_gob_type_from_info(spec) embedded = gob_type.from_value(ref, secure=spec.get('secure')) return embedded
def _format_reference(reference, catalog, collection, spec): link = _create_reference_link(reference, catalog, collection) if spec.get('secure', {}).get(FIELD.SOURCE_VALUE): # Original bronwaarde was secured, decrypt if possible gob_type = get_gob_type_from_info(spec) # Actual input to from_value is not the original value, but it is a dict with a bronwaarde key, so the # bronwaarde key is decrypted as the original input would have been. reference = gob_type.from_value(reference, secure=spec.get('secure')).to_value return { **reference, **link, }
def is_secure_type(cls, spec): """ Tells if spec is a secure type Either a plain secure type or a JSON that contains a secure type :param spec: :return: """ gob_type = get_gob_type_from_info(spec) if issubclass(gob_type, gob_secure_types.Secure): return True elif issubclass(gob_type, gob_types.JSON): attributes = { **spec.get('attributes', {}), **spec.get('secure', {}) } return any( [cls.is_secure_type(attr) for attr in attributes.values()])
def get_secured_columns(self): """ The secured columns are the columns that (may) require decryption """ if not self._secured_columns: collection = GOBModel().get_collection(self._catalog, self._collection) if collection: cols = { column: { 'gob_type': get_gob_type_from_info(spec), 'spec': spec } for column, spec in collection['fields'].items() if self.is_secure_type(spec) } else: cols = {} self._secured_columns = cols return self._secured_columns
def build_event(self, tid: str) -> dict: query = self.db_session.query( self.basetable).filter(self.basetable._tid == tid) obj = query.one() result = {} for attr_name, attr in self.collection['attributes'].items(): if 'Reference' in attr['type']: relation = self.relations[attr_name] relation_table_rows = getattr( obj, f"{relation['relation_table_name']}_collection") relation_obj = [] for row in relation_table_rows: dst_table = getattr(row, relation['dst_table_name']) rel = { 'tid': dst_table._tid, 'id': dst_table._id, 'begin_geldigheid': str(row.begin_geldigheid) if row.begin_geldigheid else None, 'eind_geldigheid': str(row.eind_geldigheid) if row.eind_geldigheid else None, } if hasattr(dst_table, 'volgnummer'): rel['volgnummer'] = dst_table.volgnummer relation_obj.append(rel) result[attr_name] = relation_obj else: type = get_gob_type_from_info(attr) type_instance = type.from_value(getattr(obj, attr_name)) result[attr_name] = str(type_instance.to_value) return result
def test_gob_type_from_info(self): type_info = {'type': "GOB.String"} self.assertEqual(get_gob_type_from_info(type_info), GOB.String)