Skip to content

Commit

Permalink
feat ⭐ categories update based on fields_to_update attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
samar-hassan committed Feb 13, 2024
1 parent b97f4c4 commit 336449d
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 79 deletions.
5 changes: 1 addition & 4 deletions oscar_odin/mappings/_model_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ def add_related_field_values_to_context(self, parent, related_field_values):
)

for relation, instances in related_field_values["m2m_related_values"].items():
if instances:
self.context.add_instances_to_m2m_relation(
relation, (parent, instances)
)
self.context.add_instances_to_m2m_relation(relation, (parent, instances))

for relation, instances in related_field_values["o2m_related_values"].items():
if instances:
Expand Down
5 changes: 4 additions & 1 deletion oscar_odin/mappings/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@


ALL_CATALOGUE_FIELDS = (
ALL_PRODUCT_FIELDS + ALL_PRODUCTIMAGE_FIELDS + ALL_STOCKRECORD_FIELDS
ALL_PRODUCT_FIELDS
+ ALL_PRODUCTIMAGE_FIELDS
+ ALL_STOCKRECORD_FIELDS
+ ALL_CATEGORY_FIELDS
)

MODEL_IDENTIFIERS_MAPPING = {
Expand Down
23 changes: 19 additions & 4 deletions oscar_odin/mappings/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def separate_instances_to_create_and_update(Model, instances, identifier_mapping

identifiers = identifier_mapping.get(Model, {})

if identifiers:
if identifiers and instances:
# pylint: disable=protected-access
id_mapping = in_bulk(Model._default_manager, instances, identifiers)

Expand Down Expand Up @@ -245,8 +245,10 @@ def bulk_update_or_create_many_to_many(self):

# Create many to many's
for relation, instances in m2m_to_create.items():
validated_m2m_instances = self.validate_instances(instances)
relation.related_model.objects.bulk_create(validated_m2m_instances)
fields = self.get_fields_to_update(relation.related_model)
if fields is not None:
validated_m2m_instances = self.validate_instances(instances)
relation.related_model.objects.bulk_create(validated_m2m_instances)

# Update many to many's
for relation, instances in m2m_to_update.items():
Expand All @@ -258,11 +260,19 @@ def bulk_update_or_create_many_to_many(self):
)

for relation, values in self.many_to_many_items.items():
fields = self.get_fields_to_update(relation.related_model)
if fields is None:
continue

Through = getattr(self.Model, relation.name).through

# Create all through models that are needed for the products and many to many
throughs = defaultdict(Through)
delete_throughs = []
for product, instances in values:
if not instances and self.delete_related:
delete_throughs.append(product.id)
continue
for instance in instances:
throughs[(product.pk, instance.pk)] = Through(
**{
Expand All @@ -271,6 +281,11 @@ def bulk_update_or_create_many_to_many(self):
}
)

# Delete throughs if no instances are passed for the field
Through.objects.filter(product_id__in=delete_throughs).all().delete()
if not throughs:
continue

# Bulk query the through models to see if some already exist
bulk_troughs = in_bulk(
Through.objects,
Expand All @@ -286,7 +301,7 @@ def bulk_update_or_create_many_to_many(self):
if b in throughs:
throughs.pop(b)

# Delete non-existing through models
# Delete remaining non-existing through models
if self.delete_related:
Through.objects.filter(
product_id__in=[item[0] for item in bulk_troughs.keys()]
Expand Down
4 changes: 3 additions & 1 deletion tests/reverse/test_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def test_create_product_with_related_fields(self):
stockrecord = prd.stockrecords.first()
self.assertEqual(stockrecord.price, D("21.50"))
self.assertEqual(stockrecord.num_in_stock, 3)
self.assertEqual(prd.categories.count(), 2)
# Category was not included in fields_to_update
self.assertEqual(prd.categories.count(), 1)
self.assertFalse(prd.categories.filter(code="1").exists())

self.assertEqual(prd.images.count(), 4)

Expand Down
Loading

0 comments on commit 336449d

Please sign in to comment.