Exemplo n.º 1
0
    def test_part_delete(self):
        """
        Test we can create and delete a Part instance via the API
        """

        n = len(Part.list(self.api))

        # Create a new part
        # We do not specify 'active' value so it will default to True
        p = Part.create(
            self.api, {
                'name': 'Delete Me',
                'description': 'Not long for this world!',
                'category': 1,
            })

        self.assertIsNotNone(p)
        self.assertIsNotNone(p.pk)

        self.assertEqual(len(Part.list(self.api)), n + 1)

        # Cannot delete - part is 'active'!
        with self.assertRaises(requests.exceptions.HTTPError) as ar:
            response = p.delete()

        self.assertIn("is active: cannot delete", str(ar.exception))

        p.save(data={'active': False})
        response = p.delete()
        self.assertEqual(response.status_code, 204)

        # And check that the part has indeed been deleted
        self.assertEqual(len(Part.list(self.api)), n)
Exemplo n.º 2
0
    def test_part_list(self):
        """
        Check that we can list Part objects,
        and apply certain filters
        """

        parts = Part.list(self.api)
        self.assertTrue(len(parts) >= 19)

        parts = Part.list(self.api, category=5)

        n = len(parts)

        for i in range(5):
            prt = Part.create(
                self.api, {
                    "category": 5,
                    "name": f"Special Part {n+i}",
                    "description": "A new part in this category!",
                })

            self.assertEqual(prt.category, 5)
            cat = prt.getCategory()
            self.assertEqual(cat.pk, 5)

            parts = cat.getParts()

            self.assertGreaterEqual(len(parts), i + 1)
Exemplo n.º 3
0
def find_part_location(part_number):
    """
    find part location in database
    :param part_number: part number to find
    :return: location of the part
    """
    try:
        api = InvenTreeAPI(SERVER_ADDRESS, token=My_TOKEN, verbose=True)
        parts = Part.list(api, search=part_number)
        # print(len(parts))

        if len(parts) == 1:
            my_part = parts[0].pk
        elif len(parts) > 1:
            for part in parts:
                my_part = part.pk  # get internal part number
                part_count = part._data['in_stock']  # total qty in stock

        part = Part(api, my_part)
        stock_items = part.getStockItems()
        if stock_items:
            if stock_items[0]._data['location']:
                stock_location = stock_items[0]._data['location']
                location = inventree.stock.StockLocation(api,
                                                         pk=stock_location)
                my_location = location._data['name']
            else:
                my_location = 'No Location'
        # print(my_location)
        return my_location
    except:
        print(api)
        print('Can not connect to the database')
Exemplo n.º 4
0
    def test_supplier_part_create(self):
        """
        Test that we can create SupplierPart objects via the API
        """

        supplier = company.Company(self.api, 1)

        # Find a purchaseable part
        parts = Part.list(self.api, purchasable=True)

        if len(parts) > 0:
            prt = parts[0]
        else:
            prt = Part.create(
                self.api, {
                    'name': 'My purchaseable part',
                    'description':
                    'A purchasenable part we can use to make a SupplierPart',
                    'category': 1,
                    'purchaseable': True
                })

        n = len(company.SupplierPart.list(self.api))

        supplier_part = company.SupplierPart.create(self.api, {
            'supplier': supplier.pk,
            'SKU': f'SKU_TEST_{n}',
            'part': prt.pk,
        })

        self.assertIsNotNone(supplier_part)
        self.assertTrue(supplier_part.part, prt.pk)

        self.assertEqual(len(company.SupplierPart.list(self.api)), n + 1)
Exemplo n.º 5
0
    def test_image_upload(self):
        """
        Test image upload functionality for Part model
        """

        # Grab the first part
        p = Part.list(self.api)[0]

        # Ensure the part does *not* have an image associated with it
        p.save(data={'image': None})

        # Create a dummy file (not an image)
        with open('dummy_image.jpg', 'w') as dummy_file:
            dummy_file.write("hello world")

        # Attempt to upload an image
        with self.assertRaises(requests.exceptions.HTTPError):
            response = p.uploadImage("dummy_image.jpg")

        # Now, let's actually upload a real image
        img = Image.new('RGB', (128, 128), color='red')
        img.save('dummy_image.png')

        response = p.uploadImage("dummy_image.png")

        self.assertIsNotNone(response)
        self.assertIsNotNone(p['image'])
        self.assertIn('dummy_image', p['image'])

        # Re-download the image file
        fout = 'test/output.png'

        if os.path.exists(fout):
            # Delete the file if it already exists
            os.remove(fout)

        response = p.downloadImage(fout)
        self.assertTrue(response)

        self.assertTrue(os.path.exists(fout))

        # Attempt to re-download
        with self.assertRaises(FileExistsError):
            p.downloadImage(fout)

        # Download, with overwrite enabled
        p.downloadImage(fout, overwrite=True)
