to_inventory/back/inventory/views.py

53 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import urllib
import requests
from django.conf import settings
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework import status
from rest_framework.response import Response
from .models import Element, Partner
from .serializers import PartnerSerializer, ElementSerializer
import logging
logger = logging.getLogger("root")
class PartnerViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows partners to be viewed or edited.
"""
queryset = Partner.objects.all()
serializer_class = PartnerSerializer
@action(detail=False, methods=["get"], url_path=r"external")
def get_remote_partners(self, request):
params = {
"$format": "json",
"$select": ",".join(["НаименованиеПолное", "Description", "Ref_Key"]),
"$filter": "Недействителен eq false",
}
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})
try:
parsed_data = data.json()
return Response(parsed_data["value"])
except Exception as e:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class ElementViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows elements to be viewed or edited.
"""
queryset = Element.objects.all()
serializer_class = ElementSerializer