From 7d7579d3d62df45a8c824bba9d2cefb4985d957c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 15 Apr 2017 00:55:30 +1000 Subject: [PATCH] Style fixes --- InvenTree/InvenTree/urls.py | 6 +-- InvenTree/stock/location_urls.py | 10 +++++ InvenTree/stock/urls.py | 13 +----- InvenTree/stock/views.py | 55 ++++++++++------------- InvenTree/supplier/customer_urls.py | 2 +- InvenTree/supplier/manufacturer_urls.py | 2 +- InvenTree/supplier/part_urls.py | 10 +++++ InvenTree/supplier/price_urls.py | 10 +++++ InvenTree/supplier/serializers.py | 6 +-- InvenTree/supplier/urls.py | 15 +------ InvenTree/supplier/views.py | 59 ++++++++++++++----------- InvenTree/track/models.py | 2 +- 12 files changed, 97 insertions(+), 93 deletions(-) create mode 100644 InvenTree/stock/location_urls.py create mode 100644 InvenTree/supplier/part_urls.py create mode 100644 InvenTree/supplier/price_urls.py diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 46ab1311d0..63014a36d1 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -1,17 +1,17 @@ from django.conf.urls import url, include from django.contrib import admin -from rest_framework import status -from rest_framework.response import Response -from rest_framework.decorators import api_view from rest_framework.documentation import include_docs_urls admin.site.site_header = "InvenTree Admin" apipatterns = [ url(r'^stock/', include('stock.urls')), + url(r'^stock-location/', include('stock.location_urls')), url(r'^part/', include('part.urls')), url(r'^supplier/', include('supplier.urls')), + url(r'^supplier-part/', include('supplier.part_urls')), + url(r'^price-break/', include('supplier.price_urls')), url(r'^manufacturer/', include('supplier.manufacturer_urls')), url(r'^customer/', include('supplier.customer_urls')), url(r'^track/', include('track.urls')), diff --git a/InvenTree/stock/location_urls.py b/InvenTree/stock/location_urls.py new file mode 100644 index 0000000000..9df7a53b28 --- /dev/null +++ b/InvenTree/stock/location_urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import url +from . import views + +urlpatterns = [ + url(r'^(?P[0-9]+)/?$', views.LocationDetail.as_view(), name='stocklocation-detail'), + + url(r'^\?.*/?$', views.LocationList.as_view()), + + url(r'^$', views.LocationList.as_view()) +] diff --git a/InvenTree/stock/urls.py b/InvenTree/stock/urls.py index bb026d2a08..b2822682d5 100644 --- a/InvenTree/stock/urls.py +++ b/InvenTree/stock/urls.py @@ -1,19 +1,8 @@ -from django.conf.urls import url, include +from django.conf.urls import url from . import views -locpatterns = [ - url(r'^(?P[0-9]+)/?$', views.LocationDetail.as_view(), name='stocklocation-detail'), - - url(r'^\?.*/?$', views.LocationList.as_view()), - - url(r'^$', views.LocationList.as_view()) -] - urlpatterns = [ - # Stock location urls - url(r'^location/', include(locpatterns)), - # Detail for a single stock item url(r'^(?P[0-9]+)/?$', views.StockDetail.as_view(), name='stockitem-detail'), diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index fa32ce234d..27fa65bfbe 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -1,7 +1,10 @@ -from rest_framework import generics, permissions import django_filters +from django_filters.rest_framework import FilterSet, DjangoFilterBackend +from django_filters import NumberFilter -from InvenTree.models import FilterChildren +from rest_framework import generics, permissions + +# from InvenTree.models import FilterChildren from .models import StockLocation, StockItem from .serializers import StockItemSerializer, LocationSerializer @@ -14,37 +17,24 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView): class StockFilter(django_filters.rest_framework.FilterSet): - min_stock = django_filters.NumberFilter(name='quantity', lookup_expr='gte') - max_stock = django_filters.NumberFilter(name='quantity', lookup_expr='lte') + min_stock = NumberFilter(name='quantity', lookup_expr='gte') + max_stock = NumberFilter(name='quantity', lookup_expr='lte') + part = NumberFilter(name='part', lookup_expr='exact') + location = NumberFilter(name='location', lookup_expr='exact') class Meta: model = StockItem - fields = ['quantity'] + fields = ['quantity', 'part', 'location'] class StockList(generics.ListCreateAPIView): + queryset = StockItem.objects.all() serializer_class = StockItemSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) + filter_backends = (DjangoFilterBackend,) filter_class = StockFilter - def get_queryset(self): - items = StockItem.objects.all() - - # Specify a particular part - part_id = self.request.query_params.get('part', None) - if part_id: - items = items.filter(part=part_id) - - # Specify a particular location - loc_id = self.request.query_params.get('location', None) - - if loc_id: - items = items.filter(location=loc_id) - - return items - class LocationDetail(generics.RetrieveUpdateDestroyAPIView): """ Return information on a specific stock location @@ -55,19 +45,22 @@ class LocationDetail(generics.RetrieveUpdateDestroyAPIView): permission_classes = (permissions.IsAuthenticatedOrReadOnly,) +class StockLocationFilter(FilterSet): + + parent = NumberFilter(name='parent', lookup_expr='exact') + + class Meta: + model = StockLocation + fields = ['parent'] + + class LocationList(generics.ListCreateAPIView): """ Return a list of top-level locations Locations are considered "top-level" if they do not have a parent """ - def get_queryset(self): - params = self.request.query_params - - locations = StockLocation.objects.all() - - locations = FilterChildren(locations, params.get('parent', None)) - - return locations - + queryset = StockLocation.objects.all() serializer_class = LocationSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + filter_backends = (DjangoFilterBackend,) + filter_class = StockLocationFilter diff --git a/InvenTree/supplier/customer_urls.py b/InvenTree/supplier/customer_urls.py index 870bba1a31..9bfc222074 100644 --- a/InvenTree/supplier/customer_urls.py +++ b/InvenTree/supplier/customer_urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url, include +from django.conf.urls import url from . import views diff --git a/InvenTree/supplier/manufacturer_urls.py b/InvenTree/supplier/manufacturer_urls.py index 56a74c8106..9586990f1b 100644 --- a/InvenTree/supplier/manufacturer_urls.py +++ b/InvenTree/supplier/manufacturer_urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url, include +from django.conf.urls import url from . import views diff --git a/InvenTree/supplier/part_urls.py b/InvenTree/supplier/part_urls.py new file mode 100644 index 0000000000..813e4ce37e --- /dev/null +++ b/InvenTree/supplier/part_urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import url + +from . import views + +urlpatterns = [ + url(r'^(?P[0-9]+)/?$', views.SupplierPartDetail.as_view(), name='supplierpart-detail'), + + url(r'^\?.*/?$', views.SupplierPartList.as_view()), + url(r'^$', views.SupplierPartList.as_view()) +] diff --git a/InvenTree/supplier/price_urls.py b/InvenTree/supplier/price_urls.py new file mode 100644 index 0000000000..044f22b289 --- /dev/null +++ b/InvenTree/supplier/price_urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import url + +from . import views + +urlpatterns = [ + url(r'^(?P[0-9]+)/?$', views.SupplierPriceBreakDetail.as_view(), name='supplierpricebreak-detail'), + + url(r'^\?.*/?$', views.SupplierPriceBreakList.as_view()), + url(r'^$', views.SupplierPriceBreakList.as_view()) +] diff --git a/InvenTree/supplier/serializers.py b/InvenTree/supplier/serializers.py index 88c23a573c..5aa278ed4b 100644 --- a/InvenTree/supplier/serializers.py +++ b/InvenTree/supplier/serializers.py @@ -35,13 +35,13 @@ class SupplierPartSerializer(serializers.ModelSerializer): view_name='supplierpricebreak-detail') part = serializers.HyperlinkedRelatedField(view_name='part-detail', - queryset = Part.objects.all()) + queryset=Part.objects.all()) supplier = serializers.HyperlinkedRelatedField(view_name='supplier-detail', - queryset = Supplier.objects.all()) + queryset=Supplier.objects.all()) manufacturer = serializers.HyperlinkedRelatedField(view_name='manufacturer-detail', - queryset = Manufacturer.objects.all()) + queryset=Manufacturer.objects.all()) class Meta: model = SupplierPart diff --git a/InvenTree/supplier/urls.py b/InvenTree/supplier/urls.py index 5efac75d31..f810662976 100644 --- a/InvenTree/supplier/urls.py +++ b/InvenTree/supplier/urls.py @@ -1,14 +1,7 @@ -from django.conf.urls import url, include +from django.conf.urls import url from . import views -partpatterns = [ - url(r'^(?P[0-9]+)/?$', views.SupplierPartDetail.as_view(), name='supplierpart-detail'), - - url(r'^\?.*/?$', views.SupplierPartList.as_view()), - url(r'^$', views.SupplierPartList.as_view()) -] - pricepatterns = [ url(r'^(?P[0-9]+)/?$', views.SupplierPriceBreakDetail.as_view(), name='supplierpricebreak-detail'), @@ -18,12 +11,6 @@ pricepatterns = [ urlpatterns = [ - # Supplier part information - url(r'part/', include(partpatterns)), - - # Supplier price information - url(r'price/', include(pricepatterns)), - # Display details of a supplier url(r'^(?P[0-9]+)/$', views.SupplierDetail.as_view(), name='supplier-detail'), diff --git a/InvenTree/supplier/views.py b/InvenTree/supplier/views.py index a12a7f5124..5867ddea67 100644 --- a/InvenTree/supplier/views.py +++ b/InvenTree/supplier/views.py @@ -1,3 +1,6 @@ +from django_filters.rest_framework import FilterSet, DjangoFilterBackend +from django_filters import NumberFilter + from rest_framework import generics, permissions from .models import Supplier, SupplierPart, SupplierPriceBreak @@ -58,28 +61,27 @@ class SupplierPartDetail(generics.RetrieveUpdateDestroyAPIView): permission_classes = (permissions.IsAuthenticatedOrReadOnly,) +class SupplierPartFilter(FilterSet): + + supplier = NumberFilter(name='supplier', lookup_expr='exact') + + part = NumberFilter(name='part', lookup_expr='exact') + + manufacturer = NumberFilter(name='manufacturer', lookup_expr='exact') + + class Meta: + model = SupplierPart + fields = ['supplier', 'part', 'manufacturer'] + + class SupplierPartList(generics.ListCreateAPIView): + queryset = SupplierPart.objects.all() serializer_class = SupplierPartSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) - def get_queryset(self): - parts = SupplierPart.objects.all() - params = self.request.query_params - - supplier_id = params.get('supplier', None) - if supplier_id: - parts = parts.filter(supplier=supplier_id) - - part_id = params.get('part', None) - if part_id: - parts = parts.filter(part=part_id) - - manu_id = params.get('manufacturer', None) - if manu_id: - parts = parts.filter(manufacturer=manu_id) - - return parts + filter_backends = (DjangoFilterBackend,) + filter_class = SupplierPartFilter class SupplierPriceBreakDetail(generics.RetrieveUpdateDestroyAPIView): @@ -89,17 +91,20 @@ class SupplierPriceBreakDetail(generics.RetrieveUpdateDestroyAPIView): permission_classes = (permissions.IsAuthenticatedOrReadOnly,) +class PriceBreakFilter(FilterSet): + + part = NumberFilter(name='part', lookup_expr='exact') + + class Meta: + model = SupplierPriceBreak + fields = ['part'] + + class SupplierPriceBreakList(generics.ListCreateAPIView): - def get_queryset(self): - prices = SupplierPriceBreak.objects.all() - params = self.request.query_params - - part_id = params.get('part', None) - if part_id: - prices = prices.filter(part=part_id) - - return prices - + queryset = SupplierPriceBreak.objects.all() serializer_class = SupplierPriceBreakSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + + filter_backends = (DjangoFilterBackend,) + filter_class = PriceBreakFilter diff --git a/InvenTree/track/models.py b/InvenTree/track/models.py index d0e8a5aa46..1bc189ae7c 100644 --- a/InvenTree/track/models.py +++ b/InvenTree/track/models.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.core.exceptions import ValidationError from django.utils.translation import ugettext as _ from django.db import models -from django.contrib.auth.models import User +# from django.contrib.auth.models import User from supplier.models import Customer from part.models import Part, PartRevision