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 #3231

Merged

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.

Copy link

github-actions bot commented Nov 21, 2024

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Elapsed time
✅ PYTHON black 994 0 13.24s
✅ PYTHON ruff 990 0 0.1s

See detailed report in MegaLinter reports

MegaLinter is graciously provided by OX Security

Copy link

github-actions bot commented Nov 21, 2024

Test results

    9 files      9 suites   8m 9s ⏱️
2 147 tests 2 147 ✅ 0 💤 0 ❌
4 033 runs  4 033 ✅ 0 💤 0 ❌

Results for commit b2b92d3.

♻️ This comment has been updated with latest results.

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.33%. Comparing base (27b12b5) to head (b2b92d3).
Report is 84 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3231      +/-   ##
==========================================
- Coverage   60.39%   60.33%   -0.06%     
==========================================
  Files         605      606       +1     
  Lines       43687    43700      +13     
  Branches       48       48              
==========================================
- Hits        26383    26366      -17     
- Misses      17292    17322      +30     
  Partials       12       12              

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

@lunkwill42 lunkwill42 force-pushed the feature/maintenance-improve-deleted-component-display branch 2 times, most recently from 467367d to 3bd0ebd Compare November 21, 2024 13:33
@lunkwill42 lunkwill42 marked this pull request as ready for review November 21, 2024 13:44
@lunkwill42 lunkwill42 self-assigned this Nov 21, 2024
Copy link
Contributor

@johannaengland johannaengland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only tiny superficial improvement suggestions

python/nav/models/manage.py Outdated Show resolved Hide resolved
python/nav/web/maintenance/utils.py Outdated Show resolved Hide resolved
python/nav/django/templatetags/maintenance.py Outdated Show resolved Hide resolved
python/nav/web/maintenance/utils.py Show resolved Hide resolved
@lunkwill42 lunkwill42 force-pushed the feature/maintenance-improve-deleted-component-display branch from c403ccc to d950a1c Compare November 29, 2024 08:47
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?
@lunkwill42 lunkwill42 force-pushed the feature/maintenance-improve-deleted-component-display branch from d950a1c to 536b76d Compare November 29, 2024 08:50
lunkwill42 and others added 7 commits November 29, 2024 09:58
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.
As mentioned in code review, the original name doesn't convey much to
new devs.
As noticed in code review, the refactored version of the maintenance
UI did not display the same descriptive title tooltips on component
trails as the original did.

This solves the issue by adding a separate `component_description`
template tag.
Adding minor, but useful suggestions from code review

Co-authored-by: Johanna England <[email protected]>
@lunkwill42 lunkwill42 force-pushed the feature/maintenance-improve-deleted-component-display branch from 536b76d to b2b92d3 Compare November 29, 2024 08:58
Copy link

sonarcloud bot commented Nov 29, 2024

@lunkwill42 lunkwill42 merged commit 5a35066 into master Nov 29, 2024
12 checks passed
@lunkwill42 lunkwill42 deleted the feature/maintenance-improve-deleted-component-display branch November 29, 2024 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
2 participants