code
stringlengths
66
870k
docstring
stringlengths
19
26.7k
func_name
stringlengths
1
138
language
stringclasses
1 value
repo
stringlengths
7
68
path
stringlengths
5
324
url
stringlengths
46
389
license
stringclasses
7 values
def test_self_move(self): """Test moving stock to its current location.""" it = StockItem.objects.get(pk=1) n = it.tracking_info.count() self.assertTrue(it.move(it.location, 'Moved to same place', None)) # Ensure tracking info was not added self.assertEqual(it.tracking_...
Test moving stock to its current location.
test_self_move
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_return_from_customer(self): """Test removing previous allocated stock from customer.""" it = StockItem.objects.get(pk=2) # First establish total stock for this part allstock_before = StockItem.objects.filter(part=it.part).aggregate( Sum('quantity') )['quanti...
Test removing previous allocated stock from customer.
test_return_from_customer
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_big_serials(self): """Unit tests for "large" serial numbers which exceed integer encoding.""" p = Part.objects.create( name='trackable part', description='A trackable part with really big serial numbers', trackable=True, ) item = StockItem.ob...
Unit tests for "large" serial numbers which exceed integer encoding.
test_big_serials
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_serialize_stock_invalid(self): """Test manual serialization of parts. Each of these tests should fail """ # Test serialization of non-serializable part item = StockItem.objects.get(pk=1234) with self.assertRaises(ValidationError): item.serializeStoc...
Test manual serialization of parts. Each of these tests should fail
test_serialize_stock_invalid
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_location_tree(self): """Unit tests for stock location tree structure (MPTT). Ensure that the MPTT structure is rebuilt correctly, and the current ancestor tree is observed. Ref: https://github.com/inventree/InvenTree/issues/2636 Ref: https://github.com/inventree/InvenT...
Unit tests for stock location tree structure (MPTT). Ensure that the MPTT structure is rebuilt correctly, and the current ancestor tree is observed. Ref: https://github.com/inventree/InvenTree/issues/2636 Ref: https://github.com/inventree/InvenTree/issues/2733
test_location_tree
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_merge(self): """Test merging of multiple stock items.""" from djmoney.money import Money part = Part.objects.first() part.stock_items.all().delete() # Test simple merge without any pricing information s1 = StockItem.objects.create(part=part, quantity=10) ...
Test merging of multiple stock items.
test_merge
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_notify_low_stock(self): """Test that the 'notify_low_stock' task is triggered correctly.""" FUNC_NAME = 'part.tasks.notify_low_stock_if_required' from django_q.models import OrmQ # Start from a blank slate OrmQ.objects.all().delete() def check_func() -> bool: ...
Test that the 'notify_low_stock' task is triggered correctly.
test_notify_low_stock
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def check_func() -> bool: """Check that the 'notify_low_stock_if_required' task has been triggered.""" found = False for task in OrmQ.objects.all(): if task.func() == FUNC_NAME: found = True break # Clear the task q...
Check that the 'notify_low_stock_if_required' task has been triggered.
check_func
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_stock_item_barcode_basics(self): """Simple tests for the StockItem barcode integration.""" item = StockItem.objects.get(pk=1) self.assertEqual(StockItem.barcode_model_type(), 'stockitem') # Render simple barcode data for the StockItem barcode = item.barcode sel...
Simple tests for the StockItem barcode integration.
test_stock_item_barcode_basics
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_location_barcode_basics(self): """Simple tests for the StockLocation barcode integration.""" self.assertEqual(StockLocation.barcode_model_type(), 'stocklocation') loc = StockLocation.objects.get(pk=1) barcode = loc.format_barcode() self.assertEqual('{"stocklocation": 1...
Simple tests for the StockLocation barcode integration.
test_location_barcode_basics
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_serial_numbers(self): """Test serial number functionality for variant / template parts.""" InvenTreeSetting.set_setting('SERIAL_NUMBER_GLOBALLY_UNIQUE', False, self.user) chair = Part.objects.get(pk=10000) # Operations on the top-level object [ self.assertF...
Test serial number functionality for variant / template parts.
test_serial_numbers
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_stock_split(self): """Test that stock splitting works correctly.""" StockItem.objects.rebuild() part = Part.objects.create(name='My part', description='My part description') location = StockLocation.objects.create(name='Test Location') # Create an initial stock item ...
Test that stock splitting works correctly.
test_stock_split
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_installed_tests(self): """Test test results for stock in stock. Or, test "test results" for "stock items" installed "inside" a "stock item" """ # Get a "master" stock item item = StockItem.objects.get(pk=105) tests = item.testResultMap(include_installed=False) ...
Test test results for stock in stock. Or, test "test results" for "stock items" installed "inside" a "stock item"
test_installed_tests
python
inventree/InvenTree
src/backend/InvenTree/stock/tests.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/tests.py
MIT
def test_stock_location_delete(self): """Test stock location deletion with different parameters.""" class Target(IntEnum): move_sub_locations_to_parent_move_stockitems_to_parent = (0,) move_sub_locations_to_parent_delete_stockitems = (1,) delete_sub_locations_move_st...
Test stock location deletion with different parameters.
test_stock_location_delete
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_stock_location_structural(self): """Test the effectiveness of structural stock locations. Make sure: - Stock items cannot be created in structural locations - Stock items cannot be located to structural locations - Check that stock location change to structural fails if...
Test the effectiveness of structural stock locations. Make sure: - Stock items cannot be created in structural locations - Stock items cannot be located to structural locations - Check that stock location change to structural fails if items located into it
test_stock_location_structural
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_stock_location_icon(self): """Test stock location icon inheritance from StockLocationType.""" parent_location = StockLocation.objects.create(name='Parent location') location_type = StockLocationType.objects.create( name='Box', description='This is a very cool type of box', ...
Test stock location icon inheritance from StockLocationType.
test_stock_location_icon
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_list(self): """Test that the list endpoint works as expected.""" location_types = [ StockLocationType.objects.create( name='Type 1', description='Type 1 desc', icon='ti:package:outline' ), StockLocationType.objects.create( name...
Test that the list endpoint works as expected.
test_list
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_delete(self): """Test that we can delete a location type via API.""" location_type = StockLocationType.objects.create( name='Type 1', description='Type 1 desc', icon='ti:package:outline' ) self.delete( reverse('api-location-type-detail', kwargs={'pk': loc...
Test that we can delete a location type via API.
test_delete
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_create(self): """Test that we can create a location type via API.""" self.post( self.list_url, {'name': 'Test Type 1', 'description': 'Test desc 1', 'icon': 'fas fa-box'}, expected_code=400, ) self.post( self.list_url, ...
Test that we can create a location type via API.
test_create
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_update(self): """Test that we can update a location type via API.""" location_type = StockLocationType.objects.create( name='Type 1', description='Type 1 desc', icon='ti:package:outline' ) self.patch( reverse('api-location-type-detail', kwargs={'pk': loca...
Test that we can update a location type via API.
test_update
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def get_stock(self, **kwargs): """Filter stock and return JSON object.""" response = self.client.get(self.list_url, format='json', data=kwargs) self.assertEqual(response.status_code, status.HTTP_200_OK) # Return JSON data return response.data
Filter stock and return JSON object.
get_stock
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_top_level_filtering(self): """Test filtering against "top level" stock location.""" # No filters, should return *all* items response = self.get(self.list_url, {}, expected_code=200) self.assertEqual(len(response.data), StockItem.objects.count()) # Filter with "cascade=F...
Test filtering against "top level" stock location.
test_top_level_filtering
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_filter_by_in_stock(self): """Filter StockItem by 'in stock' status.""" response = self.get_stock(in_stock=1) self.assertEqual(len(response), 26) response = self.get_stock(in_stock=0) self.assertEqual(len(response), 3)
Filter StockItem by 'in stock' status.
test_filter_by_in_stock
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_filter_by_has_batch(self): """Test the 'has_batch' filter, which tests if the stock item has been assigned a batch code.""" with_batch = self.get_stock(has_batch=1) without_batch = self.get_stock(has_batch=0) n_stock_items = StockItem.objects.all().count() # Total sum ...
Test the 'has_batch' filter, which tests if the stock item has been assigned a batch code.
test_filter_by_has_batch
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_filter_by_tracked(self): """Test the 'tracked' filter. This checks if the stock item has either a batch code *or* a serial number """ tracked = self.get_stock(tracked=True) untracked = self.get_stock(tracked=False) n_stock_items = StockItem.objects.all().count(...
Test the 'tracked' filter. This checks if the stock item has either a batch code *or* a serial number
test_filter_by_tracked
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_paginate(self): """Test that we can paginate results correctly.""" for n in [1, 5, 10]: response = self.get_stock(limit=n) self.assertIn('count', response) self.assertIn('results', response) self.assertEqual(len(response['results']), n)
Test that we can paginate results correctly.
test_paginate
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_export(self): """Test exporting of Stock data via the API.""" required_headers = [ 'Part', 'Customer', 'Stock Location', 'Parent Item', 'Quantity', 'Status', 'Part.Name', 'Part.Description', ...
Test exporting of Stock data via the API.
test_export
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_filter_by_allocated(self): """Test that we can filter by "allocated" status. Rules: - Only return stock items which are 'allocated' - Either to a build order or sales order - Test that the results are "distinct" (no duplicated results) - Ref: https://github.com/...
Test that we can filter by "allocated" status. Rules: - Only return stock items which are 'allocated' - Either to a build order or sales order - Test that the results are "distinct" (no duplicated results) - Ref: https://github.com/inventree/InvenTree/pull/5916
test_filter_by_allocated
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_query_count(self): """Test that the number of queries required to fetch stock items is reasonable.""" # Create a bunch of StockItem objects prt = Part.objects.first() StockItem.objects.bulk_create([ StockItem(part=prt, quantity=1, level=0, tree_id=0, lft=0, rght=0) ...
Test that the number of queries required to fetch stock items is reasonable.
test_query_count
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_batch_generate_api(self): """Test helper API for batch management.""" set_global_setting( 'STOCK_BATCH_CODE_TEMPLATE', '{% if item %}{{ item.pk }}{% endif %}' ) url = reverse('api-generate-batch-code') response = self.post(url) self.assertEqual(respo...
Test helper API for batch management.
test_batch_generate_api
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_serial_generate_api(self): """Test helper API for serial management.""" url = reverse('api-generate-serial-number') # Generate serial number response = self.post(url) self.assertIn('serial_number', response.data) # With full data response = self.post(ur...
Test helper API for serial management.
test_serial_generate_api
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_child_items(self): """Test that the 'child_items' annotation works as expected.""" # Create a trackable part my_part = Part.objects.create( name='Test Part', description='Test Part Description', trackable=True ) # Create an initial stock item parent_...
Test that the 'child_items' annotation works as expected.
test_child_items
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_options(self): """Test the StockItem OPTIONS endpoint to contain custom StockStatuses.""" response = self.options(self.list_url) self.assertEqual(response.status_code, 200) # Check that the response contains the custom StockStatuses actions = response.data['actions']['...
Test the StockItem OPTIONS endpoint to contain custom StockStatuses.
test_options
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_create_default_location(self): """Test the default location functionality, if a 'location' is not specified in the creation request.""" # The part 'R_4K7_0603' (pk=4) has a default location specified response = self.post( self.list_url, data={'part': 4, 'quantity': 10}, exp...
Test the default location functionality, if a 'location' is not specified in the creation request.
test_create_default_location
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_stock_item_create(self): """Test creation of a StockItem via the API.""" # POST with an empty part reference response = self.client.post(self.list_url, data={'quantity': 10, 'location': 1}) self.assertContains( response, 'Valid part must be supplied', ...
Test creation of a StockItem via the API.
test_stock_item_create
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_stock_item_create_with_supplier_part(self): """Test creation of a StockItem via the API, including SupplierPart data.""" # POST with non-existent supplier part response = self.post( self.list_url, data={'part': 1, 'location': 1, 'quantity': 4, 'supplier_part': 10...
Test creation of a StockItem via the API, including SupplierPart data.
test_stock_item_create_with_supplier_part
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_creation_with_serials(self): """Test that serialized stock items can be created via the API.""" trackable_part = part.models.Part.objects.create( name='My part', description='A trackable part', trackable=True, default_location=StockLocation.object...
Test that serialized stock items can be created via the API.
test_creation_with_serials
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_default_expiry(self): """Test that the "default_expiry" functionality works via the API. - If an expiry_date is specified, use that - Otherwise, check if the referenced part has a default_expiry defined - If so, use that! - Otherwise, no expiry Notes: ...
Test that the "default_expiry" functionality works via the API. - If an expiry_date is specified, use that - Otherwise, check if the referenced part has a default_expiry defined - If so, use that! - Otherwise, no expiry Notes: - Part <25> has a default_expir...
test_default_expiry
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_purchase_price(self): """Test that we can correctly read and adjust purchase price information via the API.""" url = reverse('api-stock-detail', kwargs={'pk': 1}) data = self.get(url, expected_code=200).data # Check fixture values self.assertAlmostEqual(data['purchase_...
Test that we can correctly read and adjust purchase price information via the API.
test_purchase_price
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_install(self): """Test that stock item can be installed into another item, via the API.""" # Select the "parent" stock item parent_part = part.models.Part.objects.get(pk=100) item = StockItem.objects.create( part=parent_part, serial='12345688-1230', quantity=1 ...
Test that stock item can be installed into another item, via the API.
test_install
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_return_from_customer(self): """Test that we can return a StockItem from a customer, via the API.""" # Assign item to customer item = StockItem.objects.get(pk=521) customer = company.models.Company.objects.get(pk=4) item.customer = customer item.save() n...
Test that we can return a StockItem from a customer, via the API.
test_return_from_customer
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_convert_to_variant(self): """Test that we can convert a StockItem to a variant part via the API.""" category = part.models.PartCategory.objects.get(pk=3) # First, construct a set of template / variant parts master_part = part.models.Part.objects.create( name='Master...
Test that we can convert a StockItem to a variant part via the API.
test_convert_to_variant
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_set_status(self): """Test API endpoint for setting StockItem status.""" url = reverse('api-stock-change-status') prt = Part.objects.first() # Create a bunch of items items = [StockItem.objects.create(part=prt, quantity=10) for _ in range(10)] for item in items...
Test API endpoint for setting StockItem status.
test_set_status
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_action(self): """Test each stocktake action endpoint, for validation.""" target = { 'api-stock-count': '10.00000', 'api-stock-add': '10.00000', 'api-stock-remove': '10.00000', } for endpoint in ['api-stock-count', 'api-stock-add', 'api-stock-r...
Test each stocktake action endpoint, for validation.
test_action
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_post(self): """Test creation of a new test result.""" url = self.get_url() response = self.client.get(url) n = len(response.data) # Test upload using test name (legacy method) # Note that a new test template will be created data = { 'stock_i...
Test creation of a new test result.
test_post
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_post_bitmap(self): """2021-08-25. For some (unknown) reason, prior to fix https://github.com/inventree/InvenTree/pull/2018 uploading a bitmap image would result in a failure. This test has been added to ensure that there is no regression. As a bonus this also tests th...
2021-08-25. For some (unknown) reason, prior to fix https://github.com/inventree/InvenTree/pull/2018 uploading a bitmap image would result in a failure. This test has been added to ensure that there is no regression. As a bonus this also tests the file-upload component
test_post_bitmap
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_bulk_delete(self): """Test that the BulkDelete endpoint works for this model.""" n = StockItemTestResult.objects.count() tests = [] url = reverse('api-stock-test-result-list') stock_item = StockItem.objects.get(pk=1) # Ensure the part is marked as "testable" ...
Test that the BulkDelete endpoint works for this model.
test_bulk_delete
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_value_choices(self): """Test that the 'value' field is correctly validated.""" url = reverse('api-stock-test-result-list') test_template = PartTestTemplate.objects.first() test_template.choices = 'AA, BB, CC' test_template.save() stock_item = StockItem.objects...
Test that the 'value' field is correctly validated.
test_value_choices
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def setUpTestData(cls): """Initialize some test data for the StockTracking tests.""" super().setUpTestData() import build.models import company.models import order.models import stock.models import stock.status_codes entries = [] N_BO = build.mo...
Initialize some test data for the StockTracking tests.
setUpTestData
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_missing_data(self): """Test responses which are missing required data.""" # Post completely empty data = self.post(self.URL, {}, expected_code=400).data self.assertIn('This field is required', str(data['items'])) self.assertIn('This field is required', str(data['locati...
Test responses which are missing required data.
test_missing_data
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_invalid_data(self): """Test responses which have invalid data.""" # Serialized stock items should be rejected data = self.post( self.URL, {'items': [{'item': 501}, {'item': 502}], 'location': 1}, expected_code=400, ).data self.assertI...
Test responses which have invalid data.
test_invalid_data
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def test_valid_merge(self): """Test valid merging of stock items.""" # Check initial conditions n = StockItem.objects.filter(part=self.part).count() self.assertEqual(self.item_1.quantity, 100) payload = { 'items': [ {'item': self.item_1.pk}, ...
Test valid merging of stock items.
test_valid_merge
python
inventree/InvenTree
src/backend/InvenTree/stock/test_api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_api.py
MIT
def prepare(self): """Create initial data for this migration.""" Part = self.old_state.apps.get_model('part', 'part') StockItem = self.old_state.apps.get_model('stock', 'stockitem') # Create a base part my_part = Part.objects.create( name='PART-123', desc...
Create initial data for this migration.
prepare
python
inventree/InvenTree
src/backend/InvenTree/stock/test_migrations.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_migrations.py
MIT
def test_migrations(self): """Test that the migrations have been applied correctly.""" StockItem = self.new_state.apps.get_model('stock', 'stockitem') # Check that the serial number integer conversion has been applied correctly for sn in range(10, 20): item = StockItem.objec...
Test that the migrations have been applied correctly.
test_migrations
python
inventree/InvenTree
src/backend/InvenTree/stock/test_migrations.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_migrations.py
MIT
def test_migration(self): """Test that all stock items were actually removed.""" StockItem = self.new_state.apps.get_model('stock', 'stockitem') # All the "scheduled for deletion" items have been removed self.assertEqual(StockItem.objects.count(), 3)
Test that all stock items were actually removed.
test_migration
python
inventree/InvenTree
src/backend/InvenTree/stock/test_migrations.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_migrations.py
MIT
def test_migration(self): """Test that the migrations were applied as expected.""" Part = self.new_state.apps.get_model('part', 'part') PartTestTemplate = self.new_state.apps.get_model('part', 'parttesttemplate') StockItem = self.new_state.apps.get_model('stock', 'stockitem') Sto...
Test that the migrations were applied as expected.
test_migration
python
inventree/InvenTree
src/backend/InvenTree/stock/test_migrations.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_migrations.py
MIT
def enable_ownership(self): """Helper function to turn on ownership control.""" # Enable stock location ownership InvenTreeSetting.set_setting('STOCK_OWNERSHIP_CONTROL', True, self.user) self.assertEqual(True, InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL'))
Helper function to turn on ownership control.
enable_ownership
python
inventree/InvenTree
src/backend/InvenTree/stock/test_views.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_views.py
MIT
def assert_ownership(self, assertion: bool = True, user=None): """Helper function to check ownership control.""" if user is None: user = self.user item = StockItem.objects.get(pk=self.test_item_id) self.assertEqual(assertion, item.check_ownership(user)) location = S...
Helper function to check ownership control.
assert_ownership
python
inventree/InvenTree
src/backend/InvenTree/stock/test_views.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_views.py
MIT
def assert_api_change(self): """Helper function to get response to API change.""" return self.client.patch( reverse('api-stock-detail', args=(self.test_item_id,)), {'status': StockStatus.DAMAGED.value}, content_type='application/json', )
Helper function to get response to API change.
assert_api_change
python
inventree/InvenTree
src/backend/InvenTree/stock/test_views.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_views.py
MIT
def test_ownership_as_superuser(self): """Test that superuser are always allowed to access items.""" self.enable_ownership() # Check with superuser self.user.is_superuser = True self.user.save() self.assert_ownership(True)
Test that superuser are always allowed to access items.
test_ownership_as_superuser
python
inventree/InvenTree
src/backend/InvenTree/stock/test_views.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_views.py
MIT
def test_ownership_functions(self): """Test that ownership is working correctly for StockItem/StockLocation.""" self.enable_ownership() item = StockItem.objects.get(pk=self.test_item_id) location = StockLocation.objects.get(pk=self.test_location_id) # Check that user is not allo...
Test that ownership is working correctly for StockItem/StockLocation.
test_ownership_functions
python
inventree/InvenTree
src/backend/InvenTree/stock/test_views.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/test_views.py
MIT
def update_history(apps, schema_editor): """ Update each existing StockItemTracking object, convert the recorded "quantity" to a delta """ StockItem = apps.get_model('stock', 'stockitem') StockItemTracking = apps.get_model('stock', 'stockitemtracking') StockLocation = apps.get_model('stock'...
Update each existing StockItemTracking object, convert the recorded "quantity" to a delta
update_history
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0061_auto_20210511_0911.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0061_auto_20210511_0911.py
MIT
def extract_purchase_price(apps, schema_editor): """ Find instances of StockItem which do *not* have a purchase price set, but which point to a PurchaseOrder where there *is* a purchase price set. Then, assign *that* purchase price to original StockItem. This is to address an issue where older ver...
Find instances of StockItem which do *not* have a purchase price set, but which point to a PurchaseOrder where there *is* a purchase price set. Then, assign *that* purchase price to original StockItem. This is to address an issue where older versions of InvenTree did not correctly copy purchase p...
extract_purchase_price
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0064_auto_20210621_1724.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0064_auto_20210621_1724.py
MIT
def update_serials(apps, schema_editor): """ Rebuild the integer serial number field for existing StockItem objects """ StockItem = apps.get_model('stock', 'stockitem') for item in StockItem.objects.all(): # pragma: no cover if item.serial is None: # Skip items without existi...
Rebuild the integer serial number field for existing StockItem objects
update_serials
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0069_auto_20211109_2347.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0069_auto_20211109_2347.py
MIT
def delete_scheduled(apps, schema_editor): """ Delete all stock items which are marked as 'scheduled_for_deletion'. The issue that this field was addressing has now been fixed, and so we can all move on with our lives... """ StockItem = apps.get_model('stock', 'stockitem') items = StockIt...
Delete all stock items which are marked as 'scheduled_for_deletion'. The issue that this field was addressing has now been fixed, and so we can all move on with our lives...
delete_scheduled
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0071_auto_20211205_1733.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0071_auto_20211205_1733.py
MIT
def update_pathstring(apps, schema_editor): """Construct pathstring for all existing StockLocation objects""" StockLocation = apps.get_model('stock', 'stocklocation') n = StockLocation.objects.count() if n > 0: for loc in StockLocation.objects.all(): # Construct complete path fo...
Construct pathstring for all existing StockLocation objects
update_pathstring
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0081_auto_20220801_0044.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0081_auto_20220801_0044.py
MIT
def uid_to_barcode(apps, schama_editor): """Migrate old 'uid' field to new 'barcode_hash' field""" StockItem = apps.get_model('stock', 'stockitem') # Find all StockItem objects with non-empty UID field items = StockItem.objects.exclude(uid=None).exclude(uid='') for item in items: item.bar...
Migrate old 'uid' field to new 'barcode_hash' field
uid_to_barcode
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0085_auto_20220903_0225.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0085_auto_20220903_0225.py
MIT
def barcode_to_uid(apps, schema_editor): """Migrate new 'barcode_hash' field to old 'uid' field""" StockItem = apps.get_model('stock', 'stockitem') # Find all StockItem objects with non-empty UID field items = StockItem.objects.exclude(barcode_hash=None).exclude(barcode_hash='') for item in items...
Migrate new 'barcode_hash' field to old 'uid' field
barcode_to_uid
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0085_auto_20220903_0225.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0085_auto_20220903_0225.py
MIT
def fix_purchase_price(apps, schema_editor): """Data migration for fixing historical issue with StockItem.purchase_price field. Ref: https://github.com/inventree/InvenTree/pull/4373 Due to an existing bug, if a PurchaseOrderLineItem was received, which had: a) A SupplierPart with a non-unity pack...
Data migration for fixing historical issue with StockItem.purchase_price field. Ref: https://github.com/inventree/InvenTree/pull/4373 Due to an existing bug, if a PurchaseOrderLineItem was received, which had: a) A SupplierPart with a non-unity pack size b) A defined purchase_price then the ...
fix_purchase_price
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0094_auto_20230220_0025.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0094_auto_20230220_0025.py
MIT
def update_stock_history(apps, schema_editor): """Data migration to fix a 'shortcoming' in the implementation of StockTracking history Prior to https://github.com/inventree/InvenTree/pull/4488, shipping items via a SalesOrder did not record the SalesOrder in the tracking history. This PR looks to add i...
Data migration to fix a 'shortcoming' in the implementation of StockTracking history Prior to https://github.com/inventree/InvenTree/pull/4488, shipping items via a SalesOrder did not record the SalesOrder in the tracking history. This PR looks to add in SalesOrder history where it does not already exist: ...
update_stock_history
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0096_auto_20230330_1121.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0096_auto_20230330_1121.py
MIT
def set_template(apps, schema_editor): """Matching existing StockItemTestResult objects to their associated template. - Use the 'key' value from the associated test object. - Look at the referenced part first - If no matches, look at parent part template(s) - If still no matches, create a new PartT...
Matching existing StockItemTestResult objects to their associated template. - Use the 'key' value from the associated test object. - Look at the referenced part first - If no matches, look at parent part template(s) - If still no matches, create a new PartTestTemplate object
set_template
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0106_auto_20240207_0353.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0106_auto_20240207_0353.py
MIT
def remove_template(apps, schema_editor): """Remove template links from existing StockItemTestResult objects.""" StockItemTestResult = apps.get_model('stock', 'stockitemtestresult') results = StockItemTestResult.objects.all() results.update(template=None) if results.count() > 0: print(f"\n...
Remove template links from existing StockItemTestResult objects.
remove_template
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0106_auto_20240207_0353.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0106_auto_20240207_0353.py
MIT
def update_templates(apps, schema_editor): """Run data migration to fix potentially mis-applied data migration. Ref: https://github.com/inventree/InvenTree/pull/6514 The previous data migration (stock.0106_auto_20240207_0353) had a bug, where it would look for any matching PartTestTemplate objects...
Run data migration to fix potentially mis-applied data migration. Ref: https://github.com/inventree/InvenTree/pull/6514 The previous data migration (stock.0106_auto_20240207_0353) had a bug, where it would look for any matching PartTestTemplate objects for a given StockItemTestResult, as long as t...
update_templates
python
inventree/InvenTree
src/backend/InvenTree/stock/migrations/0108_auto_20240219_0252.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/stock/migrations/0108_auto_20240219_0252.py
MIT
def get_fields(self, request, obj=None): """Return list of fields to display.""" fields = ['token'] if obj else ['key'] fields += [ 'user', 'name', 'created', 'last_seen', 'revoked', 'expiry', 'metadata', ...
Return list of fields to display.
get_fields
python
inventree/InvenTree
src/backend/InvenTree/users/admin.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/admin.py
MIT
def get_readonly_fields(self, request, obj=None): """Some fields are read-only after creation.""" ro = ['created', 'last_seen'] if obj: ro += ['token', 'user', 'expiry', 'name'] return ro
Some fields are read-only after creation.
get_readonly_fields
python
inventree/InvenTree
src/backend/InvenTree/users/admin.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/admin.py
MIT
def __init__(self, *args, **kwargs): # pragma: no cover """Populate the 'users' field with the users in the current group.""" super().__init__(*args, **kwargs) if self.instance.pk: # Populate the users field with the current Group users. self.fields['users'].initial = s...
Populate the 'users' field with the users in the current group.
__init__
python
inventree/InvenTree
src/backend/InvenTree/users/admin.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/admin.py
MIT
def save(self, *args, **kwargs): # pragma: no cover """Custom save method for Group admin form.""" # Default save instance = super().save() # Save many-to-many data self.save_m2m() return instance
Custom save method for Group admin form.
save
python
inventree/InvenTree
src/backend/InvenTree/users/admin.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/admin.py
MIT
def filter_queryset(self, queryset): """Implement text search for the "owner" model. Note that an "owner" can be either a group, or a user, so we cannot do a direct text search. A "hack" here is to post-process the queryset and simply remove any values which do not match. ...
Implement text search for the "owner" model. Note that an "owner" can be either a group, or a user, so we cannot do a direct text search. A "hack" here is to post-process the queryset and simply remove any values which do not match. It is not necessarily "efficient" to do it t...
filter_queryset
python
inventree/InvenTree
src/backend/InvenTree/users/api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/api.py
MIT
def get(self, request, *args, **kwargs): """Return an API token if the user is authenticated. - If the user already has a matching token, delete it and create a new one - Existing tokens are *never* exposed again via the API - Once the token is provided, it can be used for auth until it...
Return an API token if the user is authenticated. - If the user already has a matching token, delete it and create a new one - Existing tokens are *never* exposed again via the API - Once the token is provided, it can be used for auth until it expires
get
python
inventree/InvenTree
src/backend/InvenTree/users/api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/api.py
MIT
def get_queryset(self): """Only return data for current user.""" if self.request.user.is_superuser and self.request.query_params.get( 'all_users', False ): return ApiToken.objects.all() return ApiToken.objects.filter(user=self.request.user)
Only return data for current user.
get_queryset
python
inventree/InvenTree
src/backend/InvenTree/users/api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/api.py
MIT
def create(self, request, *args, **kwargs): """Create token and show key to user.""" resp = super().create(request, *args, **kwargs) resp.data['token'] = self.serializer_class.Meta.model.objects.get( id=resp.data['id'] ).key return resp
Create token and show key to user.
create
python
inventree/InvenTree
src/backend/InvenTree/users/api.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/api.py
MIT
def ready(self): """Called when the 'users' app is loaded at runtime.""" # skip loading if plugin registry is not loaded or we run in a background thread if ( not InvenTree.ready.isPluginRegistryLoaded() or not InvenTree.ready.isInMainThread() ): retur...
Called when the 'users' app is loaded at runtime.
ready
python
inventree/InvenTree
src/backend/InvenTree/users/apps.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/apps.py
MIT
def update_owners(self): """Create an 'owner' object for each user and group instance.""" from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from users.models import Owner # Create group owners for group in Group.objects.all(): ...
Create an 'owner' object for each user and group instance.
update_owners
python
inventree/InvenTree
src/backend/InvenTree/users/apps.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/apps.py
MIT
def authenticate_credentials(self, key): """Adds additional checks to the default token authentication method.""" # If this runs without error, then the token is valid (so far) (user, token) = super().authenticate_credentials(key) if token.revoked: raise exceptions.Authentic...
Adds additional checks to the default token authentication method.
authenticate_credentials
python
inventree/InvenTree
src/backend/InvenTree/users/authentication.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/authentication.py
MIT
def user_model_str(self): """Function to override the default Django User __str__.""" if get_global_setting('DISPLAY_FULL_NAMES', cache=True): if self.first_name or self.last_name: return f'{self.first_name} {self.last_name}' return self.username
Function to override the default Django User __str__.
user_model_str
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def create_email_address(user, **kwargs): """If a django user is from LDAP and has an email attached to it, create an allauth email address for them automatically. https://django-auth-ldap.readthedocs.io/en/latest/users.html#populating-users https://django-auth-ldap.readthedocs.io/en/latest/ref...
If a django user is from LDAP and has an email attached to it, create an allauth email address for them automatically. https://django-auth-ldap.readthedocs.io/en/latest/users.html#populating-users https://django-auth-ldap.readthedocs.io/en/latest/reference.html#django_auth_ldap.backend.populate_user ...
create_email_address
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def default_create_token(token_model, user, serializer): """Generate a default value for the token.""" token = token_model.objects.filter(user=user, name='', revoked=False) if token.exists(): return token.first() else: return token_model.objects.create(user=user, name='')
Generate a default value for the token.
default_create_token
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def generate_key(cls, prefix='inv-'): """Generate a new token key - with custom prefix.""" # Suffix is the date of creation suffix = '-' + str(datetime.datetime.now().date().isoformat().replace('-', '')) return prefix + str(AuthToken.generate_key()) + suffix
Generate a new token key - with custom prefix.
generate_key
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def token(self) -> str: """Provide a redacted version of the token. The *raw* key value should never be displayed anywhere! """ # If the token has not yet been saved, return the raw key if self.pk is None: return self.key # pragma: no cover M = len(self.key...
Provide a redacted version of the token. The *raw* key value should never be displayed anywhere!
token
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def expired(self) -> bool: """Test if this token has expired.""" return ( self.expiry is not None and self.expiry < InvenTree.helpers.current_date() )
Test if this token has expired.
expired
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def save(self, *args, **kwargs): """Intercept the 'save' functionality to make additional permission changes. It does not make sense to be able to change / create something, but not be able to view it! """ if self.can_add or self.can_change or self.can_delete: self.c...
Intercept the 'save' functionality to make additional permission changes. It does not make sense to be able to change / create something, but not be able to view it!
save
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def get_owners_matching_user(cls, user): """Return all "owner" objects matching the provided user. Includes: - An exact match for the user - Any groups that the user is a part of """ user_type = ContentType.objects.get(app_label='auth', model='user') group_type =...
Return all "owner" objects matching the provided user. Includes: - An exact match for the user - Any groups that the user is a part of
get_owners_matching_user
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def name(self): """Return the 'name' of this owner.""" if self.owner_type.name == 'user' and get_global_setting( 'DISPLAY_FULL_NAMES', cache=True ): if self.owner and hasattr(self.owner, 'get_full_name'): # Use the get_full_name method if available ...
Return the 'name' of this owner.
name
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def create(cls, obj): """Check if owner exist then create new owner entry.""" # Check for existing owner existing_owner = cls.get_owner(obj) if not existing_owner: # Create new owner try: return cls.objects.create(owner=obj) except Int...
Check if owner exist then create new owner entry.
create
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def get_owner(cls, user_or_group): """Get owner instance for a group or user.""" user_model = get_user_model() owner = None content_type_id = 0 content_type_id_list = [ ContentType.objects.get_for_model(Group).id, ContentType.objects.get_for_model(user_mod...
Get owner instance for a group or user.
get_owner
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def get_related_owners(self, include_group=False): """Get all owners "related" to an owner. This method is useful to retrieve all "user-type" owners linked to a "group-type" owner """ user_model = get_user_model() related_owners = None if type(self.owner) is Group: ...
Get all owners "related" to an owner. This method is useful to retrieve all "user-type" owners linked to a "group-type" owner
get_related_owners
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def is_user_allowed(self, user, include_group: bool = False): """Check if user is allowed to access something owned by this owner.""" user_owner = Owner.get_owner(user) return user_owner in self.get_related_owners(include_group=include_group)
Check if user is allowed to access something owned by this owner.
is_user_allowed
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def create_owner(sender, instance, **kwargs): """Callback function to create a new owner instance after either a new group or user instance is saved.""" # Ignore during data import process to avoid data duplication if not isImportingData(): Owner.create(obj=instance)
Callback function to create a new owner instance after either a new group or user instance is saved.
create_owner
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def save(self, *args, **kwargs): """Ensure primary_group is a group that the user is a member of.""" if self.primary_group and self.primary_group not in self.user.groups.all(): self.primary_group = None super().save(*args, **kwargs)
Ensure primary_group is a group that the user is a member of.
save
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT
def validate_primary_group_on_save(sender, instance, **kwargs): """Validate primary_group on user profiles when a group is created or updated.""" for user in instance.user_set.all(): profile = user.profile if profile.primary_group and profile.primary_group not in user.groups.all(): p...
Validate primary_group on user profiles when a group is created or updated.
validate_primary_group_on_save
python
inventree/InvenTree
src/backend/InvenTree/users/models.py
https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/users/models.py
MIT