Add Tag.normalize_name to be called by PDB.normalize_tagname.

This commit is contained in:
voussoir 2018-04-20 15:28:27 -07:00
parent bdec6cf4a3
commit fec5eaf21e
2 changed files with 27 additions and 21 deletions

View file

@ -1207,6 +1207,26 @@ class Tag(ObjectBase, GroupableMixin):
return description
@staticmethod
def normalize_name(name, valid_chars=None, min_length=None, max_length=None):
original_name = name
if valid_chars is None:
valid_chars = constants.DEFAULT_CONFIG['tag']['valid_chars']
name = name.lower()
name = name.replace('-', '_')
name = name.replace(' ', '_')
name = (c for c in name if c in valid_chars)
name = ''.join(name)
if min_length is not None and len(name) < min_length:
raise exceptions.TagTooShort(original_name)
if max_length is not None and len(name) > max_length:
raise exceptions.TagTooLong(name)
return name
def _uncache(self):
self.photodb.caches['tag'].remove(self.id)
self._cached_qualified_name = None

View file

@ -892,26 +892,12 @@ class PDBTagMixin:
return tag
def normalize_tagname(self, tagname):
'''
Tag names can only consist of characters defined in the config.
The given tagname is lowercased, gets its spaces and hyphens
replaced by underscores, and is stripped of any not-whitelisted
characters.
'''
original_tagname = tagname
tagname = tagname.lower()
tagname = tagname.replace('-', '_')
tagname = tagname.replace(' ', '_')
tagname = (c for c in tagname if c in self.config['tag']['valid_chars'])
tagname = ''.join(tagname)
if len(tagname) < self.config['tag']['min_length']:
raise exceptions.TagTooShort(original_tagname)
elif len(tagname) > self.config['tag']['max_length']:
raise exceptions.TagTooLong(tagname)
else:
tagname = objects.Tag.normalize_name(
tagname,
valid_chars=self.config['tag']['valid_chars'],
min_length=self.config['tag']['min_length'],
max_length=self.config['tag']['max_length'],
)
return tagname