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

WIP_Add Django Debug Toolbar #2940

Draft
wants to merge 5 commits into
base: testing
Choose a base branch
from

Conversation

FroggyFlox
Copy link
Member

Initial commit adding Django Debug Toolbar #2939

  • Add django-debug-toolbar package to the optional dev dependency group
  • Add --dev flag to build.sh to run poetry install with that flag
  • Add DjDTB to settings.py
  • Add DjDTB urls

This pull request is created with the only purpose to illustrate what would need to be implemented to use Django Debug Toolbar.
Ideally, all changes to settings.py and urls.py would need to be scripted as well so that no manual configuration would be required.

To run this branch, first checkout, then build from source with the new --dev flag. For instance, I run the following:

cd /opt/rockstor
systemctl stop rockstor rockstor-pre rockstor-build rockstor-bootstrap postgresql && systemctl start postgresql && ./build.sh --dev && poetry run initrock && systemctl enable --now rockstor-bootstrap && poetry run debug-mode ON

Note that DEBUG must be True for the toolbar to show up.

Add django-debug-toolbar package to the optional `dev` dependency group
Add `--dev` flag to build.sh to run `poetry install` with that flag
Add DjDTB to settings.py
Add DjDTB urls
Comment on lines 482 to 487
DEBUG_TOOLBAR_CONFIG = {
# Update to the latest AJAX request
# Without this, we can only catch the initial request
# which is not helpful in most of our cases
"UPDATE_ON_FETCH": True
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
DEBUG_TOOLBAR_CONFIG = {
# Update to the latest AJAX request
# Without this, we can only catch the initial request
# which is not helpful in most of our cases
"UPDATE_ON_FETCH": True
}
DEBUG_TOOLBAR_CONFIG = {
# Update to the latest AJAX request
# Without this, we can only catch the initial request
# which is not helpful in most of our cases
"UPDATE_ON_FETCH": True
# If using htmx, to retain toolbar handle
# through page renders add this
"ROOT_TAG_EXTRA_ATTRS": "hx-preserve"
}

If the project is contemplating to use htmx (#2735 and #2821) then this would also be required (among other things of course to enable htmx overall), according to:
https://django-debug-toolbar.readthedocs.io/en/stable/tips.html#working-with-htmx-and-turbo

Rely on the DJANGO_DEBUG env variable to set DEBUG boolean that will in
turn decide the installation of the dev group dependencies (includes
the Django debug toolbar) as the appearance of the toolbar in the UI.

Leave commented (= unset) by default to keep the default build NOT in
the dev mode.
@FroggyFlox
Copy link
Member Author

@phillxnet , I have now tried a different approach to "trigger" the installation of the dev dependencies and the toolbar itself into Django. The idea was to try to "streamline/simplify" the process for the user/developer and cut down the number of steps. I'm anxious about affecting "normal" builds and installs, though, so I would appreciate your feedback; hopefully my commits here won't affect an RPM build, install, and update.

Testing

Ensure all traces from previous/current install are wiped

systemctl stop rockstor rockstor-pre rockstor-build rockstor-bootstrap
rm -vf /usr/lib/systemd/system/rockstor*
systemctl daemon-reload
# DJANGO_DEBUG should not be set but let's make sure of it
unset $DJANGO_DEBUG

cd /opt/rockstor
rm -rf .venv/
rm -rf static/

Now, we can build
Manually edit .env to include DJANGO_DEBUG=True

Given this would be the very first run of the script, we can't rely on
rockstor-build.service to run build.sh with the ENV from .env so we need to set it manually here

systemctl stop postgresql && systemctl start postgresql && DJANGO_DEBUG=True ./build.sh && poetry run initrock && systemctl enable --now rockstor-bootstrap

Open browser and visit Rockstor's IP: the toolbar shows up

To test rebuild after some development edits:

# Wipe existing venv
cd /opt/rockstor
rm -rf .venv/
rm -rf static/
systemctl stop postgresql rockstor rockstor-pre rockstor-build rockstor-bootstrap && systemctl start rockstor-build && systemctl enable --now rockstor-bootstrap

Open browser and visit Rockstor's IP: the toolbar shows up

To test a normal build still works without installing the Debug Toolbar
Manually edit .env to comment DJANGO_DEBUG=True

cd /opt/rockstor
rm -rf .venv/
rm -rf static/
systemctl stop postgresql rockstor rockstor-pre rockstor-build rockstor-bootstrap && systemctl start rockstor-build && systemctl enable --now rockstor-bootstrap

Open browser and visit Rockstor's IP: the toolbar DOES NOT show up

@FroggyFlox
Copy link
Member Author

Currently exploring the option of NOT having the installation of Django Debug Toolbar conditioned on a dev flag of sort but instead installing it by default (setting it as a normal dependency).

buildvm:/opt/rockstor # poetry add django-debug-toolbar
Using version ^4.4.6 for django-debug-toolbar

Updating dependencies
Resolving dependencies... (0.2s)

Package operations: 1 install, 0 updates, 0 removals

  • Installing django-debug-toolbar (4.4.6)

Writing lock file

Then, clear current install and re-build:

buildvm:/opt/rockstor # rm -rf .initrock .venv/ static/

buildvm:/opt/rockstor # systemctl stop postgresql && systemctl start postgresql && ./build.sh && poetry run initrock && systemctl enable --now rockstor-bootstrap

# confirm DEBUG is false
buildvm:/opt/rockstor # poetry run debug-mode
currently debug flag is False

Go to webUI, create user account (first time login): no toolbar is showed on pages (browsed to a random handful ones).

Now, let's turn debug-mode ON:

buildvm:/opt/rockstor # poetry run debug-mode ON
DEBUG flag is now set to True

Visit webUI: the toolbar does NOT appear...
Try a machine reboot: the toolbar still does NOT appear...

Try a complete re-do:

buildvm:/opt/rockstor # systemctl stop postgresql && systemctl start postgresql && ./build.sh && poetry run initrock && systemctl enable --now rockstor-bootstrap

Visit webUI: the toolbar DOES appear... not sure why, though... still looking.

@FroggyFlox
Copy link
Member Author

Visit webUI: the toolbar DOES appear... not sure why, though... still looking.

The answer seems to be in the static files...
Running:

buildvm:/opt/rockstor # poetry run django-admin collectstatic --no-input --verbosity 2
(...)
Post-processed 'debug_toolbar/css/print.css' as 'debug_toolbar/css/print.fe959e423a6a.css'
Post-processed 'debug_toolbar/css/toolbar.css' as 'debug_toolbar/css/toolbar.75839e761b96.css'
Post-processed 'debug_toolbar/js/history.js' as 'debug_toolbar/js/history.e65591a92b59.js'
Post-processed 'debug_toolbar/js/redirect.js' as 'debug_toolbar/js/redirect.d643ba40b49f.js'
Post-processed 'debug_toolbar/js/timer.js' as 'debug_toolbar/js/timer.928069f58d39.js'
Post-processed 'debug_toolbar/js/toolbar.js' as 'debug_toolbar/js/toolbar.3e473713cbe8.js'
Post-processed 'debug_toolbar/js/utils.js' as 'debug_toolbar/js/utils.eca387b0e53d.js'
(...)
7 static files copied to '/opt/rockstor/static', 516 unmodified, 277 post-processed.

... and we can see that some static files needed by the toolbar need to be processed. The toolbar now shows up in the webUI.

Install django-debug-toolbar by default to simplify its enablement by
users as needed.

Do not use DJANGO_DEBUG env variable anymore as our debug-mode script
would break its use in further uses.
@FroggyFlox
Copy link
Member Author

With the latest commit (4faa050), the process would thus be as follows:

systemctl stop rockstor rockstor-pre rockstor-build rockstor-bootstrap
rm -vf /usr/lib/systemd/system/rockstor*
systemctl daemon-reload

cd /opt/rockstor
rm -rf .venv/
rm -rf static/

userdel radmin

Re-build in normal mode (not DEBUG):

systemctl stop postgresql && systemctl start postgresql && ./build.sh && poetry run initrock && systemctl enable --now rockstor-bootstrap

To use the toolbar:

cd /opt/rockstor
poetry run debug-mode ON
poetry run django-admin collectstatic --no-input --verbosity 2 --clear

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.

3 participants