diff --git a/passbook/outposts/models.py b/passbook/outposts/models.py index 703f903dc..b461838d7 100644 --- a/passbook/outposts/models.py +++ b/passbook/outposts/models.py @@ -104,25 +104,22 @@ class Outpost(models.Model): return datetime.fromtimestamp(value) return None - def _create_user(self) -> User: - """Create user and assign permissions for all required objects""" - user: User = User.objects.create(username=f"pb-outpost-{self.uuid.hex}") - user.set_unusable_password() - user.save() + @property + def user(self) -> User: + """Get/create user with access to all required objects""" + users = User.objects.filter(username=f"pb-outpost-{self.uuid.hex}") + if not users.exists(): + user: User = User.objects.create(username=f"pb-outpost-{self.uuid.hex}") + user.set_unusable_password() + user.save() + else: + user = users.first() for model in self.get_required_objects(): assign_perm( f"{model._meta.app_label}.view_{model._meta.model_name}", user, model ) return user - @property - def user(self) -> User: - """Get/create user with access to all required objects""" - user = User.objects.filter(username=f"pb-outpost-{self.uuid.hex}") - if user.exists(): - return user.first() - return self._create_user() - @property def token(self) -> Token: """Get/create token for auto-generated user""" diff --git a/passbook/outposts/tasks.py b/passbook/outposts/tasks.py index e7d7fe717..c28dfd4a3 100644 --- a/passbook/outposts/tasks.py +++ b/passbook/outposts/tasks.py @@ -41,7 +41,7 @@ def outpost_send_update(model_class: str, model_pk: Any): """Send outpost update to all registered outposts, irregardless to which passbook instance they are connected""" model = path_to_class(model_class) - outpost_model: OutpostModel = model.objects.get(model_pk) + outpost_model: OutpostModel = model.objects.get(pk=model_pk) for outpost in outpost_model.outpost_set.all(): channel_layer = get_channel_layer() for channel in outpost.channels: