part of get data

This commit is contained in:
aarizona 2024-05-29 12:54:47 +03:00
parent 698f791920
commit b71ebfc398
1 changed files with 31 additions and 26 deletions

View File

@ -44,6 +44,21 @@ class PartnerViewSet(viewsets.ModelViewSet):
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get_depth_cat(id):
params = {
"$format": "json",
"$select": ",".join(["Description", "Ref_Key", "Parent_Key"]),
"$filter": f"Parent_Key eq guid'{id}'",
}
remote_url = (
"https://1c.svs-tech.pro/UNF/odata/standard.odata/Catalog_КатегорииНоменклатуры?"
+ "&".join([f"{p}={params[p]}" for p in params])
)
data = requests.get(remote_url, headers={"Authorization": settings.ODATA_AUTH})
parsed_data = data.json()
return parsed_data["value"]
class ElementViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows elements to be viewed or edited.
@ -55,32 +70,22 @@ class ElementViewSet(viewsets.ModelViewSet):
@action(detail=False, methods=["get"], url_path=r"external_categories")
def get_remote_categories(self, request):
try:
params = {
"$format": "json",
"$select": ",".join(["Description", "Ref_Key", "Parent_Key"]),
"$filter": "Parent_Key eq guid'a6aadfe8-7e0f-11ee-ab5a-a47a2bd811cb'",
}
remote_url = (
"https://1c.svs-tech.pro/UNF/odata/standard.odata/Catalog_КатегорииНоменклатуры?"
+ "&".join([f"{p}={params[p]}" for p in params])
)
data = requests.get(
remote_url, headers={"Authorization": settings.ODATA_AUTH}
)
parsed_data = data.json()
params["$filter"] = " or ".join(
[f"Parent_Key eq guid'{v['Ref_Key']}'" for v in parsed_data["value"]]
)
remote_url = (
"https://1c.svs-tech.pro/UNF/odata/standard.odata/Catalog_КатегорииНоменклатуры?"
+ "&".join([f"{p}={params[p]}" for p in params])
)
data = requests.get(
remote_url, headers={"Authorization": settings.ODATA_AUTH}
)
parsed_data = data.json()
return Response(parsed_data["value"])
all_categories = []
categories = get_depth_cat("a6aadfe8-7e0f-11ee-ab5a-a47a2bd811cb")
for category in categories:
depth = get_depth_cat(category["Ref_Key"])
if len(depth):
for cat2 in depth:
depth2 = get_depth_cat(cat2["Ref_Key"])
if len(depth2):
for cat3 in depth2:
depth3 = get_depth_cat(cat3["Ref_Key"])
all_categories.append(depth3)
else:
all_categories.append(depth2)
else:
all_categories.append(depth)
return Response(categories)
except Exception as e:
logger.error(e)
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)