Exemplo n.º 6
0
    def test_default_values(self):
        """
        Test that the DRF framework will correctly insert the default values
        """

        n = len(Part.list(self.api))

        # Create a part without specifying 'active' and 'virtual' fields
        p = Part.create(
            self.api, {
                'name': f"Part_{n}_default_test",
                'category': 1,
                'description': "Some part thingy",
            })

        self.assertEqual(p.active, True)
        self.assertEqual(p.virtual, False)

        # Set both to false
        p = Part.create(
            self.api, {
                'name': f"Part_{n}_default_test_2",
                'category': 1,
                'description': 'Setting fields to false',
                'active': False,
                'virtual': False,
            })

        self.assertFalse(p.active)
        self.assertFalse(p.virtual)

        # Set both to true
        p = Part.create(
            self.api, {
                'name': f"Part_{n}_default_test_3",
                'category': 1,
                'description': 'Setting fields to true',
                'active': True,
                'virtual': True,
            })

        self.assertTrue(p.active)
        self.assertTrue(p.virtual)
def create_inventree_part(dkpart: DigiPart):
    category = find_category()
    possible_parts = Part.list(API, name=dkpart.name, description=dkpart.description)
    if len(possible_parts) > 0:
        part_names = [p.name.lower() for p in possible_parts]
        if dkpart.name.lower() in part_names:
            print("Part already exists")
            return -1
    part = Part.create(API, {
        'name': dkpart.name,
        'description': dkpart.description,
        'category': category.pk,
        'active': True,
        'virtual': False,
        'component': True,
        'purchaseable': 1
        })
    upload_picture(dkpart, part)
    return part
Exemplo n.º 8
0
    def test_part_edit(self):
        """
        Test that we can edit a part
        """

        # Select a part
        p = Part.list(self.api)[-1]

        name = p.name

        # Ajdust the name
        if len(name) < 40:
            name += '_append'
        else:
            name = name[:-10]

        p.save(data={'name': name, 'description': 'A new description'}, )
        p.reload()

        self.assertEqual(p.name, name)
        self.assertEqual(p.description, 'A new description')
Exemplo n.º 9
0
    def test_set_price(self):
        """
        Tests that an internal price can be set for a part
        """

        test_price = 100.0
        test_quantity = 1

        # Grab the first part
        p = Part.list(self.api)[0]

        # Grab all internal prices for the part
        ip = InternalPrice.list(self.api, part=p.pk)

        # Delete any existsing prices
        for price in ip:
            self.assertEqual(type(price), InternalPrice)
            price.delete()

        # Ensure that no part has an internal price
        ip = InternalPrice.list(self.api, part=p.pk)
        self.assertEqual(len(ip), 0)

        # Set the internal price
        p.setInternalPrice(test_quantity, test_price)

        # Ensure that the part has an internal price
        ip = InternalPrice.list(self.api, part=p.pk)
        self.assertEqual(len(ip), 1)

        # Grab the internal price
        ip = ip[0]

        self.assertEqual(ip.quantity, test_quantity)
        self.assertEqual(ip.part, p.pk)
        ip_price_clean = float(ip.price)
        self.assertEqual(ip_price_clean, test_price)
Exemplo n.º 10
0
from digikey.v3.productinformation import KeywordSearchRequest

os.environ['DIGIKEY_CLIENT_ID'] = 'DjV4w1v0ebNTiL7Nqvslw0GkNYuYdrLG'
os.environ['DIGIKEY_CLIENT_SECRET'] = 'dK0dTRimeq3aiPH1'
os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'False'
os.environ['DIGIKEY_STORAGE_PATH'] = 'C:\\Users\\Oliver\\Desktop\\digikey\\'

INVENTREE_URL = "http://localhost:8000"
INVENTREE_USERNAME = "******"
INVENTREE_PASSWORD = "******"

inventree = InvenTreeAPI(INVENTREE_URL,
                         username=INVENTREE_USERNAME,
                         password=INVENTREE_PASSWORD)

resistors = Part.list(inventree, category=5)


def getParameter(result, name):

    for param in result.parameters:
        if param.parameter.lower() == name.lower():
            return param

    return None


