def edit_string_for_tags(tags): """ Given list of ``Tag`` instances, creates a string representation of the list suitable for editing by the user, such that submitting the given string representation back without changing it will give the same list of tags. Tag names which contain commas will be double quoted. If any tag name which isn't being quoted contains whitespace, the resulting string of tag names will be comma-delimited, otherwise it will be space-delimited. """ names = [] use_commas = False for tag in tags: name = tag.name if u',' in name: names.append('"%s"' % name) continue elif u' ' in name: if not use_commas: use_commas = True names.append(name) if use_commas: glue = u', ' else: glue = u' ' return glue.join(names)