def rename_field(key: FlattenKey, data: FlattenDataDict, errors: FlattenErrorDict, context: Context): index = max([int(k[1]) for k in data.keys() if len(k) == 3 and k[0] == new] + [-1]) for field_name in list(data.keys()): if field_name[0] == old and data.get(field_name): new_field_name = list(field_name) new_field_name[0] = new if len(new_field_name) > 1: new_field_name[1] = int(new_field_name[1]) + index + 1 data[tuple(new_field_name)] = data[field_name] data.pop(field_name)
def func(key: FlattenKey, data: FlattenDataDict, errors: FlattenErrorDict, context: Context): new_tags = data.get(key) if not new_tags: return if isinstance(new_tags, str): new_tags = [new_tags] # get current number of tags n = 0 for k in data.keys(): if k[0] == 'tags': n = max(n, k[1] + 1) v = model.Vocabulary.get(vocab) if not v: raise df.Invalid(_('Tag vocabulary "%s" does not exist') % vocab) context['vocabulary'] = v for tag in new_tags: validators.tag_in_vocabulary_validator(tag, context) for num, tag in enumerate(new_tags): data[('tags', num + n, 'name')] = tag data[('tags', num + n, 'vocabulary_id')] = v.id
def free_tags_only(key: FlattenKey, data: FlattenDataDict, errors: FlattenErrorDict, context: Context) -> Any: tag_number = key[1] if not data.get(('tags', tag_number, 'vocabulary_id')): return for k in list(data.keys()): if k[0] == 'tags' and k[1] == tag_number: del data[k]
def unflatten(data: FlattenDataDict) -> dict[str, Any]: '''Unflatten a simple dict whose keys are tuples. e.g. >>> unflatten( {('name',): u'testgrp4', ('title',): u'', ('description',): u'', ('packages', 0, 'name'): u'testpkg', ('packages', 1, 'name'): u'testpkg', ('extras', 0, 'key'): u'packages', ('extras', 0, 'value'): u'["testpkg"]', ('extras', 1, 'key'): u'', ('extras', 1, 'value'): u'', ('state',): u'active' ('save',): u'Save Changes', ('cancel',): u'Cancel'}) {'name': u'testgrp4', 'title': u'', 'description': u'', 'packages': [{'name': u'testpkg'}, {'name': u'testpkg'}], 'extras': [{'key': u'packages', 'value': u'["testpkg"]'}, {'key': u'', 'value': u''}], 'state': u'active', 'save': u'Save Changes', 'cancel': u'Cancel'} ''' unflattened: dict[str, Any] = {} clean_lists: dict[int, Any] = {} for flattend_key in sorted(data.keys(), key=flattened_order_key): current_pos: Union[list[Any], dict[str, Any]] = unflattened for key in flattend_key[:-1]: try: current_pos = current_pos[key] except IndexError: while True: new_pos: Any = {} assert isinstance(current_pos, list) current_pos.append(new_pos) if key < len(current_pos): break # skipped list indexes need to be removed before returning clean_lists[id(current_pos)] = current_pos current_pos = new_pos except KeyError: new_pos = [] current_pos[key] = new_pos current_pos = new_pos current_pos[flattend_key[-1]] = data[flattend_key] for cl in clean_lists.values(): cl[:] = [i for i in cl if i] return unflattened
def convert_to_extras(key: FlattenKey, data: FlattenDataDict, errors: FlattenErrorDict, context: Context) -> Any: # Get the current extras index current_indexes = [ k[1] for k in data.keys() if len(k) > 1 and k[0] == 'extras' ] new_index = max(current_indexes) + 1 if current_indexes else 0 data[('extras', new_index, 'key')] = key[-1] data[('extras', new_index, 'value')] = data[key]
def func(key: FlattenKey, data: FlattenDataDict, errors: FlattenErrorDict, context: Context): v = model.Vocabulary.get(vocab) if not v: raise df.Invalid(_('Tag vocabulary "%s" does not exist') % vocab) tags = [] for k in data.keys(): if k[0] == 'tags': if data[k].get('vocabulary_id') == v.id: name = data[k].get('display_name', data[k]['name']) tags.append(name) data[key] = tags
def tag_string_convert(key: FlattenKey, data: FlattenDataDict, errors: FlattenErrorDict, context: Context) -> Any: '''Takes a list of tags that is a comma-separated string (in data[key]) and parses tag names. These are added to the data dict, enumerated. They are also validated.''' if isinstance(data[key], str): tags = [tag.strip() \ for tag in data[key].split(',') \ if tag.strip()] else: tags = data[key] current_index = max( [int(k[1]) for k in data.keys() if len(k) == 3 and k[0] == 'tags'] + [-1] ) for num, tag in zip(count(current_index+1), tags): data[('tags', num, 'name')] = tag for tag in tags: tag_length_validator(tag, context) tag_name_validator(tag, context)