def getValue(result, name):

    param = getParameter(result, name)
Exemplo n.º 11
0
    def test_parameters(self):
        """
        Test setting and getting Part parameter templates, as well as parameter values
        """

        # Count number of existing Parameter Templates
        existingTemplates = len(ParameterTemplate.list(self.api))

        # Create new parameter template - this will fail, no name given
        parametertemplate = ParameterTemplate.create(self.api,
                                                     data={'units': "kg A"})

        # result should be None
        self.assertIsNone(parametertemplate)

        # Now create a proper parameter template
        parametertemplate = ParameterTemplate.create(
            self.api,
            data={
                'name': f'Test parameter no {existingTemplates}',
                'units': "kg A"
            })

        # result should not be None
        self.assertIsNotNone(parametertemplate)

        # Count should be one higher now
        self.assertEqual(len(ParameterTemplate.list(self.api)),
                         existingTemplates + 1)

        # Grab the first part
        p = Part.list(self.api)[0]

        # Count number of parameters
        existingParameters = len(p.getParameters())

        # Define parameter value for this part - without all required values
        param = Parameter.create(self.api,
                                 data={
                                     'part': p.pk,
                                     'template': parametertemplate.pk
                                 })

        # result should be None
        self.assertIsNone(param)

        # Define parameter value for this part - without all required values
        param = Parameter.create(self.api, data={'part': p.pk, 'data': 10})

        # result should be None
        self.assertIsNone(param)

        # Define w. required values - integer
        param = Parameter.create(self.api,
                                 data={
                                     'part': p.pk,
                                     'template': parametertemplate.pk,
                                     'data': 10
                                 })

        # result should not be None
        self.assertIsNotNone(param)

        # Same parameter for same part - should fail
        # Define w. required values - string
        param2 = Parameter.create(self.api,
                                  data={
                                      'part': p.pk,
                                      'template': parametertemplate.pk,
                                      'data': 'String value'
                                  })

        # result should be None
        self.assertIsNone(param2)

        # Number of parameters should be one higher than before
        self.assertEqual(len(p.getParameters()), existingParameters + 1)

        # Delete the parameter
        param.delete()

        # Check count
        self.assertEqual(len(p.getParameters()), existingParameters)
Exemplo n.º 12
0
import partkeepr
from inventree.api import InvenTreeAPI
from inventree.part import PartCategory,Part
from inventree.stock import StockItem, StockLocation

SERVER_ADDRESS = 'http://127.0.0.1:8000'
MY_USERNAME = '******'
MY_PASSWORD = '******'

api = InvenTreeAPI(SERVER_ADDRESS, username=MY_USERNAME, password=MY_PASSWORD)

#delete all the exisiting parts

itparts = Part.list(api)
for p in itparts:
    print("delete p ",p.name)
    p._data['active'] = False
    p.save()
    p.delete()

#delete all of the exisitng categories

cat = PartCategory.list(api)


for c in cat:
    print("delete c ",c.name)
    c.delete()


Exemplo n.º 13
0
from inventree.api import InvenTreeAPI

from inventree.part import Part, PartCategory
from inventree.stock import StockItem, StockLocation
from inventree.company import SupplierPart

import random
import sys

INVENTREE_URL = "http://localhost:8000"
INVENTREE_USERNAME = "******"
INVENTREE_PASSWORD = "******"

api = InvenTreeAPI(INVENTREE_URL, username=INVENTREE_USERNAME, password=INVENTREE_PASSWORD)

resistors = Part.list(api, category=5)

storage = StockLocation(api, pk=8)

count = 0

for resistor in resistors:

    if random.random() > 0.65:
        continue

    q = random.random()

    quantity = 1000

    if q < 0.1:
from inventree.part import Part, PartCategory
from inventree.stock import StockItem, StockLocation
from inventree.company import SupplierPart

import random
import sys

INVENTREE_URL = "http://localhost:8000"
INVENTREE_USERNAME = "******"
INVENTREE_PASSWORD = "******"

api = InvenTreeAPI(INVENTREE_URL,
                   username=INVENTREE_USERNAME,
                   password=INVENTREE_PASSWORD)

capacitors = Part.list(api, category=6)

storage = StockLocation(api, pk=8)

count = 0

for cap in capacitors:

    if random.random() > 0.65:
        continue

    # Get the first matching supplierpart
    sp_list = SupplierPart.list(api, part=cap.pk)

    for sp in sp_list:
        if random.random() > 0.6: