def test_sluggify_arabic(): # this "_" value will get replaced with something else by `autoname` ll = sluggify_label(u'مرØبا بالعالم') assert ll == '_' ll = sluggify_label(u'بالعالم') assert ll == '_'
def test_sluggify_arabic(): # this "_" value will get replaced with something else by `autoname` ll = sluggify_label('مرحبا بالعالم') assert ll == '_' ll = sluggify_label('بالعالم') assert ll == '_'
def autoname_fields_in_place(surv_content, destination_key): surv_list = surv_content.get('survey') other_names = OrderedDict() def _assign_row_to_name(row, suggested_name): if suggested_name in other_names: raise ValueError('Duplicate name error: {}'.format(suggested_name)) other_names[suggested_name] = row row[destination_key] = suggested_name # rows_needing_names is all rows needing a valid and unique name # end_group, etc. do not need valid names rows_needing_names = filter(lambda r: not _is_group_end(r), surv_list) # cycle through existing names ane ensure that names are valid and unique for row in filter(lambda r: _has_name(r), rows_needing_names): _name = row['name'] if _name in other_names: # We cannot automatically rename these because the names could be # referenced in a skip logic string. raise ValueError('Duplicate names: {}'.format(_name)) _assign_row_to_name(row, _name) for row in filter(lambda r: not _has_name(r), rows_needing_names): if 'label' in row: if isinstance(row['label'], list): # in this case, label is a list of translations. # for simplicity, we pick the first non-falsey value # to use as a basis for the generated name _label = _first_non_falsey_item(row['label']) else: _label = row['label'] if _label: _name = sluggify_label(_label, other_names=other_names.keys(), characterLimit=40) _assign_row_to_name(row, _name) continue # if no labels can be used, then use a combination of type (which is # always available) and kuid, which should always be unique _slug = row['type'] if '$kuid' in row: _slug += ('_' + row['$kuid']) _assign_row_to_name(row, sluggify_label(_slug, other_names=other_names.keys(), characterLimit=40, )) return surv_list
def adjust_content_on_save(self): """ This is called on save by default if content exists. Can be disabled / skipped by calling with parameter: asset.save(adjust_content=False) """ self._standardize(self.content) self._make_default_translation_first(self.content) self._strip_empty_rows(self.content) self._assign_kuids(self.content) self._autoname(self.content) self._unlink_list_items(self.content) self._remove_empty_expressions(self.content) settings = self.content['settings'] _title = settings.pop('form_title', None) id_string = settings.get('id_string') filename = self.summary.pop('filename', None) if filename: # if we have filename available, set the id_string # and/or form_title from the filename. if not id_string: id_string = sluggify_label(filename) settings['id_string'] = id_string if not _title: _title = filename if self.asset_type not in [ASSET_TYPE_SURVEY, ASSET_TYPE_TEMPLATE]: # instead of deleting the settings, simply clear them out self.content['settings'] = {} if _title is not None: self.name = _title
def test_sluggify_label(self): inp_exps = [ [["asdf jkl"], "asdf_jkl"], [["asdf", ["asdf"]], "asdf_001"], [["2. asdf"], "_2_asdf"], [["2. asdf", ["_2_asdf"]], "_2_asdf_001"], [["asdf#123"], "asdf_123"], [[" hello "], "hello"], # FIX THIS when we come up with a better way to summarize # arabic and cyrillic text [["أين السوق؟", ["_", "__001"]], "__002"] ] for inps, expected in inp_exps: inp = inps[0] if len(inps) > 1: other_names = inps[1] else: other_names = [] _converted = sluggify_label(inp, other_names=other_names) self.assertEqual(expected, _converted)
def test_sluggify_label(self): inp_exps = [ [["asdf jkl"], "asdf_jkl"], [["asdf", ["asdf"]], "asdf_001"], [["2. asdf"], "_2_asdf"], [["2. asdf", ["_2_asdf"]], "_2_asdf_001"], [["asdf#123"], "asdf_123"], [[" hello "], "hello"], # FIX THIS when we come up with a better way to summarize # arabic and cyrillic text [["أين السوق؟", ["_", "__001"]], "__002"] ] for (inps, expected) in inp_exps: inp = inps[0] if len(inps) > 1: other_names = inps[1] else: other_names = [] _converted = sluggify_label(inp, other_names=other_names) self.assertEqual(expected, _converted)
def autoname_fields_in_place(surv_content, destination_key): surv_list = surv_content.get('survey') other_names = OrderedDict() def _assign_row_to_name(row, suggested_name): if suggested_name in other_names: raise ValueError('Duplicate name error: {}'.format(suggested_name)) other_names[suggested_name] = row row[destination_key] = suggested_name # rows_needing_names is all rows needing a valid and unique name # end_group, etc. do not need valid names rows_needing_names = filter(lambda r: not _is_group_end(r), surv_list) # cycle through existing names ane ensure that names are valid and unique for row in filter(lambda r: _has_name(r), rows_needing_names): _name = row['name'] _attempt_count = 0 while (not is_valid_nodeName(_name) or _name in other_names): # this will be necessary for untangling skip logic row['$given_name'] = _name _name = sluggify_label(_name, other_names=other_names.keys()) # We might be able to remove these next 4 lines because # sluggify_label shouldn't be returning an empty string # and these fields already have names (_has_name(r)==True). # However, these lines were added when testing a large set # of forms so it's possible some edge cases (e.g. arabic) # still permit it if _name == '' and '$kuid' in row: _name = '{}_{}'.format(row['type'], row['$kuid']) elif _name == '': _name = row['type'] if _attempt_count > 1000: raise RuntimeError('Loop error: valid_name') _attempt_count += 1 _assign_row_to_name(row, _name) for row in filter(lambda r: not _has_name(r), rows_needing_names): if 'label' in row: if isinstance(row['label'], list): # in this case, label is a list of translations. # for simplicity, we pick the first non-falsey value # to use as a basis for the generated name _label = _first_non_falsey_item(row['label']) else: _label = row['label'] if _label: _name = sluggify_label(_label, other_names=other_names.keys(), characterLimit=40) if _name not in ['', '_']: _assign_row_to_name(row, _name) continue # if no labels can be used, then use a combination of type (which is # always available) and kuid, which should always be unique _slug = row['type'] if '$kuid' in row: _slug += ('_' + row['$kuid']) _assign_row_to_name(row, sluggify_label(_slug, other_names=other_names.keys(), characterLimit=40, )) return surv_list
def autoname_fields_in_place(surv_content, destination_key): surv_list = surv_content.get('survey') other_names = OrderedDict() def _assign_row_to_name(row, suggested_name): if suggested_name in other_names: raise ValueError('Duplicate name error: {}'.format(suggested_name)) other_names[suggested_name] = row row[destination_key] = suggested_name # rows_needing_names is all rows needing a valid and unique name # end_group, etc. do not need valid names rows_needing_names = [r for r in surv_list if not _is_group_end(r)] # cycle through existing names ane ensure that names are valid and unique for row in [r for r in rows_needing_names if _has_name(r)]: _name = row['name'] _attempt_count = 0 while not is_valid_node_name(_name) or _name in other_names: # this will be necessary for untangling skip logic row['$given_name'] = _name _name = sluggify_label(_name, other_names=list(other_names.keys())) # We might be able to remove these next 4 lines because # sluggify_label shouldn't be returning an empty string # and these fields already have names (_has_name(r)==True). # However, these lines were added when testing a large set # of forms so it's possible some edge cases (e.g. arabic) # still permit it if _name == '' and '$kuid' in row: _name = '{}_{}'.format(row['type'], row['$kuid']) elif _name == '': _name = row['type'] if _attempt_count > 1000: raise RuntimeError('Loop error: valid_name') _attempt_count += 1 _assign_row_to_name(row, _name) for row in [r for r in rows_needing_names if not _has_name(r)]: if 'label' in row: if isinstance(row['label'], list): # in this case, label is a list of translations. # for simplicity, we pick the first non-falsey value # to use as a basis for the generated name _label = _first_non_falsey_item(row['label']) else: _label = row['label'] if _label: _name = sluggify_label(_label, other_names=list(other_names.keys()), characterLimit=40) if _name not in ['', '_']: _assign_row_to_name(row, _name) continue # If no labels can be used (such as with non-Latin characters), then # use a combination of type (which is always available) and kuid, which # should always be unique. In the case of KoboRank and KoboScore, use # `select_one` as the type rather than `score__row` and `rank__level` # respectively, otherwise it does not match up with the transformed # asset content passed to kobocat. _slug = row['type'] if _slug in ['score__row', 'rank__level']: _slug = 'select_one' if '$kuid' in row: _slug += ('_' + row['$kuid']) _assign_row_to_name( row, sluggify_label( _slug, other_names=list(other_names.keys()), characterLimit=40, )) return surv_list