diff --git a/evennia/objects/manager.py b/evennia/objects/manager.py index 399a43293d..fbed029624 100644 --- a/evennia/objects/manager.py +++ b/evennia/objects/manager.py @@ -105,7 +105,7 @@ class ObjectDBManager(TypedObjectManager): candidates (list, optional): Only match among the given list of candidates. Returns: - matches (list): The matching objects. + matches (query): The matching objects. """ cand_restriction = candidates is not None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q() @@ -119,18 +119,19 @@ class ObjectDBManager(TypedObjectManager): Args: attribute_name (str): Attribute name to search for. - candidates (list, optional): Only match among the given list of candidates. + candidates (list, optional): Only match among the given list of object + candidates. Returns: - matches (list): All objects having the given attribute_name defined at all. + matches (query): All objects having the given attribute_name defined at all. """ - cand_restriction = candidates is not None and Q(db_attributes__db_obj__pk__in=[_GA(obj, "id") for obj - in make_iter(candidates) - if obj]) or Q() - return list(self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name))) + cand_restriction = \ + candidates is not None and Q(id__in=[obj.id for obj in candidates]) or Q() + return self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name)) - def get_objs_with_attr_value(self, attribute_name, attribute_value, candidates=None, typeclasses=None): + def get_objs_with_attr_value(self, attribute_name, attribute_value, + candidates=None, typeclasses=None): """ Get all objects having the given attrname set to the given value. @@ -141,7 +142,8 @@ class ObjectDBManager(TypedObjectManager): typeclasses (list, optional): Python pats to restrict matches with. Returns: - matches (list): Objects fullfilling both the `attribute_name` and `attribute_value` criterions. + matches (list): Objects fullfilling both the `attribute_name` and + `attribute_value` criterions. Notes: This uses the Attribute's PickledField to transparently search the database by matching diff --git a/evennia/objects/tests.py b/evennia/objects/tests.py index a08b1ec29d..db1ea15f0f 100644 --- a/evennia/objects/tests.py +++ b/evennia/objects/tests.py @@ -55,6 +55,8 @@ class TestObjectManager(EvenniaTest): self.assertEqual(query, self.char1) query = ObjectDB.objects.get_object_with_account(self.account.dbref) self.assertEqual(query, self.char1) + query = ObjectDB.objects.get_object_with_account("#123456") + self.assertEqual(query, None) query = ObjectDB.objects.get_object_with_account("TestAccou").first() self.assertEqual(query, None) @@ -64,3 +66,28 @@ class TestObjectManager(EvenniaTest): query = ObjectDB.objects.get_object_with_account( "TestAccou", candidates=[self.char1, self.obj1], exact=False) self.assertEqual(list(query), [self.char1]) + + def test_get_objs_with_key_and_typeclass(self): + query = ObjectDB.objects.get_objs_with_key_and_typeclass( + "Char", "evennia.objects.objects.DefaultCharacter") + self.assertEqual(list(query), [self.char1]) + query = ObjectDB.objects.get_objs_with_key_and_typeclass( + "Char", "evennia.objects.objects.DefaultObject") + self.assertFalse(query) + query = ObjectDB.objects.get_objs_with_key_and_typeclass( + "NotFound", "evennia.objects.objects.DefaultCharacter") + self.assertFalse(query) + query = ObjectDB.objects.get_objs_with_key_and_typeclass( + "Char", "evennia.objects.objects.DefaultCharacter", candidates=[self.char1, self.char2]) + self.assertEqual(list(query), [self.char1]) + + def test_get_objs_with_attr(self): + self.obj1.db.testattr = "testval1" + query = ObjectDB.objects.get_objs_with_attr("testattr") + self.assertEqual(list(query), [self.obj1]) + query = ObjectDB.objects.get_objs_with_attr( + "testattr", candidates=[self.char1, self.obj1] ) + self.assertEqual(list(query), [self.obj1]) + query = ObjectDB.objects.get_objs_with_attr( + "NotFound", candidates=[self.char1, self.obj1] ) + self.assertFalse(query)