2021-08-07 19:47:34 +02:00
|
|
|
from django.shortcuts import render
|
2024-11-10 20:46:45 +01:00
|
|
|
from django.contrib.auth.models import Group
|
|
|
|
from django.contrib.auth import get_user_model
|
2021-12-30 21:12:58 +01:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2021-08-07 19:47:34 +02:00
|
|
|
from rest_framework import viewsets, status
|
|
|
|
from rest_framework import permissions
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from .serializers import *
|
|
|
|
from parts import models as parts_models
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from django.db.models.deletion import ProtectedError
|
|
|
|
from django.db.models import F
|
|
|
|
import datetime
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import timezone
|
|
|
|
from rest_framework.authtoken.views import ObtainAuthToken
|
|
|
|
from rest_framework.authtoken.models import Token
|
|
|
|
from rest_framework.throttling import AnonRateThrottle
|
2021-08-14 00:25:19 +02:00
|
|
|
from rest_framework.decorators import action
|
2021-10-26 19:56:20 +02:00
|
|
|
from rest_framework import filters
|
2021-12-30 21:55:35 +01:00
|
|
|
import django_filters.rest_framework
|
2021-08-07 19:47:34 +02:00
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
class UserViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint that allows users to be viewed or edited.
|
|
|
|
"""
|
2024-11-10 20:46:45 +01:00
|
|
|
queryset = get_user_model().objects.all()
|
2021-08-07 19:47:34 +02:00
|
|
|
serializer_class = UserSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2021-10-26 19:56:20 +02:00
|
|
|
filter_backends = [filters.SearchFilter]
|
|
|
|
search_fields = ['username', 'first_name', 'last_name', 'email']
|
2021-08-07 19:47:34 +02:00
|
|
|
|
|
|
|
class GroupViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint that allows users to be viewed or edited.
|
|
|
|
"""
|
|
|
|
queryset = Group.objects.all()
|
|
|
|
serializer_class = GroupSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
class PartsStorageViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.Storage.objects.all()
|
|
|
|
serializer_class = StorageSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
2021-12-30 21:55:35 +01:00
|
|
|
filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
|
2024-11-10 20:46:45 +01:00
|
|
|
search_fields = ['id', 'name', 'parent_storage']
|
2021-12-30 21:55:35 +01:00
|
|
|
filterset_fields = ['id', 'name', 'parent_storage']
|
2024-11-10 20:46:45 +01:00
|
|
|
|
|
|
|
class PartsStorageTemplatesViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
queryset = parts_models.Storage.objects.filter(is_template=True)
|
|
|
|
serializer_class = StorageSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
filter_backends = [filters.SearchFilter]
|
|
|
|
search_fields = ['id', 'name']
|
|
|
|
filterset_fields = ['id', 'name']
|
|
|
|
|
2021-08-07 19:47:34 +02:00
|
|
|
class PartsComponentViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.Component.objects.all()
|
|
|
|
serializer_class = ComponentSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
2022-01-04 20:06:22 +01:00
|
|
|
filter_backends = [filters.SearchFilter, django_filters.rest_framework.DjangoFilterBackend]
|
2021-11-08 23:11:05 +01:00
|
|
|
search_fields = ['id', 'name', 'package__name', 'manufacturer__name']
|
2022-01-04 20:06:22 +01:00
|
|
|
filterset_fields = ['id', 'name']
|
2021-08-07 19:47:34 +02:00
|
|
|
|
2021-11-11 20:51:02 +01:00
|
|
|
class PartsComponentTypeViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.ComponentType.objects.all()
|
|
|
|
serializer_class = ComponentTypeSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
2022-01-04 20:05:11 +01:00
|
|
|
filter_backends = [filters.SearchFilter]
|
2021-11-11 20:51:02 +01:00
|
|
|
search_fields = ['class_name']
|
|
|
|
|
|
|
|
class PartsComponentParameterTypeViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.ComponentParameterType.objects.all()
|
|
|
|
serializer_class = ComponentParameterTypeSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
|
|
|
filter_backends = [filters.SearchFilter]
|
2022-01-03 17:38:50 +01:00
|
|
|
search_fields = ['parameter_name', 'unit']
|
2021-11-11 20:51:02 +01:00
|
|
|
|
2021-11-08 00:56:33 +01:00
|
|
|
class PartsManufacturerViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.Manufacturer.objects.all()
|
|
|
|
serializer_class = ManufacturerSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
|
|
|
filter_backends = [filters.SearchFilter]
|
|
|
|
search_fields = ['name']
|
|
|
|
|
2021-08-07 19:47:34 +02:00
|
|
|
class PartsStockViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.Stock.objects.all()
|
|
|
|
serializer_class = StockSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
|
|
|
|
2021-08-14 00:25:19 +02:00
|
|
|
@action(detail=True, methods=['patch'], name="change-stock-count")
|
|
|
|
def update_stock(self, request, pk=None):
|
|
|
|
stock = self.get_object()
|
|
|
|
serializer = StockIncrementDecrementSerializer(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
increment = serializer.data['increment']
|
|
|
|
if stock.atomic_increment(increment):
|
|
|
|
return Response({'status': 'Stock updated', 'update_value': increment})
|
|
|
|
else:
|
|
|
|
return Response({'status': 'Stock not updated. Would be negative'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
else:
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2021-08-07 19:47:34 +02:00
|
|
|
class PartsPackageViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.Package.objects.all()
|
|
|
|
serializer_class = PackageSerializer
|
2021-11-11 20:51:02 +01:00
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
|
|
|
filter_backends = [filters.SearchFilter]
|
|
|
|
search_fields = ['name']
|
2021-08-07 19:47:34 +02:00
|
|
|
|
|
|
|
class PartsDistributorviewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = parts_models.Distributor.objects.all()
|
|
|
|
serializer_class = DistributorSerializer
|
|
|
|
permission_classes = [permissions.DjangoModelPermissions]
|
2021-11-11 20:51:02 +01:00
|
|
|
filter_backends = [filters.SearchFilter]
|
|
|
|
search_fields = ['name']
|
2021-08-07 19:47:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Token Authentication views
|
|
|
|
|
|
|
|
EXPIRE_HOURS = getattr(settings, 'REST_FRAMEWORK_TOKEN_EXPIRE_HOURS', 24)
|
|
|
|
|
|
|
|
|
|
|
|
class ObtainExpiringAuthToken(ObtainAuthToken):
|
|
|
|
throttle_classes = [AnonRateThrottle]
|
|
|
|
def post(self, request):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
try:
|
|
|
|
token = Token.objects.get(user=serializer.validated_data['user'])
|
|
|
|
if token.created < timezone.now() - timedelta(hours=EXPIRE_HOURS):
|
|
|
|
token.delete()
|
|
|
|
except Token.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
token, created = Token.objects.get_or_create(user=serializer.validated_data['user'])
|
|
|
|
|
|
|
|
if not created:
|
|
|
|
# update the created time of the token to keep it valid
|
|
|
|
token.created = datetime.datetime.utcnow()
|
|
|
|
token.save()
|
|
|
|
|
|
|
|
return Response({'token': token.key, 'username': serializer.validated_data['user'].username, 'expiry': token.created + timedelta(hours=EXPIRE_HOURS)})
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
|
|
class TokenLogout(APIView):
|
|
|
|
def post(self, request, format=None):
|
|
|
|
try:
|
|
|
|
request.user.auth_token.delete()
|
|
|
|
except AttributeError as e:
|
|
|
|
pass
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
def get(self, request, format=None):
|
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|