From 506de7fd1dc9306d013c73a9f3d303494659020f Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 23 Apr 2024 17:56:59 -0700 Subject: [PATCH] User.get_or_create: if the id is a copy, return the original ...which will be in a different protocol! worried this will break something. hrm. --- models.py | 2 ++ tests/test_models.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/models.py b/models.py index 6c10c87..d14bf1e 100644 --- a/models.py +++ b/models.py @@ -244,6 +244,8 @@ class User(StringIdModel, metaclass=ProtocolUserMeta): if not propagate: return user else: + if orig := get_original(id): + return orig user = cls(id=id, **kwargs) user.existing = False diff --git a/tests/test_models.py b/tests/test_models.py index 4a76f42..448b4ab 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -138,6 +138,11 @@ class UserTest(TestCase): self.assertEqual('y.z', got.key.id()) assert got.existing + def test_get_or_create_by_copies(self): + other = self.make_user(id='other:ab', cls=OtherFake, + copies=[Target(uri='fake:ab', protocol='fake')]) + self.assert_entities_equal(other, Fake.get_or_create('fake:ab')) + def test_get_or_create_opted_out(self): user = self.make_user('fake:user', cls=Fake, obj_as1 = {'summary': '#nobridge'})