Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting custom field data if custom field object is missing #730

Merged
merged 2 commits into from
Apr 7, 2022

Conversation

kr3ator
Copy link
Contributor

@kr3ator kr3ator commented Apr 5, 2022

Related Issue:

New Behavior

This PR fixes a bug in the set_custom_fields_values() function. After applying these changes custom fields for a given object are set only if the related CustomField object is already defined and if that CustomField object is enabled with the ContentType of the given object.

If the CustomField is present but not enabled for requested ContentType then nothing is set on the object and a helpful log is printed. On subsequent run, after corrections are made the startup script will fill back the missing custom fields for an (now) existing object.
However, the custom field data is set only if the given dictionary key is absent in the object.

Contrast to Current Behavior

Currently there is no check if the CustomField requested as some object's custom_field_data exists or not, neither there is a check if that CustomField object is enabled for the ContentType in question.
Because of that, the custom fields are being saved to the database for a given object regardless of the above conditions. If CustomerField is not defined or it doesn't have the relevant ContentType enabled an error will be thrown at the first try to edit the object in question (e.g., a Device).

Discussion: Benefits and Drawbacks

This fix is backwards compatible. The only drawback is that the set_custom_fields_values() function must be run regardless whether the object in question was created by the custom script or not. Given, that typically there shouldn't be that many custom fields to process this can omitted.

Changes to the Wiki

n/a

Proposed Release Note Entry

Bugfix: prevent saving custom fields data when related CustomField is not defined or without requested ContentType

Double Check

  • I have read the comments and followed the PR template.
  • I have explained my PR according to the information in the comments.
  • My PR targets the develop branch.

@kr3ator kr3ator force-pushed the bugfix/cf_creation branch 3 times, most recently from 8830bba to b5136e8 Compare April 5, 2022 16:02
@kr3ator kr3ator force-pushed the bugfix/cf_creation branch from b5136e8 to 81d9e4f Compare April 6, 2022 12:10
@kr3ator
Copy link
Contributor Author

kr3ator commented Apr 6, 2022

Could someone assist me on this syntax check as I did not modify this line:

2022-04-06 03:48:25 [INFO]   ---------------------------
2022-04-06 03:48:25 [INFO]   File:[/github/workspace/startup_scripts/370_ip_addresses.py]
2022-04-06 03:48:25 [ERROR]   Found errors in [mypy] linter!
2022-04-06 03:48:25 [ERROR]   Error code: 1. Command output:
------
startup_scripts/370_ip_addresses.py:54: error: Dict entry 0 has incompatible type "Optional[str]": "Any"; expected "str": "Any"
startup_scripts/370_ip_addresses.py:56: error: Item "None" of "Optional[Any]" has no attribute "objects"
Found 2 errors in 1 file (checked 1 source file)

@kr3ator kr3ator mentioned this pull request Apr 6, 2022
3 tasks
@tobiasge tobiasge merged commit a6eb4fe into netbox-community:develop Apr 7, 2022
@kr3ator kr3ator deleted the bugfix/cf_creation branch April 12, 2022 09:48
@tobiasge tobiasge mentioned this pull request Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants