Skip to content

Commit

Permalink
Add category bulk admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Apr 18, 2024
1 parent c89783e commit 410dd8c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions oscarapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
AttributeOptionGroupAdminDetail,
CategoryAdminList,
CategoryAdminDetail,
CategoryBulkAdminApi,
) = get_api_classes(
"views.admin.product",
[
Expand All @@ -127,6 +128,7 @@
"AttributeOptionGroupAdminDetail",
"CategoryAdminList",
"CategoryAdminDetail",
"CategoryBulkAdminApi",
],
)

Expand Down Expand Up @@ -204,6 +206,7 @@
path("ranges/<int:pk>/", RangeDetail.as_view(), name="range-detail"),
path("categories/", CategoryList.as_view(), name="category-list"),
path("categories/<int:pk>/", CategoryDetail.as_view(), name="category-detail"),
path("categories-bulk/", CategoryBulkAdminApi.as_view(), name="admin-category-bulk"),
re_path(
"^categories/(?P<breadcrumbs>.*)/$",
CategoryList.as_view(),
Expand Down
35 changes: 35 additions & 0 deletions oscarapi/utils/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,38 @@ def find_from_full_slug(breadcrumb_str, separator="/"):
category_names = [x.strip() for x in breadcrumb_str.split(separator)]
categories = create_from_sequence(category_names, False)
return categories[-1]



def upsert_categories(data, parent_category=None):
if parent_category is None:
# Starting from root, we want the first category in the root
sibling = Category.get_first_root_node()
else:
# We are further down the category tree, we want to get the first child from the parent
sibling = parent_category.get_first_child()

for cat in data:
children = cat.pop("children", None)

try:
category = Category.objects.get(code=cat["data"]["code"])
except Category.DoesNotExist:
# Category with code does not exist, create it on the root
category = Category.add_root(**cat["data"])

if sibling is not None:
if category.pk != sibling.pk:
# Move the category to the right of the sibling
category.move(sibling, pos="right")
elif parent_category is not None:
if category.get_parent().pk != parent_category.pk:
# Move the category as the first child under the parent category since we have not sibling
category.move(parent_category, pos="first-child")

# The category is now the sibling, new categories will be moved to the right of this category
sibling = category

if children:
# Add children under this category
upsert_categories(children, parent_category=category)
13 changes: 13 additions & 0 deletions oscarapi/views/admin/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
from django.http import Http404
from rest_framework import generics
from rest_framework.exceptions import NotFound
from rest_framework.views import APIView
from rest_framework.response import Response

from oscar.core.loading import get_model
from oscarapi.utils.loading import get_api_classes, get_api_class
from oscarapi.utils.exists import construct_id_filter
from oscarapi.utils.categories import upsert_categories

APIAdminPermission = get_api_class("permissions", "APIAdminPermission")
ProductAttributeSerializer, AttributeOptionGroupSerializer = get_api_classes(
Expand Down Expand Up @@ -140,3 +143,13 @@ class CategoryAdminDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Category.objects.all()
serializer_class = AdminCategorySerializer
permission_classes = (APIAdminPermission,)


class CategoryBulkAdminApi(APIView):
def get(self, request, format=None):
return Response(Category.dump_bulk(keep_ids=False))

def post(self, request):
upsert_categories(request.data)

return self.get(request)
1 change: 1 addition & 0 deletions oscarapi/views/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def ADMIN_APIS(r, f):
("productclasses", reverse("admin-productclass-list", request=r, format=f)),
("products", reverse("admin-product-list", request=r, format=f)),
("categories", reverse("admin-category-list", request=r, format=f)),
("categories-bulk", reverse("admin-category-bulk", request=r, format=f)),
("orders", reverse("admin-order-list", request=r, format=f)),
("partners", reverse("admin-partner-list", request=r, format=f)),
("users", reverse("admin-user-list", request=r, format=f)),
Expand Down

0 comments on commit 410dd8c

Please sign in to comment.