Skip to content

Commit

Permalink
refactor 📦 rename to instance keys and identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
samar-hassan authored and viggo-devries committed Feb 23, 2024
1 parent ad7afe6 commit bd3a0c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
12 changes: 7 additions & 5 deletions oscar_odin/mappings/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand Down
7 changes: 2 additions & 5 deletions tests/reverse/test_deleting_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bd3a0c1

Please sign in to comment.