From bd3a0c1ccc3f0c0a8ee529d68933adc537e83dc3 Mon Sep 17 00:00:00 2001 From: Samar Hassan Date: Thu, 22 Feb 2024 08:54:36 +0000 Subject: [PATCH] refactor :package: rename to instance keys and identifier --- oscar_odin/mappings/context.py | 12 +++++++----- tests/reverse/test_deleting_related.py | 7 ++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/oscar_odin/mappings/context.py b/oscar_odin/mappings/context.py index ecd1786..3bc8678 100644 --- a/oscar_odin/mappings/context.py +++ b/oscar_odin/mappings/context.py @@ -54,7 +54,7 @@ class ModelMapperContext(dict): one_to_many_items = None attribute_data = None identifier_mapping = None - product_identity_list = None + instance_keys = None Model = None errors = None @@ -198,7 +198,7 @@ def bulk_update_or_create_instances(self, instances): ( instances_to_create, instances_to_update, - self.product_identity_list, + self.instance_keys, ) = separate_instances_to_create_and_update( self.Model, instances, self.identifier_mapping ) @@ -251,8 +251,10 @@ def bulk_update_or_create_one_to_many(self): ) if self.delete_related: - product_identity = self.identifier_mapping.get(Product)[0] for relation, keys in identities.items(): + instance_identifier = self.identifier_mapping.get( + relation.remote_field.related_model + )[0] fields = self.get_fields_to_update(relation.related_model) if fields is not None: conditions = Q() @@ -264,12 +266,12 @@ def bulk_update_or_create_one_to_many(self): conditions |= Q(**{f"{identifiers[0]}": key}) field_name = relation.remote_field.attname.replace( "_", "__" - ).replace("id", product_identity) + ).replace("id", instance_identifier) # Delete all related one_to_many instances where product is in the # given list of resources and excluding any instances present in # those resources relation.related_model.objects.filter( - **{f"{field_name}__in": self.product_identity_list} + **{f"{field_name}__in": self.instance_keys} ).exclude(conditions).delete() def bulk_update_or_create_many_to_many(self): diff --git a/tests/reverse/test_deleting_related.py b/tests/reverse/test_deleting_related.py index a88d58b..1f5003a 100644 --- a/tests/reverse/test_deleting_related.py +++ b/tests/reverse/test_deleting_related.py @@ -227,13 +227,10 @@ def test_deleting_product_related_models(self): upc="1234323-2", title="asdf2", structure=Product.STANDALONE, - categories=[CategoryResource(code="1")] + categories=[CategoryResource(code="1")], ), ProductResource( - upc="563-2", - title="bat", - structure=Product.STANDALONE, - categories=[] + upc="563-2", title="bat", structure=Product.STANDALONE, categories=[] ), ] _, errors = products_to_db(