Merge pull request #28 from SchrodingersGat/master

Added project API
This commit is contained in:
Oliver 2017-04-11 23:15:44 +10:00 committed by GitHub
commit 3c42f12ea0
4 changed files with 119 additions and 5 deletions

View File

@ -17,6 +17,10 @@ class ProjectCategory(InvenTreeTree):
verbose_name = "Project Category"
verbose_name_plural = "Project Categories"
@property
def projects(self):
return self.project_set.all()
class Project(models.Model):
""" A Project takes multiple Part objects.
@ -57,7 +61,7 @@ class ProjectPart(models.Model):
quantity = models.PositiveIntegerField(default=1)
overage = models.FloatField(default=0)
overage_type = models.PositiveIntegerField(
default=1,
default=OVERAGE_ABSOLUTE,
choices=OVARAGE_CODES.items())
def __str__(self):

View File

@ -0,0 +1,63 @@
from rest_framework import serializers
from .models import ProjectCategory, Project, ProjectPart
class ProjectPartSerializer(serializers.ModelSerializer):
class Meta:
model = ProjectPart
fields = ('pk',
'part',
'project',
'quantity',
'overage',
'overage_type')
class ProjectBriefSerializer(serializers.ModelSerializer):
""" Serializer for displaying brief overview of a project
"""
class Meta:
model = Project
fields = ('pk',
'name',
'description',
'category')
class ProjectDetailSerializer(serializers.ModelSerializer):
""" Serializer for detailed project information
"""
class Meta:
model = Project
fields = ('pk',
'name',
'description',
'category')
class ProjectCategoryBriefSerializer(serializers.ModelSerializer):
class Meta:
model = ProjectCategory
fields = ('pk', 'name', 'description')
class ProjectCategoryDetailSerializer(serializers.ModelSerializer):
projects = ProjectBriefSerializer(many=True)
children = ProjectCategoryBriefSerializer(many=True)
class Meta:
model = ProjectCategory
fields = ('pk',
'name',
'description',
'parent',
'path',
'children',
'projects')

View File

@ -3,5 +3,18 @@ from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
# Single project detail
url(r'^(?P<pk>[0-9]+)/$', views.ProjectDetail.as_view()),
# Parts associated with a project
url(r'^(?P<pk>[0-9]+)/parts$', views.ProjectPartsList.as_view()),
# List of all projects
url(r'^$', views.ProjectList.as_view()),
# List of top-level project categories
url(r'^category/$', views.ProjectCategoryList.as_view()),
# Detail of a single project category
url(r'^category/(?P<pk>[0-9]+)/$', views.ProjectCategoryDetail.as_view())
]

View File

@ -1,5 +1,39 @@
from django.http import HttpResponse
from rest_framework import generics
from .models import ProjectCategory, Project, ProjectPart
from .serializers import ProjectBriefSerializer, ProjectDetailSerializer
from .serializers import ProjectCategoryDetailSerializer
from .serializers import ProjectPartSerializer
def index(request):
return HttpResponse("This is the Projects page")
class ProjectDetail(generics.RetrieveAPIView):
queryset = Project.objects.all()
serializer_class = ProjectDetailSerializer
class ProjectList(generics.ListAPIView):
queryset = Project.objects.all()
serializer_class = ProjectBriefSerializer
class ProjectCategoryDetail(generics.RetrieveAPIView):
queryset = ProjectCategory.objects.all()
serializer_class = ProjectCategoryDetailSerializer
class ProjectCategoryList(generics.ListAPIView):
queryset = ProjectCategory.objects.filter(parent=None)
serializer_class = ProjectCategoryDetailSerializer
class ProjectPartsList(generics.ListAPIView):
serializer_class = ProjectPartSerializer
def get_queryset(self):
project_id = self.kwargs['pk']
return ProjectPart.objects.filter(project=project_id)