-
-
Notifications
You must be signed in to change notification settings - Fork 970
Supplier Mixin #9761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wolflu05
wants to merge
9
commits into
inventree:master
Choose a base branch
from
wolflu05:supplier-mixin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Supplier Mixin #9761
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c6fedb
commit initial draft for supplier import
wolflu05 51e4139
complete import wizard
wolflu05 adebfc4
allow importing only mp and sp
wolflu05 e26faf1
improved sample supplier plugin
wolflu05 1d90aa9
add docs
wolflu05 fdc1e24
add tests
wolflu05 fcead76
Merge remote-tracking branch 'upstream/master' into supplier-mixin
wolflu05 d1a03e6
bump api version
wolflu05 8627e4e
fix schema docu
wolflu05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
"""API views for supplier plugins in InvenTree.""" | ||
|
||
from django.db import transaction | ||
from django.urls import path | ||
|
||
from rest_framework import status | ||
from rest_framework.exceptions import NotFound | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from InvenTree import permissions | ||
from part.models import PartCategoryParameterTemplate | ||
from plugin import registry | ||
from plugin.plugin import PluginMixinEnum | ||
|
||
from .serializers import ( | ||
ImportRequestSerializer, | ||
ImportResultSerializer, | ||
SearchResultSerializer, | ||
) | ||
|
||
# from .supplier import ImportParameter, PartNotFoundError | ||
wolflu05 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class SearchPart(APIView): | ||
"""Search parts by supplier. | ||
|
||
- GET: Start part search | ||
""" | ||
|
||
role_required = 'part.add' | ||
permission_classes = [ | ||
permissions.IsAuthenticatedOrReadScope, | ||
permissions.RolePermission, | ||
] | ||
|
||
def get(self, request): | ||
"""Search parts by supplier.""" | ||
supplier_slug = request.query_params.get('supplier', '') | ||
|
||
supplier = None | ||
for plugin in registry.with_mixin(PluginMixinEnum.SUPPLIER): | ||
if plugin.slug == supplier_slug: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the "supplier" supposed to be the name of the supplier (e.g. |
||
supplier = plugin | ||
break | ||
|
||
if not supplier: | ||
raise NotFound(detail=f"Supplier '{supplier_slug}' not found") | ||
|
||
term = request.query_params.get('term', '') | ||
try: | ||
results = supplier.get_search_results(term) | ||
except Exception as e: | ||
return Response( | ||
{'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR | ||
) | ||
|
||
response = SearchResultSerializer(results, many=True).data | ||
return Response(response) | ||
|
||
|
||
class ImportPart(APIView): | ||
"""Import a part by supplier. | ||
|
||
- POST: Attempt to import part by sku | ||
""" | ||
|
||
role_required = 'part.add' | ||
permission_classes = [ | ||
permissions.IsAuthenticatedOrReadScope, | ||
permissions.RolePermission, | ||
] | ||
|
||
def post(self, request): | ||
"""Import a part by supplier.""" | ||
serializer = ImportRequestSerializer(data=request.data) | ||
if not serializer.is_valid(): | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
# Extract validated data | ||
supplier_slug = serializer.validated_data.get('supplier', '') | ||
part_import_id = serializer.validated_data.get('part_import_id', None) | ||
category = serializer.validated_data.get('category_id', None) | ||
part = serializer.validated_data.get('part_id', None) | ||
|
||
# Find the supplier plugin | ||
supplier = None | ||
for plugin in registry.with_mixin(PluginMixinEnum.SUPPLIER): | ||
if plugin.slug == supplier_slug: | ||
supplier = plugin | ||
break | ||
|
||
# Validate supplier and part/category | ||
if not supplier: | ||
raise NotFound(detail=f"Supplier '{supplier_slug}' not found") | ||
if not part and not category: | ||
return Response( | ||
{ | ||
'detail': "'category_id' is not provided, but required if no part_id is provided" | ||
}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
from plugin.base.supplier.mixins import SupplierMixin | ||
|
||
# Import part data | ||
try: | ||
import_data = supplier.get_import_data(part_import_id) | ||
|
||
with transaction.atomic(): | ||
# create part if it does not exist | ||
if not part: | ||
part = supplier.import_part( | ||
import_data, category=category, creation_user=request.user | ||
) | ||
|
||
# create manufacturer part | ||
manufacturer_part = supplier.import_manufacturer_part( | ||
import_data, part=part | ||
) | ||
|
||
# create supplier part | ||
supplier_part = supplier.import_supplier_part( | ||
import_data, part=part, manufacturer_part=manufacturer_part | ||
) | ||
|
||
# set default supplier if not set | ||
if not part.default_supplier: | ||
part.default_supplier = supplier_part | ||
part.save() | ||
|
||
# get pricing | ||
pricing = supplier.get_pricing_data(import_data) | ||
|
||
# get parameters | ||
parameters = supplier.get_parameters(import_data) | ||
except SupplierMixin.PartNotFoundError: | ||
return Response( | ||
{'detail': f"Part with id: '{part_import_id}' not found"}, | ||
status=status.HTTP_404_NOT_FOUND, | ||
) | ||
except Exception as e: | ||
return Response( | ||
{'detail': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR | ||
) | ||
|
||
# add default parameters for category | ||
if category: | ||
categories = category.get_ancestors(include_self=True) | ||
category_parameters = PartCategoryParameterTemplate.objects.filter( | ||
category__in=categories | ||
) | ||
|
||
for c in category_parameters: | ||
for p in parameters: | ||
if p.parameter_template == c.parameter_template: | ||
p.on_category = True | ||
p.value = p.value if p.value is not None else c.default_value | ||
break | ||
else: | ||
parameters.append( | ||
SupplierMixin.ImportParameter( | ||
name=c.parameter_template.name, | ||
value=c.default_value, | ||
on_category=True, | ||
parameter_template=c.parameter_template, | ||
) | ||
) | ||
parameters.sort(key=lambda x: x.on_category, reverse=True) | ||
|
||
response = ImportResultSerializer({ | ||
'part_id': part.pk, | ||
'part_detail': part, | ||
'supplier_part_id': supplier_part.pk, | ||
'manufacturer_part_id': manufacturer_part.pk, | ||
'pricing': pricing, | ||
'parameters': parameters, | ||
}).data | ||
return Response(response) | ||
|
||
|
||
supplier_api_urls = [ | ||
path('search/', SearchPart.as_view(), name='api-supplier-search'), | ||
path('import/', ImportPart.as_view(), name='api-supplier-import'), | ||
] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wolflu05 it might be worth looking into a
BulkCreateMixin
class here, to make this a generic approach.I recently added in a
BulkUpdateMixin
- #9313 - which employs a very similar approach. The intent here is to:So, thoughts? A
BulkCreateMixin
would complement theBulkUpdateMixin
andBulkDeleteMixin
classess nicely!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I'll see what I can do and if I need any pointers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you submit that as a separate PR first? I'd like to be able to review that separately