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

Refactor maintenance task views to improve display of deleted components #3230

Closed

Conversation

lunkwill42
Copy link
Member

This fixes #3228, but it was not feasible without a significant refactoring of the maintenance task UI codebase.

The codebase originally stems from a time before Django was added to NAV, and was significantly convoluted compared to what it needs to be. This PR makes it slightly less so, but there is still some way to go:

There was a lot of processing and custom validation code for the component fields of the maintenance task forms, which ended up producing large hierarchies of dicts and lists (some of which were fed directly to templates), instead of re-using ORM functionality. While much of the processing would be best encapsulated in Django Forms, this refactor mostly concerns itself with using ORM objects where we can.

The ultimate goal was really only to be able to display deleted components in a better way, by utilizing a new component description field. This is achieved in this PR by replacing the template render data witch actual lists of Django model objects, and by replacing the missing objects with a special MissingComponent class.

Please do ask for clarifications if necessary.

The description field will be mainly to store component descriptions
for future use in the case where the original object has been deleted
from the NAV database.
This is useful to get a reference to the model class of the
referenced component, even if the foreign key itself no longer resolves
to an existing record
They way the maintenance UI builds data structures to describe
maintenance components (and their "trails") is quite obtuse, and seems
to be a strange leftover from the olden days before Django was
introduced to NAV.

This can be done a lot cleaner, while improving on how missing
components are described.

These classes and functions represent an alternative way to build
component trails to place in the template render context.
These type hints help a lot when working in an IDE
This replaces the usage of the old component trail generating functions
with the new ones, and updates the templates accordingly.

It also factors out `frag-component-trail.html` to a separate template
fragment for clarity.
We would never refer to an IP device as an `ip device` in the UI,
`IP` should always be upper case.
There is a definitive canonical URL for Locations, so we should
reflect it in the model - and thereby make it easier for the maintenance
task subsystem to make links to location components.
The imports were getting chaotic in `views.py`.  Used isort to clean
it up.
This more or less removes the redundant strings used in the function,
by using a list of allowed/expected component keys.
Add each component error from POST data processing as its
own error message.  It got very unreadable when a bug caused all
component keys to generate errors.
Why hardcode the list of component keys to put in
the data structure when we already have the definitive list in a
constant?
This replaces `utils.components_for_keys()` with
`get_components_from_keydict()`, which works more like
the newer `get_components()`, but works on POST data
rather than Task object data.

The end result is an edit view that builds the component
trails use in the maintenance in much the same way as the
refactored `view()` view, which means that the HTML template
for the `edit()` view can now also re-use the
`frag-component-trail.html` template.
These are no longer used by any code.
It's stupid to keep a hardcoded list of component types that only
accept integer primary keys, when can be detected from the
corresponding model definitions.  This takes away the redundancy.
Copy link

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Elapsed time
✅ PYTHON black 991 0 12.97s
✅ PYTHON ruff 987 0 0.1s

See detailed report in MegaLinter reports

MegaLinter is graciously provided by OX Security

Copy link

Test results

    8 files      8 suites   8m 11s ⏱️
2 140 tests 2 129 ✅ 0 💤 11 ❌
4 012 runs  3 990 ✅ 0 💤 22 ❌

For more details on these failures, see this check.

Results for commit 26389a5.

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.37%. Comparing base (27b12b5) to head (26389a5).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3230      +/-   ##
==========================================
- Coverage   60.39%   60.37%   -0.02%     
==========================================
  Files         605      605              
  Lines       43687    43665      -22     
  Branches       48       48              
==========================================
- Hits        26383    26362      -21     
+ Misses      17292    17291       -1     
  Partials       12       12              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@lunkwill42
Copy link
Member Author

Branch name and history are off, closing to create a new PR

@lunkwill42 lunkwill42 closed this Nov 21, 2024
@lunkwill42 lunkwill42 deleted the feature/maintenance-close-on-deleted-components branch November 21, 2024 13:10
lunkwill42 added a commit that referenced this pull request Nov 29, 2024
…eted-component-display

 Refactor maintenance task views to improve display of deleted components #3230
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.

Save a description of maintenance components for use when the referenced components have been deleted
1 participant