Skip to content

Commit

Permalink
refrhes_from_db only path
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Sep 17, 2024
1 parent 6fdc9a8 commit 164b7f4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions oscarapi/utils/categories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.utils.translation import gettext as _
from django.db.utils import IntegrityError

from rest_framework.exceptions import NotFound

Expand Down Expand Up @@ -85,13 +84,11 @@ def upsert_categories(data):

def _upsert_categories(data, parent_category=None):
if parent_category is None:
# Starting from root, we want the first category in the root
# Get the last category in the root
sibling = Category.get_last_root_node()
else:
# We need to update the parent from the db because the path may be different now but not refresh from db since this will update the record in our local categories_to_update and it will never update parent fields
parent_category = Category.objects.get(pk=parent_category.pk)
# We are further down the category tree, we want to get the first child from the parent
sibling = parent_category.get_first_child()
# Set sibling to None if there is a parent category, we want the first category in the data to be added as a first-child of the parent
sibling = None

categories_to_update = []
category_fields_to_update = set()
Expand Down Expand Up @@ -126,8 +123,11 @@ def _upsert_categories(data, parent_category=None):
# Move the category as the first child under the parent category since we have no sibling
category.move(parent_category, pos="first-child")

# Update the new path from the database after moving the category to it's new home
category.refresh_from_db(fields=["path"])

# The category is now the sibling, new categories will be moved to the right of this category
sibling = Category.objects.get(pk=category.pk)
sibling = category

if children:
# Add children under this category
Expand Down

0 comments on commit 164b7f4

Please sign in to comment.