Fix strattr regression. Resolve #2660.

This commit is contained in:
Griatch 2022-03-01 20:32:03 +01:00
parent 31b09754a1
commit 669f17f3bb
2 changed files with 24 additions and 7 deletions

View file

@ -665,13 +665,14 @@ class IAttributeBackend:
self._set_cache(key, category, attr) self._set_cache(key, category, attr)
return attr return attr
def do_update_attribute(self, attr, value): def do_update_attribute(self, attr, value, strvalue):
""" """
Simply sets a new Value to an Attribute. Simply sets a new Value to an Attribute.
Args: Args:
attr (IAttribute): The Attribute being changed. attr (IAttribute): The Attribute being changed.
value (obj): The Value for the Attribute. value (obj): The Value for the Attribute.
strvalue (bool): If True, `value` is expected to be a string.
""" """
raise NotImplementedError() raise NotImplementedError()
@ -773,15 +774,16 @@ class IAttributeBackend:
self._delete_cache(attr.key, attr.category) self._delete_cache(attr.key, attr.category)
self.do_delete_attribute(attr) self.do_delete_attribute(attr)
def update_attribute(self, attr, value): def update_attribute(self, attr, value, strattr=False):
""" """
Simply updates an Attribute. Simply updates an Attribute.
Args: Args:
attr (IAttribute): The attribute to delete. attr (IAttribute): The attribute to delete.
value (obj): The new value. value (obj): The new value.
strattr (bool): If set, the `value` is a raw string.
""" """
self.do_update_attribute(attr, value) self.do_update_attribute(attr, value, strattr)
def do_batch_delete(self, attribute_list): def do_batch_delete(self, attribute_list):
""" """
@ -903,7 +905,7 @@ class InMemoryAttributeBackend(IAttributeBackend):
self._category_storage[category].append(new_attr) self._category_storage[category].append(new_attr)
return new_attr return new_attr
def do_update_attribute(self, attr, value): def do_update_attribute(self, attr, value, strvalue):
attr.value = value attr.value = value
def do_batch_update_attribute(self, attr_obj, category, lock_storage, new_value, strvalue): def do_batch_update_attribute(self, attr_obj, category, lock_storage, new_value, strvalue):
@ -1002,8 +1004,14 @@ class ModelAttributeBackend(IAttributeBackend):
self._set_cache(key, category, new_attr) self._set_cache(key, category, new_attr)
return new_attr return new_attr
def do_update_attribute(self, attr, value): def do_update_attribute(self, attr, value, strvalue):
if strvalue:
attr.value = None
attr.db_strvalue = value
else:
attr.value = value attr.value = value
attr.db_strvalue = None
attr.save(update_fields=["db_strvalue", "db_value"])
def do_batch_update_attribute(self, attr_obj, category, lock_storage, new_value, strvalue): def do_batch_update_attribute(self, attr_obj, category, lock_storage, new_value, strvalue):
attr_obj.db_category = category attr_obj.db_category = category
@ -1203,7 +1211,7 @@ class AttributeHandler:
if attr_obj: if attr_obj:
# update an existing attribute object # update an existing attribute object
attr_obj = attr_obj[0] attr_obj = attr_obj[0]
self.backend.update_attribute(attr_obj, value) self.backend.update_attribute(attr_obj, value, strattr)
else: else:
# create a new Attribute (no OOB handlers can be notified) # create a new Attribute (no OOB handlers can be notified)
self.backend.create_attribute(keystr, category, lockstring, value, strattr) self.backend.create_attribute(keystr, category, lockstring, value, strattr)

View file

@ -57,6 +57,15 @@ class TestAttributes(BaseEvenniaTest):
self.assertEqual(attrobj.category, "category4") self.assertEqual(attrobj.category, "category4")
self.assertEqual(attrobj.locks.all(), ["attrread:id(1)"]) self.assertEqual(attrobj.locks.all(), ["attrread:id(1)"])
def test_value_vs_strvalue(self):
self.obj1.attributes.add("test", "one")
self.assertEqual(self.obj1.attributes.get("test"), "one")
self.assertEqual(self.obj1.attributes.get("test", strattr=True), None)
# switch to strattr
self.obj1.attributes.add("test", "two", strattr=True)
self.assertEqual(self.obj1.attributes.get("test"), None)
self.assertEqual(self.obj1.attributes.get("test", strattr=True), "two")
class TestTypedObjectManager(BaseEvenniaTest): class TestTypedObjectManager(BaseEvenniaTest):
def _manager(self, methodname, *args, **kwargs): def _manager(self, methodname, *args, **kwargs):