From 377985be10cb959fa975e9cddc5161d346f3d2c6 Mon Sep 17 00:00:00 2001
From: Kyle McFarland
Date: Sun, 21 Oct 2018 19:26:32 -0600
Subject: Add view suppliers view and suppliers API endpoint
This adds a simple view (mostly cribbed from the component view but
without the database stats) to view details for a given supplier,
currently lists the components they supply and name/email address pairs for
representatives however there's room to add fields for other representative
contact and company information as well.
On the API side this adds 3 endpoints:
* api/suppliers: returns a list of all suppliers and their
representatives
* api/suppliers/: returns a supplier object containing the
representatives for the given supplier
* api/suppliers//components: returns a supplier object
containing a list of components supplied by the given supplier
This also contains minor documentation and base template changes, fixing
the navigation menu to use class="active" on the currently selected page
(previously didn't work due to page_name capitalization)
---
procurement/api.py | 16 ++++++-
procurement/forms.py | 20 ++++++---
procurement/serializers.py | 11 +++++
.../templates/procurement/documentation.html | 7 ++-
.../procurement/includes/supplier_details.html | 51 ++++++++++++++++++++++
.../templates/procurement/view_suppliers.html | 35 +++++++++++++++
procurement/urls.py | 8 +++-
procurement/views.py | 22 +++++++++-
8 files changed, 158 insertions(+), 12 deletions(-)
create mode 100644 procurement/templates/procurement/includes/supplier_details.html
create mode 100644 procurement/templates/procurement/view_suppliers.html
(limited to 'procurement')
diff --git a/procurement/api.py b/procurement/api.py
index 0419ce8..d1830c2 100644
--- a/procurement/api.py
+++ b/procurement/api.py
@@ -1,7 +1,7 @@
from rest_framework.generics import ListAPIView, RetrieveAPIView
-from procurement.models import Component
-from procurement.serializers import ComponentSerializer
+from procurement.models import Component, Supplier
+from procurement.serializers import ComponentSerializer, SupplierSerializer, SupplierComponentsSerializer
class ComponentAPIList(ListAPIView):
@@ -12,3 +12,15 @@ class ComponentAPIList(ListAPIView):
class ComponentAPIRetrieve(RetrieveAPIView):
queryset = Component.objects.all()
serializer_class = ComponentSerializer
+
+class SupplierAPIList(ListAPIView):
+ queryset = Supplier.objects.filter(is_authorized=True)
+ serializer_class = SupplierSerializer
+
+class SupplierAPIRetrieve(RetrieveAPIView):
+ queryset = Supplier.objects.filter(is_authorized=True)
+ serializer_class = SupplierSerializer
+
+class SupplierAPIComponents(RetrieveAPIView):
+ queryset = Supplier.objects.filter(is_authorized=True)
+ serializer_class = SupplierComponentsSerializer
diff --git a/procurement/forms.py b/procurement/forms.py
index 4e8b0aa..0aeca9b 100644
--- a/procurement/forms.py
+++ b/procurement/forms.py
@@ -1,14 +1,24 @@
from django import forms
-from procurement.models import Component
+from procurement.models import Component, Supplier
+class FormControlBase(forms.Form):
+ _formcontrol_fields = []
+ def __init__(self, *args, **kwargs):
+ super(FormControlBase, self).__init__(*args, **kwargs)
+ for fieldname in self._formcontrol_fields:
+ self.fields[fieldname].widget.attrs.update({"class": "form-control"})
-class ComponentSearchForm(forms.Form):
+class ComponentSearchForm(FormControlBase):
+ _formcontrol_fields = ["component"]
component = forms.ModelChoiceField(
queryset=Component.objects.all(),
required=False
)
- def __init__(self, *args, **kwargs):
- super(ComponentSearchForm, self).__init__(*args, **kwargs)
- self.fields['component'].widget.attrs.update({"class": "form-control"})
+class SupplierSearchForm(FormControlBase):
+ _formcontrol_fields = ["supplier"]
+ supplier = forms.ModelChoiceField(
+ queryset=Supplier.objects.filter(is_authorized=True),
+ required=False
+ )
diff --git a/procurement/serializers.py b/procurement/serializers.py
index 0466402..b3e9280 100644
--- a/procurement/serializers.py
+++ b/procurement/serializers.py
@@ -13,6 +13,17 @@ class SupplierSerializer(serializers.ModelSerializer):
model = Supplier
exclude = ('created', 'updated')
+class SupplierComponentSerializer(serializers.ModelSerializer):
+ text = serializers.CharField(source="__str__", read_only=True)
+ class Meta:
+ model = Component
+ exclude = ('created', 'updated')
+
+class SupplierComponentsSerializer(serializers.ModelSerializer):
+ components = SupplierComponentSerializer(many=True, read_only=True)
+ class Meta:
+ model = Supplier
+ exclude = ('created', 'updated')
class ComponentSerializer(serializers.ModelSerializer):
text = serializers.CharField(source='__str__', read_only=True)
diff --git a/procurement/templates/procurement/documentation.html b/procurement/templates/procurement/documentation.html
index 4775890..f482a1d 100644
--- a/procurement/templates/procurement/documentation.html
+++ b/procurement/templates/procurement/documentation.html
@@ -55,7 +55,9 @@
Components and their approved Suppliers can be located under
source components. From this page you may search for a
- component, and then click the Find Suppliers button to display its Suppliers
+ component, and then click the Find Suppliers button to display its Suppliers.
+ You can also view details for any authorized Supplier in the system directly under
+ view suppliers.