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

Add optional django-silk #1809

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.env
.envrc
*.sqlite3
*.prof
node_modules
env/
__pycache__
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.env
.envrc
*.sqlite3
*.prof
node_modules
env/
__pycache__
Expand Down
38 changes: 27 additions & 11 deletions privaterelay/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@
from hashlib import sha512
import base64


# This needs to be before markus, which imports pytest
IN_PYTEST = "pytest" in sys.modules
if IN_PYTEST:
assert 1==2
Comment on lines +26 to +29
Copy link
Member

Choose a reason for hiding this comment

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

question: I don't understand why we're doing this? This looks like it's just going to fail everything when we're "in pytest"?

Copy link
Member Author

Choose a reason for hiding this comment

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

That is because I didn't remove my test code from the PR 😅
Also, I should move this branch to the main repo so CI will run the tests.



from django.conf import settings

import dj_database_url

try:
# Silk is a live profiling and inspection tool for the Django framework
# https://github.com/jazzband/django-silk
import silk
HAS_SILK = True
except ImportError:
HAS_SILK = False

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -42,6 +57,7 @@
INTERNAL_IPS = config(
'DJANGO_INTERNAL_IPS', default=[]
)
USE_SILK = DEBUG and HAS_SILK and not IN_PYTEST

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Expand Down Expand Up @@ -84,9 +100,8 @@
"'self'",
'https://www.google-analytics.com/',
)
# TODO: Add with silk
# if settings.DEBUG:
# CSP_SCRIPT_SRC += ("'unsafe-inline'",)
if USE_SILK:
CSP_SCRIPT_SRC += ("'unsafe-inline'",)

csp_style_values = ["'self'"]
# Next.js dynamically inserts the relevant styles when switching pages,
Expand Down Expand Up @@ -115,9 +130,6 @@
csp_style_values.append("'sha512-%s'" % hash)

CSP_STYLE_SRC = tuple(csp_style_values)
# TODO: Add with silk
# if settings.DEBUG:
# CSP_STYLE_SRC += ("'unsafe-inline'",)

CSP_IMG_SRC = ["'self'"] + AVATAR_IMG_SRC
REFERRER_POLICY = 'strict-origin-when-cross-origin'
Expand Down Expand Up @@ -204,8 +216,9 @@
INSTALLED_APPS += [
'debug_toolbar',
'drf_yasg',
# 'silk',
]
if USE_SILK:
INSTALLED_APPS.append("silk")

if ADMIN_ENABLED:
INSTALLED_APPS += [
Expand Down Expand Up @@ -233,11 +246,10 @@ def _get_initial_middleware():

MIDDLEWARE = _get_initial_middleware()

if USE_SILK:
MIDDLEWARE.append('silk.middleware.SilkyMiddleware')
if DEBUG:
MIDDLEWARE += [
# 'silk.middleware.SilkyMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware')

MIDDLEWARE += [
'django.middleware.security.SecurityMiddleware',
Expand Down Expand Up @@ -695,3 +707,7 @@ def _get_initial_middleware():
}
]
)

if USE_SILK:
SILKY_PYTHON_PROFILER=True
SILKY_PYTHON_PROFILER_BINARY=True
4 changes: 3 additions & 1 deletion privaterelay/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
# path('silk/', include('silk.urls', namespace='silk')),
]
if settings.USE_SILK:
import silk
urlpatterns.append(path('silk/', include('silk.urls', namespace='silk')))

if settings.ADMIN_ENABLED:
urlpatterns += [
Expand Down