From ab5e5cab69f3a3b5a390aa680bb1b5ce5d1fa0c3 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 14 Mar 2023 19:23:19 +1100 Subject: [PATCH] Add ContactList and ContactDetail API endpoints --- InvenTree/InvenTree/api_version.py | 1 + InvenTree/company/api.py | 45 ++++++++++++++++++++++++++++-- InvenTree/company/serializers.py | 19 ++++++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/api_version.py b/InvenTree/InvenTree/api_version.py index b540ceba4f..1119a1ac01 100644 --- a/InvenTree/InvenTree/api_version.py +++ b/InvenTree/InvenTree/api_version.py @@ -9,6 +9,7 @@ Increment this API version number whenever there is a significant change to the v102 -> 2023-03-14 : https://github.com/inventree/InvenTree/pull/4488 - Adds various endpoints for new "ReturnOrder" models + - Exposes API endpoints for "Contact" model v101 -> 2023-03-07 : https://github.com/inventree/InvenTree/pull/4462 - Adds 'total_in_stock' to Part serializer, and supports API ordering diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 8479348ce3..5566e15cec 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -1,7 +1,7 @@ """Provides a JSON API for the Company app.""" from django.db.models import Q -from django.urls import include, re_path +from django.urls import include, path, re_path from django_filters import rest_framework as rest_filters from django_filters.rest_framework import DjangoFilterBackend @@ -15,10 +15,11 @@ from InvenTree.mixins import (ListCreateAPI, RetrieveUpdateAPI, RetrieveUpdateDestroyAPI) from plugin.serializers import MetadataSerializer -from .models import (Company, CompanyAttachment, ManufacturerPart, +from .models import (Company, CompanyAttachment, Contact, ManufacturerPart, ManufacturerPartAttachment, ManufacturerPartParameter, SupplierPart, SupplierPriceBreak) from .serializers import (CompanyAttachmentSerializer, CompanySerializer, + ContactSerializer, ManufacturerPartAttachmentSerializer, ManufacturerPartParameterSerializer, ManufacturerPartSerializer, SupplierPartSerializer, @@ -118,6 +119,41 @@ class CompanyAttachmentDetail(AttachmentMixin, RetrieveUpdateDestroyAPI): serializer_class = CompanyAttachmentSerializer +class ContactList(ListCreateDestroyAPIView): + """API endpoint for list view of Company model""" + + queryset = Contact.objects.all() + serializer_class = ContactSerializer + + filter_backends = [ + DjangoFilterBackend, + filters.SearchFilter, + filters.OrderingFilter, + ] + + filterset_fields = [ + 'company', + ] + + search_fields = [ + 'company__name', + 'name', + ] + + ordering_fields = [ + 'name', + ] + + ordering = 'name' + + +class ContactDetail(RetrieveUpdateDestroyAPI): + """Detail endpoint for Company model""" + + queryset = Contact.objects.all() + serializer_class = ContactSerializer + + class ManufacturerPartFilter(rest_filters.FilterSet): """Custom API filters for the ManufacturerPart list endpoint.""" @@ -548,6 +584,11 @@ company_api_urls = [ re_path(r'^$', CompanyAttachmentList.as_view(), name='api-company-attachment-list'), ])), + re_path(r'^contact/', include([ + path('/', ContactDetail.as_view(), name='api-contact-detail'), + re_path(r'^.*$', ContactList.as_view(), name='api-contact-list'), + ])), + re_path(r'^.*$', CompanyList.as_view(), name='api-company-list'), ] diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index faa281cbed..bad595d85f 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -17,7 +17,7 @@ from InvenTree.serializers import (InvenTreeAttachmentSerializer, InvenTreeMoneySerializer, RemoteImageMixin) from part.serializers import PartBriefSerializer -from .models import (Company, CompanyAttachment, ManufacturerPart, +from .models import (Company, CompanyAttachment, Contact, ManufacturerPart, ManufacturerPartAttachment, ManufacturerPartParameter, SupplierPart, SupplierPriceBreak) @@ -132,6 +132,23 @@ class CompanyAttachmentSerializer(InvenTreeAttachmentSerializer): ]) +class ContactSerializer(InvenTreeModelSerializer): + """Serializer class for the Contact model""" + + class Meta: + """Metaclass options""" + + model = Contact + fields = [ + 'pk', + 'company', + 'name', + 'phone', + 'email', + 'role', + ] + + class ManufacturerPartSerializer(InvenTreeModelSerializer): """Serializer for ManufacturerPart object."""