From 5e9372f2d6ee9370e2f35d74e0266438f364d747 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 2 Jul 2021 22:45:42 +1000 Subject: [PATCH] Add API endpoints for Owner model - Will be necessary for PurchaseOrder, SalesOrder and BuildOrder forms --- InvenTree/InvenTree/urls.py | 2 +- InvenTree/order/serializers.py | 1 + InvenTree/templates/js/order.js | 5 ++++ InvenTree/users/api.py | 43 ++++++++++++++++++++++++++++++--- InvenTree/users/models.py | 17 +++++++++++++ InvenTree/users/serializers.py | 27 ++++++++++++++++++++- InvenTree/users/urls.py | 13 +--------- 7 files changed, 91 insertions(+), 17 deletions(-) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 6a0d02263d..f3ea86e8cc 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -48,7 +48,7 @@ from common.views import SettingEdit from .api import InfoView, NotFoundView from .api import ActionPluginView -from users.urls import user_urls +from users.api import user_urls admin.site.site_header = "InvenTree Admin" diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index f1eac82530..31a96bf635 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -82,6 +82,7 @@ class POSerializer(InvenTreeModelSerializer): 'link', 'overdue', 'reference', + 'responsible', 'supplier', 'supplier_detail', 'supplier_reference', diff --git a/InvenTree/templates/js/order.js b/InvenTree/templates/js/order.js index 0af54fa43c..e4aa4850e6 100644 --- a/InvenTree/templates/js/order.js +++ b/InvenTree/templates/js/order.js @@ -266,6 +266,11 @@ function loadSalesOrderTable(table, options) { field: 'customer_detail', title: '{% trans "Customer" %}', formatter: function(value, row, index, field) { + + if (!row.customer_detail) { + return '{% trans "Invalid Customer" %}'; + } + return imageHoverIcon(row.customer_detail.image) + renderLink(row.customer_detail.name, `/company/${row.customer}/sales-orders/`); } }, diff --git a/InvenTree/users/api.py b/InvenTree/users/api.py index 5447bb5547..7efb0ad51c 100644 --- a/InvenTree/users/api.py +++ b/InvenTree/users/api.py @@ -1,17 +1,39 @@ # -*- coding: utf-8 -*- + from __future__ import unicode_literals -from rest_framework import generics, permissions from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist -from .serializers import UserSerializer +from django.conf.urls import url, include + +from rest_framework import generics, permissions from rest_framework.views import APIView from rest_framework.authtoken.models import Token from rest_framework.response import Response from rest_framework import status -from .models import RuleSet, check_user_role +from .serializers import UserSerializer, OwnerSerializer + +from .models import RuleSet, Owner, check_user_role + + +class OwnerList(generics.ListAPIView): + """ + List API endpoint for Owner model. Cannot create. + """ + + queryset = Owner.objects.all() + serializer_class = OwnerSerializer + + +class OwnerDetail(generics.RetrieveAPIView): + """ + Detail API endpoint for Owner model. Cannot edit or delete + """ + + queryset = Owner.objects.all() + serializer_class = OwnerSerializer class RoleDetails(APIView): @@ -110,3 +132,18 @@ class GetAuthToken(APIView): except (AttributeError, ObjectDoesNotExist): return Response({"error": "Bad request"}, status=status.HTTP_400_BAD_REQUEST) + + +user_urls = [ + + url(r'roles/?$', RoleDetails.as_view(), name='api-user-roles'), + url(r'token/?$', GetAuthToken.as_view(), name='api-token'), + + url(r'^owner/', include([ + url(r'^(?P[0-9]+)/$', OwnerDetail.as_view(), name='api-owner-detail'), + url(r'^.*$', OwnerList.as_view(), name='api-owner-list'), + ])), + + url(r'^(?P[0-9]+)/?$', UserDetail.as_view(), name='user-detail'), + url(r'^$', UserList.as_view()), +] \ No newline at end of file diff --git a/InvenTree/users/models.py b/InvenTree/users/models.py index 23353948b1..fda1830796 100644 --- a/InvenTree/users/models.py +++ b/InvenTree/users/models.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from django.urls import reverse from django.contrib.auth import get_user_model from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.fields import GenericForeignKey @@ -460,6 +461,10 @@ class Owner(models.Model): owner: Returns the Group or User instance combining the owner_type and owner_id fields """ + @staticmethod + def get_api_url(): + return reverse('api-owner-list') + class Meta: # Ensure all owners are unique constraints = [ @@ -477,6 +482,18 @@ class Owner(models.Model): """ Defines the owner string representation """ return f'{self.owner} ({self.owner_type.name})' + def name(self): + """ + Return the 'name' of this owner + """ + return str(self.owner) + + def label(self): + """ + Return the 'type' label of this owner i.e. 'user' or 'group' + """ + return str(self.owner_type.name) + @classmethod def create(cls, obj): """ Check if owner exist then create new owner entry """ diff --git a/InvenTree/users/serializers.py b/InvenTree/users/serializers.py index 13aadb24b1..2202cf299c 100644 --- a/InvenTree/users/serializers.py +++ b/InvenTree/users/serializers.py @@ -1,8 +1,14 @@ +# -*- coding: utf-8 -*- + from rest_framework import serializers from django.contrib.auth.models import User +from .models import Owner -class UserSerializer(serializers.HyperlinkedModelSerializer): +from InvenTree.serializers import InvenTreeModelSerializer + + +class UserSerializer(InvenTreeModelSerializer): """ Serializer for a User """ @@ -13,3 +19,22 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): 'first_name', 'last_name', 'email',) + + +class OwnerSerializer(InvenTreeModelSerializer): + """ + Serializer for an "Owner" (either a "user" or a "group") + """ + + name = serializers.CharField(read_only=True) + + label = serializers.CharField(read_only=True) + + class Meta: + model = Owner + fields = [ + 'pk', + 'owner_id', + 'name', + 'label', + ] diff --git a/InvenTree/users/urls.py b/InvenTree/users/urls.py index 7f29bd85cc..7c68785e9d 100644 --- a/InvenTree/users/urls.py +++ b/InvenTree/users/urls.py @@ -1,12 +1 @@ -from django.conf.urls import url - -from . import api - -user_urls = [ - url(r'^(?P[0-9]+)/?$', api.UserDetail.as_view(), name='user-detail'), - - url(r'roles/?$', api.RoleDetails.as_view(), name='api-user-roles'), - url(r'token/?$', api.GetAuthToken.as_view(), name='api-token'), - - url(r'^$', api.UserList.as_view()), -] +# -*- coding: utf-8 -*- \ No newline at end of file