From 23c3d3b03ea0b63faa86f7d08a174963a9b747b9 Mon Sep 17 00:00:00 2001 From: Griatch Date: Tue, 1 Mar 2022 20:32:03 +0100 Subject: [PATCH] Fix strattr regression. Resolve #2660. --- evennia/typeclasses/attributes.py | 22 +++++++++++++++------- evennia/typeclasses/tests.py | 9 +++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/evennia/typeclasses/attributes.py b/evennia/typeclasses/attributes.py index 67522d068d..a43f8382e4 100644 --- a/evennia/typeclasses/attributes.py +++ b/evennia/typeclasses/attributes.py @@ -665,13 +665,14 @@ class IAttributeBackend: self._set_cache(key, category, 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. Args: attr (IAttribute): The Attribute being changed. value (obj): The Value for the Attribute. + strvalue (bool): If True, `value` is expected to be a string. """ raise NotImplementedError() @@ -773,15 +774,16 @@ class IAttributeBackend: self._delete_cache(attr.key, attr.category) self.do_delete_attribute(attr) - def update_attribute(self, attr, value): + def update_attribute(self, attr, value, strattr=False): """ Simply updates an Attribute. Args: attr (IAttribute): The attribute to delete. 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): """ @@ -903,7 +905,7 @@ class InMemoryAttributeBackend(IAttributeBackend): self._category_storage[category].append(new_attr) return new_attr - def do_update_attribute(self, attr, value): + def do_update_attribute(self, attr, value, strvalue): attr.value = value 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) return new_attr - def do_update_attribute(self, attr, value): - attr.value = value + def do_update_attribute(self, attr, value, strvalue): + if strvalue: + attr.value = None + attr.db_strvalue = value + else: + 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): attr_obj.db_category = category @@ -1203,7 +1211,7 @@ class AttributeHandler: if attr_obj: # update an existing attribute object attr_obj = attr_obj[0] - self.backend.update_attribute(attr_obj, value) + self.backend.update_attribute(attr_obj, value, strattr) else: # create a new Attribute (no OOB handlers can be notified) self.backend.create_attribute(keystr, category, lockstring, value, strattr) diff --git a/evennia/typeclasses/tests.py b/evennia/typeclasses/tests.py index 6a9f6bdbcc..aef536dbe0 100644 --- a/evennia/typeclasses/tests.py +++ b/evennia/typeclasses/tests.py @@ -57,6 +57,15 @@ class TestAttributes(BaseEvenniaTest): self.assertEqual(attrobj.category, "category4") 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): def _manager(self, methodname, *args, **kwargs):