From 7efb4c21d3c9d4e9e3a8ae128661e0e6de600c31 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 16 Apr 2017 17:50:28 +1000 Subject: [PATCH] Added ProjectRun API --- InvenTree/InvenTree/urls.py | 3 +- InvenTree/project/models.py | 2 +- InvenTree/project/serializers.py | 14 ++++++- InvenTree/project/urls.py | 9 +++++ InvenTree/project/views.py | 69 +++++++++++++++++++++++++------- 5 files changed, 79 insertions(+), 18 deletions(-) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index bab9e71793..e50470dc1c 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -5,7 +5,7 @@ from rest_framework.documentation import include_docs_urls from part.urls import part_urls, part_cat_urls, part_param_urls, part_param_template_urls from stock.urls import stock_urls, stock_loc_urls, stock_track_urls -from project.urls import prj_urls, prj_part_urls, prj_cat_urls +from project.urls import prj_urls, prj_part_urls, prj_cat_urls, prj_run_urls from supplier.urls import cust_urls, manu_urls, supplier_part_urls, price_break_urls, supplier_urls from track.urls import unique_urls, part_track_urls @@ -39,6 +39,7 @@ apipatterns = [ url(r'^project/', include(prj_urls)), url(r'^project-category/', include(prj_cat_urls)), url(r'^project-part/', include(prj_part_urls)), + url(r'^project-run/', include(prj_run_urls)), ] urlpatterns = [ diff --git a/InvenTree/project/models.py b/InvenTree/project/models.py index 99228deb22..a36be41562 100644 --- a/InvenTree/project/models.py +++ b/InvenTree/project/models.py @@ -93,4 +93,4 @@ class ProjectRun(models.Model): project = models.ForeignKey(Project, on_delete=models.CASCADE) quantity = models.PositiveIntegerField(default=1, validators=[MinValueValidator(0)]) - run_date = models.DateField(auto_now_add=True) + run_date = models.DateField(blank=True, null=True) diff --git a/InvenTree/project/serializers.py b/InvenTree/project/serializers.py index 1b854da4f1..2088fc5f52 100644 --- a/InvenTree/project/serializers.py +++ b/InvenTree/project/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from .models import ProjectCategory, Project, ProjectPart +from .models import ProjectCategory, Project, ProjectPart, ProjectRun class ProjectPartSerializer(serializers.HyperlinkedModelSerializer): @@ -33,3 +33,15 @@ class ProjectCategorySerializer(serializers.HyperlinkedModelSerializer): 'description', 'parent', 'path') + + +class ProjectRunSerializer(serializers.HyperlinkedModelSerializer): + + class Meta: + model = ProjectRun + fields = ('url', + 'project', + 'quantity', + 'run_date') + + read_only_fields = ('run_date',) diff --git a/InvenTree/project/urls.py b/InvenTree/project/urls.py index ad150a00ca..e95a78fd82 100644 --- a/InvenTree/project/urls.py +++ b/InvenTree/project/urls.py @@ -28,3 +28,12 @@ prj_urls = [ url(r'^\?.*/?$', views.ProjectList.as_view()), url(r'^$', views.ProjectList.as_view()) ] + +prj_run_urls = [ + # Individual project URL + url(r'^(?P[0-9]+)/?$', views.ProjectRunDetail.as_view(), name='projectrun-detail'), + + # List of all projects + url(r'^\?.*/?$', views.ProjectRunList.as_view()), + url(r'^$', views.ProjectRunList.as_view()) +] diff --git a/InvenTree/project/views.py b/InvenTree/project/views.py index 593407ae63..4aa12efa2b 100644 --- a/InvenTree/project/views.py +++ b/InvenTree/project/views.py @@ -2,10 +2,11 @@ from django_filters.rest_framework import FilterSet, DjangoFilterBackend from rest_framework import generics, permissions from InvenTree.models import FilterChildren -from .models import ProjectCategory, Project, ProjectPart +from .models import ProjectCategory, Project, ProjectPart, ProjectRun from .serializers import ProjectSerializer from .serializers import ProjectCategorySerializer from .serializers import ProjectPartSerializer +from .serializers import ProjectRunSerializer class ProjectDetail(generics.RetrieveUpdateDestroyAPIView): @@ -96,6 +97,13 @@ class ProjectCategoryList(generics.ListCreateAPIView): permission_classes = (permissions.IsAuthenticatedOrReadOnly,) +class ProjectPartFilter(FilterSet): + + class Meta: + model = ProjectPart + fields = ['project', 'part'] + + class ProjectPartsList(generics.ListCreateAPIView): """ @@ -109,20 +117,9 @@ class ProjectPartsList(generics.ListCreateAPIView): serializer_class = ProjectPartSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) - - def get_queryset(self): - parts = ProjectPart.objects.all() - params = self.request.query_params - - project_id = params.get('project', None) - if project_id: - parts = parts.filter(project=project_id) - - part_id = params.get('part', None) - if part_id: - parts = parts.filter(part=part_id) - - return parts + queryset = ProjectPart.objects.all() + filter_backends = (DjangoFilterBackend,) + filter_class = ProjectPartFilter class ProjectPartDetail(generics.RetrieveUpdateDestroyAPIView): @@ -142,3 +139,45 @@ class ProjectPartDetail(generics.RetrieveUpdateDestroyAPIView): queryset = ProjectPart.objects.all() serializer_class = ProjectPartSerializer permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + + +class ProjectRunDetail(generics.RetrieveUpdateDestroyAPIView): + """ + + get: + Return a single ProjectRun + + post: + Update a ProjectRun + + delete: + Remove a ProjectRun + """ + + queryset = ProjectRun.objects.all() + serializer_class = ProjectRunSerializer + permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + + +class ProjectRunFilter(FilterSet): + + class Meta: + model = ProjectRun + fields = ['project'] + + +class ProjectRunList(generics.ListCreateAPIView): + """ + + get: + Return a list of all ProjectRun objects + + post: + Create a new ProjectRun object + """ + + queryset = ProjectRun.objects.all() + serializer_class = ProjectRunSerializer + permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + filter_backends = (DjangoFilterBackend,) + filter_class = ProjectRunFilter