示例#1
0
    def renderStockItem(self, item):
        """
        Render a stock item to JSON response
        """

        serializer = StockItemSerializer(item, part_detail=True, location_detail=True, supplier_part_detail=True)
        return serializer.data
示例#2
0
    def render_stock_item(self, item):
        """
        Render a StockItem object to JSON.
        Use the existing serializer to do this
        """

        serializer = StockItemSerializer(item, part_detail=True, location_detail=True, supplier_detail=True)

        return serializer.data
示例#3
0
文件: api.py 项目: sfabris/InvenTree
    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 = load_barcode_plugins()

        barcode_data = data.get('barcode')

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

        for plugin_class in plugins:
            plugin_instance = plugin_class(barcode_data)

            if plugin_instance.validate():
                plugin = plugin_instance
                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:
            hash = hash_barcode(barcode_data)

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

            # Try to look for a matching StockItem
            try:
                item = StockItem.objects.get(uid=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)
示例#4
0
文件: api.py 项目: sfabris/InvenTree
    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 = load_barcode_plugins()

        plugin = None

        for plugin_class in plugins:
            plugin_instance = plugin_class(barcode_data)

            if plugin_instance.validate():
                plugin = plugin_instance
                break

        match_found = False

        response = {}

        response['barcode_data'] = barcode_data

        # Matching plugin was found
        if plugin is not None:

            hash = plugin.hash()
            response['hash'] = 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 StockItem object')

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

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

            if not match_found:
                item = plugin.getStockItemByHash()

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

        else:
            hash = hash_barcode(barcode_data)

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

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

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

            # 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)
示例#5
0
class SalesOrderAllocationSerializer(InvenTreeModelSerializer):
    """
    Serializer for the SalesOrderAllocation model.
    This includes some fields from the related model objects.
    """

    part = serializers.PrimaryKeyRelatedField(source='item.part',
                                              read_only=True)
    order = serializers.PrimaryKeyRelatedField(source='line.order',
                                               many=False,
                                               read_only=True)
    serial = serializers.CharField(source='get_serial', read_only=True)
    quantity = serializers.FloatField(read_only=True)
    location = serializers.PrimaryKeyRelatedField(source='item.location',
                                                  many=False,
                                                  read_only=True)

    # Extra detail fields
    order_detail = SalesOrderSerializer(source='line.order',
                                        many=False,
                                        read_only=True)
    part_detail = PartBriefSerializer(source='item.part',
                                      many=False,
                                      read_only=True)
    item_detail = StockItemSerializer(source='item',
                                      many=False,
                                      read_only=True)
    location_detail = LocationSerializer(source='item.location',
                                         many=False,
                                         read_only=True)

    def __init__(self, *args, **kwargs):

        order_detail = kwargs.pop('order_detail', False)
        part_detail = kwargs.pop('part_detail', False)
        item_detail = kwargs.pop('item_detail', False)
        location_detail = kwargs.pop('location_detail', False)

        super().__init__(*args, **kwargs)

        if not order_detail:
            self.fields.pop('order_detail')

        if not part_detail:
            self.fields.pop('part_detail')

        if not item_detail:
            self.fields.pop('item_detail')

        if not location_detail:
            self.fields.pop('location_detail')

    class Meta:
        model = SalesOrderAllocation

        fields = [
            'pk',
            'line',
            'serial',
            'quantity',
            'location',
            'location_detail',
            'item',
            'item_detail',
            'order',
            'order_detail',
            'part',
            'part_detail',
        ]
示例#6
0
    def post(self, request, *args, **kwargs):
        """Respond to a barcode POST request.

        Check if required info was provided and then run though the plugin steps or try to match up-
        """
        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)