2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-07 12:22:11 +00:00

Annotate models with their API list view

- It will make sense, trust me
This commit is contained in:
Oliver
2021-06-25 00:17:58 +10:00
parent b273dc613b
commit 04374c71c2
12 changed files with 153 additions and 5 deletions

View File

@@ -75,6 +75,10 @@ class PartCategory(InvenTreeTree):
default_keywords = models.CharField(null=True, blank=True, max_length=250, verbose_name=_('Default keywords'), help_text=_('Default keywords for parts in this category'))
@staticmethod
def get_api_url():
return reverse('api-part-category-list')
def get_absolute_url(self):
return reverse('category-detail', kwargs={'pk': self.id})
@@ -329,6 +333,11 @@ class Part(MPTTModel):
# For legacy reasons the 'variant_of' field is used to indicate the MPTT parent
parent_attr = 'variant_of'
@staticmethod
def get_api_url():
return reverse('api-part-list')
def get_context_data(self, request, **kwargs):
"""
Return some useful context data about this part for template rendering
@@ -1966,6 +1975,10 @@ class PartAttachment(InvenTreeAttachment):
Model for storing file attachments against a Part object
"""
@staticmethod
def get_api_url():
return reverse('api-part-attachment-list')
def getSubdir(self):
return os.path.join("part_files", str(self.part.id))
@@ -1977,6 +1990,10 @@ class PartSellPriceBreak(common.models.PriceBreak):
"""
Represents a price break for selling this part
"""
@staticmethod
def get_api_url():
return reverse('api-part-sale-price-list')
part = models.ForeignKey(
Part, on_delete=models.CASCADE,
@@ -1994,6 +2011,10 @@ class PartInternalPriceBreak(common.models.PriceBreak):
Represents a price break for internally selling this part
"""
@staticmethod
def get_api_url():
return reverse('api-part-internal-price-list')
part = models.ForeignKey(
Part, on_delete=models.CASCADE,
related_name='internalpricebreaks',
@@ -2038,6 +2059,10 @@ class PartTestTemplate(models.Model):
run on the model (refer to the validate_unique function).
"""
@staticmethod
def get_api_url():
return reverse('api-part-test-template-list')
def save(self, *args, **kwargs):
self.clean()
@@ -2136,6 +2161,10 @@ class PartParameterTemplate(models.Model):
units: The units of the Parameter [string]
"""
@staticmethod
def get_api_url():
return reverse('api-part-param-template-list')
def __str__(self):
s = str(self.name)
if self.units:
@@ -2173,6 +2202,10 @@ class PartParameter(models.Model):
data: The data (value) of the Parameter [string]
"""
@staticmethod
def get_api_url():
return reverse('api-part-param-list')
def __str__(self):
# String representation of a PartParameter (used in the admin interface)
return "{part} : {param} = {data}{units}".format(
@@ -2264,6 +2297,10 @@ class BomItem(models.Model):
allow_variants: Stock for part variants can be substituted for this BomItem
"""
@staticmethod
def get_api_url():
return reverse('api-bom-list')
def save(self, *args, **kwargs):
self.clean()

View File

@@ -4,6 +4,7 @@ JSON serializers for Part app
import imghdr
from decimal import Decimal
from django.urls import reverse_lazy
from django.db import models
from django.db.models import Q
from django.db.models.functions import Coalesce
@@ -187,6 +188,9 @@ class PartSerializer(InvenTreeModelSerializer):
Used when displaying all details of a single component.
"""
def get_api_url(self):
return reverse_lazy('api-part-list')
def __init__(self, *args, **kwargs):
"""
Custom initialization method for PartSerializer,
@@ -305,7 +309,7 @@ class PartSerializer(InvenTreeModelSerializer):
image = InvenTreeImageSerializerField(required=False, allow_null=True)
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
starred = serializers.SerializerMethodField()
starred = serializers.BooleanField()
# PrimaryKeyRelated fields (Note: enforcing field type here results in much faster queries, somehow...)
category = serializers.PrimaryKeyRelatedField(queryset=PartCategory.objects.all())