just rewriting the func at this point

This commit is contained in:
Cal 2026-02-15 16:45:43 -07:00
parent 8c895fb1b5
commit d05fa6aeec
2 changed files with 25 additions and 15 deletions

View file

@ -1506,17 +1506,20 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
Append 01, 02 etc to obj.key. Checks next higher number in the
same location, then adds the next number available
returns the new clone name on the form keyXX
Returns the new clone name on the form keyXX
"""
key = self.key
if not self.location:
# no location means no clone numbering
return key
suffixes = [
obj.key.removeprefix(key)
for obj in self.location.contents
]
num = 1
if self.location:
num = max([0]+[
int(obj.key.lstrip(key))
for obj in self.location.contents
if obj.key.startswith(key) and obj.key.lstrip(key).isdigit()
])+1
return "%s%03i" % (key, num)
if nums := [int(suffix) for suffix in suffixes if suffix.isdigit()]:
num = max(nums) + 1
return f"{key}{num:02d}"
new_key = new_key or find_clone_key()
new_obj = ObjectDB.objects.copy_object(self, new_key=new_key, **kwargs)

View file

@ -399,20 +399,27 @@ class TestObjectManager(BaseEvenniaTest):
# reset key to avoid overlap with other tests
self.obj1.key = "CopyMe"
copied = self.obj1.copy()
self.assertEqual(copied.key, "CopyMe001")
self.assertEqual(copied.key, "CopyMe01")
copied2 = self.obj1.copy()
self.assertEqual(copied2.key, "CopyMe002")
self.assertEqual(copied2.key, "CopyMe02")
# verify that it increments based on max existing identifier
# both for skipped numbers...
copied.key = "CopyMe003"
copied.key = "CopyMe03"
copied3 = self.obj1.copy()
self.assertEqual(copied3.key, "CopyMe004")
self.assertEqual(copied3.key, "CopyMe04")
copied3.delete()
# ...and for duplicate numbers
copied.key = "CopyMe001"
copied2.key = "CopyMe001"
copied.key = "CopyMe01"
copied2.key = "CopyMe01"
copied3 = self.obj1.copy()
self.assertEqual(copied3.key, "CopyMe002")
self.assertEqual(copied3.key, "CopyMe02")
# and that sharing a partial prefix doesn't count
copied3.delete()
copied.key = "CopyMeMe02"
copied2.key = "CopyMe01"
copied3 = self.obj1.copy()
self.assertEqual(copied3.key, "CopyMe02")
def test_copy_object_no_location(self):
self.obj1.location = None