mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-02 21:38:48 +00:00
Added PartDetailFromIPN view (subclass of PartDetail) and url pattern
This commit is contained in:
parent
11b751323e
commit
2fa284d37b
@ -99,8 +99,10 @@ part_urls = [
|
|||||||
# Export data for multiple parts
|
# Export data for multiple parts
|
||||||
url(r'^export/', views.PartExport.as_view(), name='part-export'),
|
url(r'^export/', views.PartExport.as_view(), name='part-export'),
|
||||||
|
|
||||||
# Individual part
|
# Individual part using pk
|
||||||
url(r'^(?P<pk>\d+)/', include(part_detail_urls)),
|
url(r'^(?P<pk>\d+)/', include(part_detail_urls)),
|
||||||
|
# Individual part using IPN as slug
|
||||||
|
url(r'^(?P<slug>[-\w]+)/', views.PartDetailFromIPN.as_view(), name='part-detail-from-ipn'),
|
||||||
|
|
||||||
# Part category
|
# Part category
|
||||||
url(r'^category/(?P<pk>\d+)/', include(part_category_urls)),
|
url(r'^category/(?P<pk>\d+)/', include(part_category_urls)),
|
||||||
|
@ -647,6 +647,41 @@ class PartDetail(DetailView):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
class PartDetailFromIPN(PartDetail):
|
||||||
|
slug_field = 'IPN'
|
||||||
|
slug_url_kwarg = 'slug'
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
""" Return Part object which IPN field matches the slug value """
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
# Get slug
|
||||||
|
slug = self.kwargs.get(self.slug_url_kwarg)
|
||||||
|
|
||||||
|
if slug is not None:
|
||||||
|
slug_field = self.get_slug_field()
|
||||||
|
# Filter by the slug value
|
||||||
|
queryset = queryset.filter(**{slug_field: slug})
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Get unique part from queryset
|
||||||
|
part = queryset.get()
|
||||||
|
# Return Part object
|
||||||
|
return part
|
||||||
|
except queryset.model.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
""" Attempt to match slug to a Part, else redirect to PartIndex view """
|
||||||
|
self.object = self.get_object()
|
||||||
|
|
||||||
|
if not self.object:
|
||||||
|
return HttpResponseRedirect(reverse('part-index'))
|
||||||
|
|
||||||
|
return super(PartDetailFromIPN, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class PartQRCode(QRCodeView):
|
class PartQRCode(QRCodeView):
|
||||||
""" View for displaying a QR code for a Part object """
|
""" View for displaying a QR code for a Part object """
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user