Added /track/ API

This commit is contained in:
Oliver Walters 2017-04-14 15:49:10 +10:00
parent 37808f3834
commit 6f31e3447c
5 changed files with 123 additions and 16 deletions

View File

@ -17,13 +17,13 @@ def Inventree404(self):
urlpatterns = [ urlpatterns = [
url(r'^stock/', include('stock.urls')), url(r'^stock/?', include('stock.urls')),
url(r'^part/', include('part.urls')), url(r'^part/?', include('part.urls')),
url(r'^supplier/', include('supplier.urls')), url(r'^supplier/?', include('supplier.urls')),
url(r'^track/', include('track.urls')), url(r'^track/?', include('track.urls')),
url(r'^project/', include('project.urls')), url(r'^project/?', include('project.urls')),
url(r'^admin/', admin.site.urls), url(r'^admin/?', admin.site.urls),
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')), url(r'^auth/?', include('rest_framework.urls', namespace='rest_framework')),
# Any other URL # Any other URL
url(r'', Inventree404) url(r'', Inventree404)

View File

@ -57,7 +57,6 @@ class PartTrackingInfo(models.Model):
a new PartTrackingInfo object should be created. a new PartTrackingInfo object should be created.
""" """
part = models.ForeignKey(UniquePart, on_delete=models.CASCADE) part = models.ForeignKey(UniquePart, on_delete=models.CASCADE, related_name='tracking_info')
date = models.DateField(auto_now_add=True, date = models.DateField(auto_now_add=True, editable=False)
editable=False)
notes = models.CharField(max_length=500) notes = models.CharField(max_length=500)

View File

@ -0,0 +1,27 @@
from rest_framework import serializers
from .models import UniquePart, PartTrackingInfo
class UniquePartSerializer(serializers.ModelSerializer):
tracking_info = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = UniquePart
fields = ['pk',
'part',
'revision',
'creation_date',
'serial',
'createdBy',
'customer',
'status',
'tracking_info']
class PartTrackingInfoSerializer(serializers.ModelSerializer):
class Meta:
model = PartTrackingInfo
fields = '__all__'

View File

@ -1,7 +1,19 @@
from django.conf.urls import url from django.conf.urls import url, include
from . import views from . import views
urlpatterns = [ infopatterns = [
url(r'^$', views.index, name='index') url(r'^(?P<pk>[0-9]+)/?$', views.PartTrackingDetail.as_view()),
url(r'^\?*[^/]*/?$', views.PartTrackingList.as_view())
]
urlpatterns = [
url(r'info/?', include(infopatterns)),
# Detail for a single unique part
url(r'^(?P<pk>[0-9]+)$', views.UniquePartDetail.as_view()),
# List all unique parts, with optional filters
url(r'^\?*[^/]*/?$', views.UniquePartList.as_view()),
] ]

View File

@ -1,5 +1,74 @@
from django.http import HttpResponse import django_filters
from rest_framework import generics, permissions
from .models import UniquePart, PartTrackingInfo
from .serializers import UniquePartSerializer, PartTrackingInfoSerializer
def index(request): class UniquePartDetail(generics.RetrieveUpdateDestroyAPIView):
return HttpResponse("This is the Tracking page")
queryset = UniquePart.objects.all()
serializer_class = UniquePartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class UniquePartFilter(django_filters.rest_framework.FilterSet):
# Filter based on serial number
min_sn = django_filters.NumberFilter(name='serial', lookup_expr='gte')
max_sn = django_filters.NumberFilter(name='serial', lookup_expr='lte')
class Meta:
model = UniquePart
fields = ['serial',]
class UniquePartList(generics.ListCreateAPIView):
serializer_class = UniquePartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,)
filter_class = UniquePartFilter
def get_queryset(self):
parts = UniquePart.objects.all()
query = self.request.query_params
# Filter by associated part
part_id = query.get('part', None)
if part_id:
parts = parts.filter(part=part_id)
# Filter by serial number
sn = query.get('sn', None)
if sn:
parts = parts.filter(serial=sn)
# Filter by customer
customer = query.get('customer', None)
if customer:
parts = parts.filter(customer=customer)
return parts
class PartTrackingDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = PartTrackingInfo.objects.all()
serializer_class = PartTrackingInfoSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class PartTrackingList(generics.ListCreateAPIView):
serializer_class = PartTrackingInfoSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def get_queryset(self):
tracking = PartTrackingInfo.objects.all()
query = self.request.query_params
part_id = query.get('part', None)
if part_id:
tracking = tracking.filter(part=part_id)
return tracking