Merge pull request #2092 from trhr/validators

Validators and normalization of character names
This commit is contained in:
Griatch 2020-04-10 09:00:16 +02:00 committed by GitHub
commit 2dabcb45e6
2 changed files with 44 additions and 0 deletions

View file

@ -2063,6 +2063,13 @@ class DefaultCharacter(DefaultObject):
# If no typeclass supplied, use this class
kwargs["typeclass"] = kwargs.pop("typeclass", cls)
# Normalize to latin characters and validate, if necessary, the supplied key
key = cls.normalize_name(key)
if not cls.validate_name(key):
errors.append("Invalid character name.")
return obj, errors
# Set the supplied key as the name of the intended object
kwargs["key"] = key
@ -2112,6 +2119,37 @@ class DefaultCharacter(DefaultObject):
return obj, errors
@classmethod
def normalize_name(cls, name):
"""
Normalize the character name prior to creating. Note that this should be refactored
to support i18n for non-latin scripts, but as we (currently) have no bug reports requesting better
support of non-latin character sets, requiring character names to be latinified is an acceptable option.
Args:
name (str) : The name of the character
Returns:
latin_name (str) : A valid name.
"""
from evennia.utils.utils import latinify
latin_name = latinify(name, default="X")
return latin_name
@classmethod
def validate_name(cls, name):
""" Validate the character name prior to creating. Overload this function to add custom validators
Args:
name (str) : The name of the character
Returns:
valid (bool) : True if character creation should continue; False if it should fail
"""
return True # Default validator does not perform any operations
def basetype_setup(self):
"""
Setup character-specific security.

View file

@ -39,6 +39,12 @@ class DefaultObjectTest(EvenniaTest):
self.assertFalse(errors, errors)
self.assertEqual(obj.db_home, self.room1)
def test_character_create_weirdname(self):
obj, errors = DefaultCharacter.create("SigurðurÞórarinsson", self.account, home=self.room1.dbref)
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
self.assertEqual(obj.name, "SigurXurXorarinsson")
def test_room_create(self):
description = "A dimly-lit alley behind the local Chinese restaurant."
obj, errors = DefaultRoom.create("alley", self.account, description=description, ip=self.ip)