Skip to content

Commit

Permalink
Fix Django 2 deprecations (#227)
Browse files Browse the repository at this point in the history
* Make sure all OneToOneFields explicitly declare on_delete

* Switch to django.urls for imports

* Update test project for django 2

* Default to newer MIDDLEWARE setting

* Declare required app_name variable for inclusion
  • Loading branch information
albertyw authored and avelis committed Dec 4, 2017
1 parent 6e93cd4 commit 48a2a6c
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 25 deletions.
5 changes: 4 additions & 1 deletion project/example_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib import admin
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from .models import Blind


Expand Down
30 changes: 21 additions & 9 deletions project/project/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import django
import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Expand All @@ -22,15 +23,26 @@

ROOT_URLCONF = 'project.urls'

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'silk.middleware.SilkyMiddleware'
)
if django.VERSION >= (1, 10):
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'silk.middleware.SilkyMiddleware'
]
else:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'silk.middleware.SilkyMiddleware'
)

WSGI_APPLICATION = 'wsgi.application'

Expand Down
5 changes: 4 additions & 1 deletion project/tests/test_config_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse, NoReverseMatch
try:
from django.urls import reverse, NoReverseMatch
except ImportError:
from django.core.urlresolvers import reverse, NoReverseMatch
from django.test import TestCase

from silk.config import SilkyConfig, default_permissions
Expand Down
7 changes: 5 additions & 2 deletions project/tests/test_config_max_body_size.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.test import TestCase
from mock import Mock
from silk.model_factory import RequestModelFactory, ResponseModelFactory
Expand Down Expand Up @@ -56,4 +59,4 @@ def test_max_response(self):
mock_response.content = 'a'.encode('ascii') * 1024 * 100 # 100kb
mock_response.status_code = 200
response_model = ResponseModelFactory(mock_response).construct_response_model()
self.assertFalse(response_model.raw_body)
self.assertFalse(response_model.raw_body)
5 changes: 4 additions & 1 deletion project/tests/test_end_points.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import random
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.db.models import Count

from django.test import TestCase
Expand Down
5 changes: 4 additions & 1 deletion project/tests/test_silky_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.test import TestCase
from mock import patch, Mock

Expand Down
5 changes: 4 additions & 1 deletion project/tests/test_view_sql_detail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import random

from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.test import TestCase
from silk.config import SilkyConfig
from silk.middleware import silky_reverse
Expand Down
5 changes: 4 additions & 1 deletion silk/middleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import random

from django.core.urlresolvers import reverse, NoReverseMatch
from django.db import transaction, DatabaseError
try:
from django.urls import reverse, NoReverseMatch
except ImportError:
from django.core.urlresolvers import reverse, NoReverseMatch

from django.db.models.sql.compiler import SQLCompiler
from django.utils import timezone
Expand Down
6 changes: 3 additions & 3 deletions silk/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Migration(migrations.Migration):
('raw_body', models.TextField(blank=True, default='')),
('body', models.TextField(blank=True, default='')),
('encoded_headers', models.TextField(blank=True, default='')),
('request', models.OneToOneField(to='silk.Request', related_name='response')),
('request', models.OneToOneField(to='silk.Request', related_name='response', on_delete=models.CASCADE)),
],
),
migrations.CreateModel(
Expand All @@ -72,7 +72,7 @@ class Migration(migrations.Migration):
('end_time', models.DateTimeField(blank=True, null=True)),
('time_taken', models.FloatField(blank=True, null=True)),
('traceback', models.TextField()),
('request', models.ForeignKey(to='silk.Request', blank=True, null=True, related_name='queries')),
('request', models.ForeignKey(to='silk.Request', blank=True, null=True, related_name='queries', on_delete=models.CASCADE)),
],
),
migrations.AddField(
Expand All @@ -83,6 +83,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='profile',
name='request',
field=models.ForeignKey(to='silk.Request', blank=True, null=True),
field=models.ForeignKey(to='silk.Request', blank=True, null=True, on_delete=models.CASCADE),
),
]
5 changes: 4 additions & 1 deletion silk/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import base64
from uuid import UUID

from django.core.urlresolvers import resolve, Resolver404
try:
from django.urls import resolve, Resolver404
except:
from django.core.urlresolvers import resolve, Resolver404

from silk import models
from silk.collector import DataCollector
Expand Down
9 changes: 6 additions & 3 deletions silk/profiling/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):

def _silk_installed(self):
app_installed = 'silk' in settings.INSTALLED_APPS
middleware_installed = 'silk.middleware.SilkyMiddleware' in settings.MIDDLEWARE_CLASSES
if django.VERSION[:2] >= (1, 10):
middleware_installed = middleware_installed or 'silk.middleware.SilkyMiddleware' in settings.MIDDLEWARE
middlewares = getattr(settings, 'MIDDLEWARE', [])
if django.VERSION[0] < 2 and not middlewares:
middlewares = getattr(settings, 'MIDDLEWARE_CLASSES', [])
if not middlewares:
middlewares = []
middleware_installed = 'silk.middleware.SilkyMiddleware' in middlewares
return app_installed and middleware_installed

def _should_profile(self):
Expand Down
5 changes: 4 additions & 1 deletion silk/templatetags/silk_nav.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django import template
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except:
from django.core.urlresolvers import reverse

register = template.Library()

Expand Down
1 change: 1 addition & 0 deletions silk/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from silk.views.sql_detail import SQLDetailView
from silk.views.summary import SummaryView

app_name = 'silk'
urlpatterns = [
url(r'^$', SummaryView.as_view(), name='summary'),
url(r'^requests/$', RequestsView.as_view(), name='requests'),
Expand Down

0 comments on commit 48a2a6c

Please sign in to comment.