From 167784049d4bab3bd003f6171d29be6e2141b8b1 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 6 Apr 2024 19:36:57 +1100 Subject: [PATCH] Improve logic for non-nullable fields (#6962) (#6964) * Improve logic for non-nullable fields (#6962) * Fix URL for unit test * Fix more links * Fix more links * Remove files * Fix CI URL * Fix more docs links * Update docker_install.md Fix another docs link --- InvenTree/InvenTree/admin.py | 40 +++++++++++++++++++++----- InvenTree/InvenTree/tests.py | 2 +- docs/docs/develop/contributing.md | 2 +- docs/docs/extend/plugins.md | 8 +++--- docs/docs/extend/plugins/locate.md | 2 +- docs/docs/extend/plugins/report.md | 2 +- docs/docs/extend/plugins/schedule.md | 2 +- docs/docs/extend/plugins/urls.md | 2 +- docs/docs/extend/plugins/validation.md | 2 +- docs/docs/report/bom.md | 2 +- docs/docs/report/build.md | 2 +- docs/docs/report/helpers.md | 2 +- docs/docs/report/purchase_order.md | 2 +- docs/docs/report/report.md | 2 +- docs/docs/report/return_order.md | 2 +- docs/docs/report/sales_order.md | 2 +- docs/docs/report/stock_location.md | 2 +- docs/docs/report/test.md | 2 +- docs/docs/start/config.md | 2 +- docs/docs/start/docker_install.md | 8 +++--- 20 files changed, 58 insertions(+), 32 deletions(-) diff --git a/InvenTree/InvenTree/admin.py b/InvenTree/InvenTree/admin.py index 1c79d07cc1..4920e0038f 100644 --- a/InvenTree/InvenTree/admin.py +++ b/InvenTree/InvenTree/admin.py @@ -84,22 +84,48 @@ class InvenTreeResource(ModelResource): return [f for f in fields if f.column_name not in fields_to_exclude] + def before_import(self, dataset, using_transactions, dry_run, **kwargs): + """Run custom code before importing data. + + - Determine the list of fields which need to be converted to empty strings + """ + # Construct a map of field names + db_fields = {field.name: field for field in self.Meta.model._meta.fields} + + for field_name, field in self.fields.items(): + # Skip read-only fields (they cannot be imported) + if field.readonly: + continue + + # Determine the name of the associated column in the dataset + column = getattr(field, 'column_name', field_name) + + # Determine the attribute name of the associated database field + attribute = getattr(field, 'attribute', field_name) + + # Check if the associated database field is a non-nullable string + if db_field := db_fields.get(attribute): + if ( + isinstance(db_field, CharField) + and db_field.blank + and not db_field.null + ): + if column not in self.CONVERT_NULL_FIELDS: + self.CONVERT_NULL_FIELDS.append(column) + + return super().before_import(dataset, using_transactions, dry_run, **kwargs) + def before_import_row(self, row, row_number=None, **kwargs): """Run custom code before importing each row. - Convert any null fields to empty strings, for fields which do not support null values """ - # We can automatically determine which fields might need such a conversion - for field in self.Meta.model._meta.fields: - if isinstance(field, CharField): - if field.blank and not field.null: - if field.name not in self.CONVERT_NULL_FIELDS: - self.CONVERT_NULL_FIELDS.append(field.name) - for field in self.CONVERT_NULL_FIELDS: if field in row and row[field] is None: row[field] = '' + return super().before_import_row(row, row_number, **kwargs) + class CustomRateAdmin(RateAdmin): """Admin interface for the Rate class.""" diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 8a3d41b528..2197234c3e 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -541,7 +541,7 @@ class TestHelpers(TestCase): # TODO: Re-implement this test when we are happier with the external service # dl_helper("https://httpstat.us/200?sleep=5000", requests.exceptions.ReadTimeout, timeout=1) - large_img = 'https://github.com/inventree/InvenTree/raw/master/InvenTree/InvenTree/static/img/paper_splash_large.jpg' + large_img = 'https://github.com/inventree/InvenTree/raw/0.14.x/InvenTree/InvenTree/static/img/paper_splash_large.jpg' InvenTreeSetting.set_setting( 'INVENTREE_DOWNLOAD_IMAGE_MAX_SIZE', 1, change_user=None diff --git a/docs/docs/develop/contributing.md b/docs/docs/develop/contributing.md index 12ed77faf7..e8eaea5e0d 100644 --- a/docs/docs/develop/contributing.md +++ b/docs/docs/develop/contributing.md @@ -96,7 +96,7 @@ The HEAD of the "stable" branch represents the latest stable release code. ## API versioning -The [API version](https://github.com/inventree/InvenTree/blob/master/InvenTree/InvenTree/api_version.py) needs to be bumped every time when the API is changed. +The [API version](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/InvenTree/api_version.py) needs to be bumped every time when the API is changed. ## Environment diff --git a/docs/docs/extend/plugins.md b/docs/docs/extend/plugins.md index 16502ff9f1..1e6b1ec96b 100644 --- a/docs/docs/extend/plugins.md +++ b/docs/docs/extend/plugins.md @@ -16,7 +16,7 @@ For further information, read more about [installing plugins](./plugins/install. ### Plugin Base Class -Custom plugins must inherit from the [InvenTreePlugin class](https://github.com/inventree/InvenTree/blob/2d1776a151721d65d0ae007049d358085b2fcfd5/InvenTree/plugin/plugin.py#L204). Any plugins installed via the methods outlined above will be "discovered" when the InvenTree server launches. +Custom plugins must inherit from the [InvenTreePlugin class](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/plugin.py#L204). Any plugins installed via the methods outlined above will be "discovered" when the InvenTree server launches. !!! warning "Namechange" The name of the base class was changed with `0.7.0` from `IntegrationPluginBase` to `InvenTreePlugin`. While the old name is still available till `0.8.0` we strongly suggest upgrading your plugins. Deprecation warnings are raised if the old name is used. @@ -28,7 +28,7 @@ Please read all release notes and watch out for warnings - we generally provide #### Plugins -General classes and mechanisms are provided under the `plugin` [namespaces](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/__init__.py). These include: +General classes and mechanisms are provided under the `plugin` [namespaces](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/__init__.py). These include: ```python # Management objects @@ -44,7 +44,7 @@ MixinNotImplementedError # Is raised if a mixin was not implemented (core mec #### Mixins -Mixins are split up internally to keep the source tree clean and enable better testing separation. All public APIs that should be used are exposed under `plugin.mixins`. These include all built-in mixins and notification methods. An up-to-date reference can be found in the source code (current master can be [found here](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/mixins/__init__.py)). +Mixins are split up internally to keep the source tree clean and enable better testing separation. All public APIs that should be used are exposed under `plugin.mixins`. These include all built-in mixins and notification methods. An up-to-date reference can be found in the source code (current master can be [found here](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/mixins/__init__.py)). #### Models and other internal InvenTree APIs @@ -72,7 +72,7 @@ MIN_VERSION = None # Lowest InvenTree version number that is supported by the p MAX_VERSION = None # Highest InvenTree version number that is supported by the plugin ``` -Refer to the [sample plugins](https://github.com/inventree/InvenTree/tree/master/InvenTree/plugin/samples) for further examples. +Refer to the [sample plugins](https://github.com/inventree/InvenTree/tree/0.14.x/InvenTree/plugin/samples) for further examples. ### Plugin Config diff --git a/docs/docs/extend/plugins/locate.md b/docs/docs/extend/plugins/locate.md index e56f4c06e9..4a6b36d69f 100644 --- a/docs/docs/extend/plugins/locate.md +++ b/docs/docs/extend/plugins/locate.md @@ -28,4 +28,4 @@ If a locate plugin is installed and activated, the [InvenTree mobile app](../../ ### Implementation -Refer to the [InvenTree source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/samples/locate/locate_sample.py) for a simple implementation example. +Refer to the [InvenTree source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/samples/locate/locate_sample.py) for a simple implementation example. diff --git a/docs/docs/extend/plugins/report.md b/docs/docs/extend/plugins/report.md index 1d0dc0c727..3bdc1ccbfb 100644 --- a/docs/docs/extend/plugins/report.md +++ b/docs/docs/extend/plugins/report.md @@ -16,7 +16,7 @@ Additionally the `add_label_context` method, allowing custom context data to be ### Example -A sample plugin which provides additional context data to the report templates can be found [in the InvenTree source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/samples/integration/report_plugin_sample.py): +A sample plugin which provides additional context data to the report templates can be found [in the InvenTree source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/samples/integration/report_plugin_sample.py): ```python """Sample plugin for extending reporting functionality""" diff --git a/docs/docs/extend/plugins/schedule.md b/docs/docs/extend/plugins/schedule.md index 78e70da81c..92f0564efe 100644 --- a/docs/docs/extend/plugins/schedule.md +++ b/docs/docs/extend/plugins/schedule.md @@ -59,4 +59,4 @@ class ScheduledTaskPlugin(ScheduleMixin, SettingsMixin, InvenTreePlugin): ``` !!! info "More Info" - For more information on any of the methods described below, refer to the InvenTree source code. [A working example is available as a starting point](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/samples/integration/scheduled_task.py). + For more information on any of the methods described below, refer to the InvenTree source code. [A working example is available as a starting point](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/samples/integration/scheduled_task.py). diff --git a/docs/docs/extend/plugins/urls.md b/docs/docs/extend/plugins/urls.md index efa0ca8f7c..b8114be2f2 100644 --- a/docs/docs/extend/plugins/urls.md +++ b/docs/docs/extend/plugins/urls.md @@ -65,7 +65,7 @@ Additionally, add the following imports after the extended line. #### Blocks The page_base file is split into multiple sections called blocks. This allows you to implement sections of the webpage while getting many items like navbars, sidebars, and general layout provided for you. -The current default page base can be found [here](https://github.com/inventree/InvenTree/blob/master/InvenTree/templates/page_base.html). Look through this file to determine overridable blocks. The [stock app](https://github.com/inventree/InvenTree/tree/master/InvenTree/stock) offers a great example of implementing these blocks. +The current default page base can be found [here](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/templates/page_base.html). Look through this file to determine overridable blocks. The [stock app](https://github.com/inventree/InvenTree/tree/0.14.x/InvenTree/stock) offers a great example of implementing these blocks. !!! warning "Sidebar Block" You may notice that implementing the `sidebar` block doesn't initially work. Be sure to enable the sidebar using JavaScript. This can be achieved by appending the following code, replacing `label` with a label of your choosing, to the end of your template file. diff --git a/docs/docs/extend/plugins/validation.md b/docs/docs/extend/plugins/validation.md index 2968ce6a1d..829f68a40f 100644 --- a/docs/docs/extend/plugins/validation.md +++ b/docs/docs/extend/plugins/validation.md @@ -9,7 +9,7 @@ The `ValidationMixin` class enables plugins to perform custom validation of obje Any of the methods described below can be implemented in a custom plugin to provide functionality as required. !!! info "More Info" - For more information on any of the methods described below, refer to the InvenTree source code. [A working example is available as a starting point](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/samples/integration/validation_sample.py). + For more information on any of the methods described below, refer to the InvenTree source code. [A working example is available as a starting point](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/plugin/samples/integration/validation_sample.py). !!! info "Multi Plugin Support" It is possible to have multiple plugins loaded simultaneously which support validation methods. For example when validating a field, if one plugin returns a null value (`None`) then the *next* plugin (if available) will be queried. diff --git a/docs/docs/report/bom.md b/docs/docs/report/bom.md index f6f2864e01..5a426e4cc0 100644 --- a/docs/docs/report/bom.md +++ b/docs/docs/report/bom.md @@ -183,4 +183,4 @@ Finally added a `{% raw %}|floatformat:0{% endraw %}` to the quantity that remov A default *BOM Report* template is provided out of the box, which is useful for generating simple test reports. Furthermore, it may be used as a starting point for developing custom BOM reports: -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_bill_of_materials_report.html) for the default test report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_bill_of_materials_report.html) for the default test report template. diff --git a/docs/docs/report/build.md b/docs/docs/report/build.md index f1b138f5eb..242acd5438 100644 --- a/docs/docs/report/build.md +++ b/docs/docs/report/build.md @@ -321,4 +321,4 @@ This will result a report page like this: A default *Build Report* template is provided out of the box, which is useful for generating simple test reports. Furthermore, it may be used as a starting point for developing custom BOM reports: -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_build_order_base.html) for the default build report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_build_order_base.html) for the default build report template. diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 6aa5475e23..ff23ddb136 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -12,7 +12,7 @@ Some common functions are provided for use in custom report and label templates. ``` !!! tip "Use the Source, Luke" - To see the full range of available helper functions, refer to the source file [report.py](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templatetags/report.py) where these functions are defined! + To see the full range of available helper functions, refer to the source file [report.py](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templatetags/report.py) where these functions are defined! ## Assigning Variables diff --git a/docs/docs/report/purchase_order.md b/docs/docs/report/purchase_order.md index d7c1bdf098..fcf0874ae6 100644 --- a/docs/docs/report/purchase_order.md +++ b/docs/docs/report/purchase_order.md @@ -62,4 +62,4 @@ Price: {% render_currency line.total_line_price %} A default *Purchase Order Report* template is provided out of the box, which is useful for generating simple test reports. Furthermore, it may be used as a starting point for developing custom BOM reports: -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_po_report_base.html) for the default purchase order report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_po_report_base.html) for the default purchase order report template. diff --git a/docs/docs/report/report.md b/docs/docs/report/report.md index e82ac5ead6..e9fce45ece 100644 --- a/docs/docs/report/report.md +++ b/docs/docs/report/report.md @@ -155,7 +155,7 @@ InvenTree supports the following reporting functionality: InvenTree is supplied with a number of default templates "out of the box". These are generally quite simple, but serve as a starting point for building custom reports to suit a specific need. !!! tip "Read the Source" - The source code for the default reports is [available on GitHub](https://github.com/inventree/InvenTree/tree/master/InvenTree/report/templates/report). Use this as a guide for generating your own reports! + The source code for the default reports is [available on GitHub](https://github.com/inventree/InvenTree/tree/0.14.x/InvenTree/report/templates/report). Use this as a guide for generating your own reports! ## Creating Reports diff --git a/docs/docs/report/return_order.md b/docs/docs/report/return_order.md index de5d19f962..f81a77ebf5 100644 --- a/docs/docs/report/return_order.md +++ b/docs/docs/report/return_order.md @@ -23,4 +23,4 @@ In addition to the default report context variables, the following context varia A default report template is provided out of the box, which can be used as a starting point for developing custom return order report templates. -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_return_order_report_base.html) for the default return order report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_return_order_report_base.html) for the default return order report template. diff --git a/docs/docs/report/sales_order.md b/docs/docs/report/sales_order.md index b8646c3015..8fca4ed065 100644 --- a/docs/docs/report/sales_order.md +++ b/docs/docs/report/sales_order.md @@ -28,4 +28,4 @@ In addition to the default report context variables, the following variables are A default *Sales Order Report* template is provided out of the box, which is useful for generating simple test reports. Furthermore, it may be used as a starting point for developing custom BOM reports: -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_so_report_base.html) for the default sales order report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_so_report_base.html) for the default sales order report template. diff --git a/docs/docs/report/stock_location.md b/docs/docs/report/stock_location.md index 27fb1b729b..6978231d18 100644 --- a/docs/docs/report/stock_location.md +++ b/docs/docs/report/stock_location.md @@ -13,4 +13,4 @@ You can use all content variables from the [StockLocation](./context_variables.m A default report template is provided out of the box, which can be used as a starting point for developing custom return order report templates. -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_slr_report.html) for the default stock location report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_slr_report.html) for the default stock location report template. diff --git a/docs/docs/report/test.md b/docs/docs/report/test.md index 00fbcf0b67..250ba25c5b 100644 --- a/docs/docs/report/test.md +++ b/docs/docs/report/test.md @@ -84,4 +84,4 @@ A default *Test Report* template is provided out of the box, which is useful for {% include "img.html" %} {% endwith %} -View the [source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/report/templates/report/inventree_test_report_base.html) for the default test report template. +View the [source code](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/report/templates/report/inventree_test_report_base.html) for the default test report template. diff --git a/docs/docs/start/config.md b/docs/docs/start/config.md index 49ff6ae994..15222542a6 100644 --- a/docs/docs/start/config.md +++ b/docs/docs/start/config.md @@ -22,7 +22,7 @@ The InvenTree server tries to locate the `config.yaml` configuration file on sta !!! tip "Config File Location" When the InvenTree server boots, it will report the location where it expects to find the configuration file -The configuration file *template* can be found on [GitHub](https://github.com/inventree/InvenTree/blob/master/InvenTree/config_template.yaml) +The configuration file *template* can be found on [GitHub](https://github.com/inventree/InvenTree/blob/0.14.x/InvenTree/config_template.yaml) !!! info "Template File" The default configuration file (as defined by the template linked above) will be copied to the specified configuration file location on first run, if a configuration file is not found in that location. diff --git a/docs/docs/start/docker_install.md b/docs/docs/start/docker_install.md index 28c787da67..3fdd266512 100644 --- a/docs/docs/start/docker_install.md +++ b/docs/docs/start/docker_install.md @@ -27,13 +27,13 @@ The following guide provides a streamlined production InvenTree installation, wi ### Required Files -The following files required for this setup are provided with the InvenTree source, located in the `./docker/` directory of the [InvenTree source code](https://github.com/inventree/InvenTree/tree/master/docker/): +The following files required for this setup are provided with the InvenTree source, located in the `./docker/` directory of the [InvenTree source code](https://github.com/inventree/InvenTree/tree/0.14.x/docker/): | Filename | Description | | --- | --- | -| [docker-compose.yml](https://github.com/inventree/InvenTree/blob/master/docker/docker-compose.yml) | The docker compose script | -| [.env](https://github.com/inventree/InvenTree/blob/master/docker/.env) | Environment variables | -| [Caddyfile](https://github.com/inventree/InvenTree/blob/master/docker/Caddyfile) | Caddy configuration file | +| [docker-compose.yml](https://github.com/inventree/InvenTree/blob/0.14.x/docker/docker-compose.yml) | The docker compose script | +| [.env](https://github.com/inventree/InvenTree/blob/0.14.x/docker/.env) | Environment variables | +| [Caddyfile](https://github.com/inventree/InvenTree/blob/0.14.x/docker/Caddyfile) | Caddy configuration file | Download these files to a directory on your local machine.