Add API endpoints for Owner model

- Will be necessary for PurchaseOrder, SalesOrder and BuildOrder forms
This commit is contained in:
Oliver 2021-07-02 22:45:42 +10:00
parent 2ff9b23647
commit 5e9372f2d6
7 changed files with 91 additions and 17 deletions

View File

@ -48,7 +48,7 @@ from common.views import SettingEdit
from .api import InfoView, NotFoundView from .api import InfoView, NotFoundView
from .api import ActionPluginView from .api import ActionPluginView
from users.urls import user_urls from users.api import user_urls
admin.site.site_header = "InvenTree Admin" admin.site.site_header = "InvenTree Admin"

View File

@ -82,6 +82,7 @@ class POSerializer(InvenTreeModelSerializer):
'link', 'link',
'overdue', 'overdue',
'reference', 'reference',
'responsible',
'supplier', 'supplier',
'supplier_detail', 'supplier_detail',
'supplier_reference', 'supplier_reference',

View File

@ -266,6 +266,11 @@ function loadSalesOrderTable(table, options) {
field: 'customer_detail', field: 'customer_detail',
title: '{% trans "Customer" %}', title: '{% trans "Customer" %}',
formatter: function(value, row, index, field) { 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/`); return imageHoverIcon(row.customer_detail.image) + renderLink(row.customer_detail.name, `/company/${row.customer}/sales-orders/`);
} }
}, },

View File

@ -1,17 +1,39 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from rest_framework import generics, permissions
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist 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.views import APIView
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status 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): class RoleDetails(APIView):
@ -110,3 +132,18 @@ class GetAuthToken(APIView):
except (AttributeError, ObjectDoesNotExist): except (AttributeError, ObjectDoesNotExist):
return Response({"error": "Bad request"}, return Response({"error": "Bad request"},
status=status.HTTP_400_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<pk>[0-9]+)/$', OwnerDetail.as_view(), name='api-owner-detail'),
url(r'^.*$', OwnerList.as_view(), name='api-owner-list'),
])),
url(r'^(?P<pk>[0-9]+)/?$', UserDetail.as_view(), name='user-detail'),
url(r'^$', UserList.as_view()),
]

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.urls import reverse
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.fields import GenericForeignKey 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 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: class Meta:
# Ensure all owners are unique # Ensure all owners are unique
constraints = [ constraints = [
@ -477,6 +482,18 @@ class Owner(models.Model):
""" Defines the owner string representation """ """ Defines the owner string representation """
return f'{self.owner} ({self.owner_type.name})' 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 @classmethod
def create(cls, obj): def create(cls, obj):
""" Check if owner exist then create new owner entry """ """ Check if owner exist then create new owner entry """

View File

@ -1,8 +1,14 @@
# -*- coding: utf-8 -*-
from rest_framework import serializers from rest_framework import serializers
from django.contrib.auth.models import User 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 """ Serializer for a User
""" """
@ -13,3 +19,22 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
'first_name', 'first_name',
'last_name', 'last_name',
'email',) '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',
]

View File

@ -1,12 +1 @@
from django.conf.urls import url # -*- coding: utf-8 -*-
from . import api
user_urls = [
url(r'^(?P<pk>[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()),
]