Further API improvements

This commit is contained in:
Oliver Walters 2017-04-14 12:14:50 +10:00
parent 6f9bf45e22
commit 2299cd0700
4 changed files with 33 additions and 9 deletions

View File

@ -157,6 +157,7 @@ class PartParameterManager(models.Manager):
return super(PartParameterManager, self).create(*args, **kwargs)
class PartParameter(models.Model):
""" PartParameter is associated with a single part
"""

View File

@ -55,6 +55,6 @@ urlpatterns = [
# Part templates
url(r'^templates/', include(parttemplatepatterns)),
# List of all parts
url(r'^$', views.PartList.as_view())
# List parts with optional filters
url(r'^\?*[^/]*/?$', views.PartList.as_view()),
]

View File

@ -1,3 +1,5 @@
# import django_filters
from rest_framework import generics, permissions
from .models import PartCategory, Part, PartParameter, PartParameterTemplate
@ -46,9 +48,33 @@ class PartParamDetail(generics.RetrieveUpdateDestroyAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class PartList(generics.ListCreateAPIView):
"""
class PartFilter(django_filters.rest_framework.FilterSet):
min_stock = django_filters.NumberFilter(name="stock", lookup_expr="gte")
max_stock = django_filters.NumberFilter(name="stock", lookup_expr="lte")
class Meta:
model = Part
fields = ['stock']
"""
class PartList(generics.ListCreateAPIView):
""" Display a list of parts, with optional filters
Filters are specified in the url, e.g.
/part/?category=127
/part/?min_stock=100
"""
def get_queryset(self):
parts = Part.objects.all()
cat_id = self.request.query_params.get('category', None)
if cat_id:
parts = parts.filter(category=cat_id)
return parts
queryset = Part.objects.all()
serializer_class = PartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

View File

@ -15,11 +15,8 @@ projectpartpatterns = [
# Detail of a single project part
url(r'^(?P<pk>[0-9]+)/$', views.ProjectPartDetail.as_view()),
# Parts associated with a project
url(r'^\?[^/]*/$', views.ProjectPartsList.as_view()),
# All project parts
url(r'^$', views.ProjectPartsList.as_view()),
# List project parts, with optional filters
url(r'^\?*[^/]*/?$', views.ProjectPartsList.as_view()),
]
projectcategorypatterns = [