diff --git a/fractal_database_matrix/migrations/0001_initial.py b/fractal_database_matrix/migrations/0001_initial.py index 48705d7..70d839d 100644 --- a/fractal_database_matrix/migrations/0001_initial.py +++ b/fractal_database_matrix/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.2 on 2024-02-16 22:59 +# Generated by Django 5.0.2 on 2024-02-22 21:00 import django.db.models.deletion from django.db import migrations, models diff --git a/fractal_database_matrix/representations.py b/fractal_database_matrix/representations.py index e64e3ac..c6361bf 100644 --- a/fractal_database_matrix/representations.py +++ b/fractal_database_matrix/representations.py @@ -264,14 +264,26 @@ async def create_representation(self, repr_log: "RepresentationLog", target_id: """ Creates a Matrix space for the ReplicatedModel "instance" that inherits from this class """ - model_class = repr_log.content_type.model_class() + # get the model the object that this representation log is for + # (this is usually a ReplicationTarget model since only ReplicationTargets can create representations) + model_class: "MatrixReplicationTarget" = repr_log.content_type.model_class() # type: ignore + # get the model for the target that this representation log is for target_model = repr_log.target_type.model_class() + # fetch the replicated model that this representation log is for instance = await model_class.objects.aget(pk=repr_log.object_id) - target = await target_model.objects.prefetch_related("matrixcredentials_set").aget( + # fetch the target + target: "MatrixReplicationTarget" = await target_model.objects.prefetch_related( + "matrixcredentials_set" + ).aget( pk=target_id - ) + ) # type: ignore + + # pull room ids from metadata parent_room_id = target.metadata["room_id"] child_room_id = instance.metadata["room_id"] + if parent_room_id == child_room_id: + raise Exception("Parent and child room IDs cannot be the same") + await self.add_subspace(target, parent_room_id, child_room_id)