From 48a2a6c76078a43f07e537a07519159129c4dc03 Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Sun, 3 Dec 2017 21:09:02 -0800 Subject: [PATCH] Fix Django 2 deprecations (#227) * 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 --- project/example_app/admin.py | 5 +++- project/project/settings.py | 30 +++++++++++++++------- project/tests/test_config_auth.py | 5 +++- project/tests/test_config_max_body_size.py | 7 +++-- project/tests/test_end_points.py | 5 +++- project/tests/test_silky_middleware.py | 5 +++- project/tests/test_view_sql_detail.py | 5 +++- silk/middleware.py | 5 +++- silk/migrations/0001_initial.py | 6 ++--- silk/model_factory.py | 5 +++- silk/profiling/profiler.py | 9 ++++--- silk/templatetags/silk_nav.py | 5 +++- silk/urls.py | 1 + 13 files changed, 68 insertions(+), 25 deletions(-) diff --git a/project/example_app/admin.py b/project/example_app/admin.py index 42a22d4e..f90891b2 100644 --- a/project/example_app/admin.py +++ b/project/example_app/admin.py @@ -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 diff --git a/project/project/settings.py b/project/project/settings.py index 48437a05..7034512a 100644 --- a/project/project/settings.py +++ b/project/project/settings.py @@ -1,3 +1,4 @@ +import django import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) @@ -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' diff --git a/project/tests/test_config_auth.py b/project/tests/test_config_auth.py index b41fa713..6f7f586d 100644 --- a/project/tests/test_config_auth.py +++ b/project/tests/test_config_auth.py @@ -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 diff --git a/project/tests/test_config_max_body_size.py b/project/tests/test_config_max_body_size.py index 20910f80..9cfc193b 100644 --- a/project/tests/test_config_max_body_size.py +++ b/project/tests/test_config_max_body_size.py @@ -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 @@ -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) \ No newline at end of file + self.assertFalse(response_model.raw_body) diff --git a/project/tests/test_end_points.py b/project/tests/test_end_points.py index bc86c711..8f2b1059 100644 --- a/project/tests/test_end_points.py +++ b/project/tests/test_end_points.py @@ -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 diff --git a/project/tests/test_silky_middleware.py b/project/tests/test_silky_middleware.py index fa6f66b2..44662256 100644 --- a/project/tests/test_silky_middleware.py +++ b/project/tests/test_silky_middleware.py @@ -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 diff --git a/project/tests/test_view_sql_detail.py b/project/tests/test_view_sql_detail.py index 3dec536e..70e0b61a 100644 --- a/project/tests/test_view_sql_detail.py +++ b/project/tests/test_view_sql_detail.py @@ -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 diff --git a/silk/middleware.py b/silk/middleware.py index b058f13b..c38ad3e3 100644 --- a/silk/middleware.py +++ b/silk/middleware.py @@ -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 diff --git a/silk/migrations/0001_initial.py b/silk/migrations/0001_initial.py index cb177dde..ff29b04f 100644 --- a/silk/migrations/0001_initial.py +++ b/silk/migrations/0001_initial.py @@ -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( @@ -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( @@ -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), ), ] diff --git a/silk/model_factory.py b/silk/model_factory.py index ea98815f..3371a9c5 100644 --- a/silk/model_factory.py +++ b/silk/model_factory.py @@ -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 diff --git a/silk/profiling/profiler.py b/silk/profiling/profiler.py index 1a8ef39c..ff5e43b7 100644 --- a/silk/profiling/profiler.py +++ b/silk/profiling/profiler.py @@ -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): diff --git a/silk/templatetags/silk_nav.py b/silk/templatetags/silk_nav.py index 9b80c9f3..d6e81ba1 100644 --- a/silk/templatetags/silk_nav.py +++ b/silk/templatetags/silk_nav.py @@ -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() diff --git a/silk/urls.py b/silk/urls.py index 286a199d..84a6c80a 100644 --- a/silk/urls.py +++ b/silk/urls.py @@ -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'),