Example #1
0
    def test_installed(self):
        """Test that a locate plugin is actually installed."""
        plugins = registry.with_mixin('locate')

        self.assertTrue(len(plugins) > 0)

        self.assertTrue('samplelocate' in [p.slug for p in plugins])
Example #2
0
    def post(self, request, *args, **kwargs):
        """This function checks if all required info was submitted and then performs a plugin_action or returns an error."""
        action = request.data.get('action', None)

        data = request.data.get('data', None)

        if action is None:
            return Response({'error': _("No action specified")})

        action_plugins = registry.with_mixin('action')
        for plugin in action_plugins:
            if plugin.action_name() == action:
                plugin.perform_action(request.user, data=data)
                return Response(plugin.get_response(request.user, data=data))

        # If we got to here, no matching action was found
        return Response({
            'error': _("No matching action found"),
            "action": action,
        })
Example #3
0
    def post(self, request, *args, **kwargs):

        action = request.data.get('action', None)

        data = request.data.get('data', None)

        if action is None:
            return Response({'error': _("No action specified")})

        action_plugins = registry.with_mixin('action')
        for plugin in action_plugins:
            if plugin.action_name() == action:
                plugin.perform_action(request.user, data=data)
                return Response(plugin.get_response(request.user, data=data))

        # If we got to here, no matching action was found
        return Response({
            'error': _("No matching action found"),
            "action": action,
        })
Example #4
0
    def post(self, request, *args, **kwargs):
        """
        Respond to a barcode POST request
        """

        data = request.data

        if 'barcode' not in data:
            raise ValidationError(
                {'barcode': _('Must provide barcode_data parameter')})

        plugins = registry.with_mixin('barcode')

        barcode_data = data.get('barcode')

        # Ensure that the default barcode handler is installed
        plugins.append(InvenTreeBarcodePlugin())

        # Look for a barcode plugin which knows how to deal with this barcode
        plugin = None

        for current_plugin in plugins:
            current_plugin.init(barcode_data)

            if current_plugin.validate():
                plugin = current_plugin
                break

        match_found = False
        response = {}

        response['barcode_data'] = barcode_data

        # A plugin has been found!
        if plugin is not None:

            # Try to associate with a stock item
            item = plugin.getStockItem()

            if item is None:
                item = plugin.getStockItemByHash()

            if item is not None:
                response['stockitem'] = plugin.renderStockItem(item)
                response['url'] = reverse('stock-item-detail',
                                          kwargs={'pk': item.id})
                match_found = True

            # Try to associate with a stock location
            loc = plugin.getStockLocation()

            if loc is not None:
                response['stocklocation'] = plugin.renderStockLocation(loc)
                response['url'] = reverse('stock-location-detail',
                                          kwargs={'pk': loc.id})
                match_found = True

            # Try to associate with a part
            part = plugin.getPart()

            if part is not None:
                response['part'] = plugin.renderPart(part)
                response['url'] = reverse('part-detail',
                                          kwargs={'pk': part.id})
                match_found = True

            response['hash'] = plugin.hash()
            response['plugin'] = plugin.name

        # No plugin is found!
        # However, the hash of the barcode may still be associated with a StockItem!
        else:
            result_hash = hash_barcode(barcode_data)

            response['hash'] = result_hash
            response['plugin'] = None

            # Try to look for a matching StockItem
            try:
                item = StockItem.objects.get(uid=result_hash)
                serializer = StockItemSerializer(item,
                                                 part_detail=True,
                                                 location_detail=True,
                                                 supplier_part_detail=True)
                response['stockitem'] = serializer.data
                response['url'] = reverse('stock-item-detail',
                                          kwargs={'pk': item.id})
                match_found = True
            except StockItem.DoesNotExist:
                pass

        if not match_found:
            response['error'] = _('No match found for barcode data')
        else:
            response['success'] = _('Match found for barcode data')

        return Response(response)
Example #5
0
    def post(self, request, *args, **kwargs):

        data = request.data

        if 'barcode' not in data:
            raise ValidationError(
                {'barcode': _('Must provide barcode_data parameter')})

        if 'stockitem' not in data:
            raise ValidationError(
                {'stockitem': _('Must provide stockitem parameter')})

        barcode_data = data['barcode']

        try:
            item = StockItem.objects.get(pk=data['stockitem'])
        except (ValueError, StockItem.DoesNotExist):
            raise ValidationError(
                {'stockitem': _('No matching stock item found')})

        plugins = registry.with_mixin('barcode')

        plugin = None

        for current_plugin in plugins:
            current_plugin.init(barcode_data)

            if current_plugin.validate():
                plugin = current_plugin
                break

        match_found = False

        response = {}

        response['barcode_data'] = barcode_data

        # Matching plugin was found
        if plugin is not None:

            result_hash = plugin.hash()
            response['hash'] = result_hash
            response['plugin'] = plugin.name

            # Ensure that the barcode does not already match a database entry

            if plugin.getStockItem() is not None:
                match_found = True
                response['error'] = _('Barcode already matches Stock Item')

            if plugin.getStockLocation() is not None:
                match_found = True
                response['error'] = _('Barcode already matches Stock Location')

            if plugin.getPart() is not None:
                match_found = True
                response['error'] = _('Barcode already matches Part')

            if not match_found:
                item = plugin.getStockItemByHash()

                if item is not None:
                    response['error'] = _(
                        'Barcode hash already matches Stock Item')
                    match_found = True

        else:
            result_hash = hash_barcode(barcode_data)

            response['hash'] = result_hash
            response['plugin'] = None

            # Lookup stock item by hash
            try:
                item = StockItem.objects.get(uid=result_hash)
                response['error'] = _(
                    'Barcode hash already matches Stock Item')
                match_found = True
            except StockItem.DoesNotExist:
                pass

        if not match_found:
            response['success'] = _('Barcode associated with Stock Item')

            # Save the barcode hash
            item.uid = response['hash']
            item.save()

            serializer = StockItemSerializer(item,
                                             part_detail=True,
                                             location_detail=True,
                                             supplier_part_detail=True)
            response['stockitem'] = serializer.data

        return Response(response)
Example #6
0
def mixin_available(mixin, *args, **kwargs):
    """Returns True if there is at least one active plugin which supports the provided mixin."""
    return len(registry.with_mixin(mixin)) > 0