Skip to content

Commit 6fe54e9

Browse files
authored
EOL Django Cleaning (#453)
1 parent 9f83f02 commit 6fe54e9

File tree

8 files changed

+48
-54
lines changed

8 files changed

+48
-54
lines changed

django_prometheus/cache/backends/redis.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django import VERSION as DJANGO_VERSION
1+
from django.core.cache.backends.redis import RedisCache as DjangoRedisCache
22
from django_redis import cache, exceptions
33

44
from django_prometheus.cache.metrics import (
@@ -33,20 +33,17 @@ def get(self, key, default=None, version=None, client=None):
3333
return default
3434

3535

36-
if DJANGO_VERSION >= (4, 0):
37-
from django.core.cache.backends.redis import RedisCache as DjangoRedisCache
38-
39-
class NativeRedisCache(DjangoRedisCache):
40-
def get(self, key, default=None, version=None):
41-
django_cache_get_total.labels(backend="native_redis").inc()
42-
try:
43-
result = super().get(key, default=None, version=version)
44-
except Exception:
45-
django_cache_get_fail_total.labels(backend="native_redis").inc()
46-
raise
47-
if result is not None:
48-
django_cache_hits_total.labels(backend="native_redis").inc()
49-
return result
50-
else:
51-
django_cache_misses_total.labels(backend="native_redis").inc()
52-
return default
36+
class NativeRedisCache(DjangoRedisCache):
37+
def get(self, key, default=None, version=None):
38+
django_cache_get_total.labels(backend="native_redis").inc()
39+
try:
40+
result = super().get(key, default=None, version=version)
41+
except Exception:
42+
django_cache_get_fail_total.labels(backend="native_redis").inc()
43+
raise
44+
if result is not None:
45+
django_cache_hits_total.labels(backend="native_redis").inc()
46+
return result
47+
else:
48+
django_cache_misses_total.labels(backend="native_redis").inc()
49+
return default

django_prometheus/db/backends/common.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

django_prometheus/db/backends/postgis/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from django.contrib.gis.db.backends.postgis import base
2+
from django.db.backends.postgresql.base import Cursor
23

3-
from django_prometheus.db.backends.common import get_postgres_cursor_class
44
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
55

66

77
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
88
def get_new_connection(self, *args, **kwargs):
99
conn = super().get_new_connection(*args, **kwargs)
1010
conn.cursor_factory = ExportingCursorWrapper(
11-
conn.cursor_factory or get_postgres_cursor_class(), "postgis", self.vendor
11+
conn.cursor_factory or Cursor(),
12+
"postgis",
13+
self.vendor,
1214
)
15+
1316
return conn
1417

1518
def create_cursor(self, name=None):

django_prometheus/db/backends/postgresql/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from django.db.backends.postgresql import base
2+
from django.db.backends.postgresql.base import Cursor
23

3-
from django_prometheus.db.backends.common import get_postgres_cursor_class
44
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
55

66

77
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
88
def get_new_connection(self, *args, **kwargs):
99
conn = super().get_new_connection(*args, **kwargs)
1010
conn.cursor_factory = ExportingCursorWrapper(
11-
conn.cursor_factory or get_postgres_cursor_class(), self.alias, self.vendor
11+
conn.cursor_factory or Cursor(),
12+
self.alias,
13+
self.vendor,
1214
)
15+
1316
return conn
1417

1518
def create_cursor(self, name=None):

django_prometheus/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ def process_request(self, request):
219219
self.label_metric(self.metrics.requests_by_transport, request, transport=transport).inc()
220220

221221
# Mimic the behaviour of the deprecated "Request.is_ajax()" method.
222-
if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
222+
if request.headers.get("x-requested-with") == "XMLHttpRequest":
223223
self.label_metric(self.metrics.requests_ajax, request).inc()
224224

225-
content_length = int(request.META.get("CONTENT_LENGTH") or 0)
225+
content_length = int(request.headers.get("content-length") or 0)
226226
self.label_metric(self.metrics.requests_body_bytes, request).observe(content_length)
227227
request.prometheus_after_middleware_event = Time()
228228

django_prometheus/tests/end2end/testapp/settings.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
import tempfile
33

4-
from django import VERSION as DJANGO_VERSION
5-
64
from testapp.helpers import get_middleware
75

86
# SECURITY WARNING: keep the secret key used in production secret!
@@ -121,6 +119,10 @@
121119
"BACKEND": "django_prometheus.cache.backends.locmem.LocMemCache",
122120
"LOCATION": os.path.join(_tmp_cache_dir, "locmem_cache"),
123121
},
122+
"native_redis": {
123+
"BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache",
124+
"LOCATION": "redis://127.0.0.1:6379/0",
125+
},
124126
"redis": {
125127
"BACKEND": "django_prometheus.cache.backends.redis.RedisCache",
126128
"LOCATION": "redis://127.0.0.1:6379/1",
@@ -137,12 +139,6 @@
137139
},
138140
}
139141

140-
if DJANGO_VERSION >= (4, 0):
141-
CACHES["native_redis"] = {
142-
"BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache",
143-
"LOCATION": "redis://127.0.0.1:6379/0",
144-
}
145-
146142

147143
# Internationalization
148144
LANGUAGE_CODE = "en-us"

django_prometheus/tests/end2end/testapp/test_caches.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import pytest
2-
from django import VERSION as DJANGO_VERSION
32
from django.core.cache import caches
43
from redis import RedisError
54

65
from django_prometheus.testutils import assert_metric_equal, get_metric
76

8-
_SUPPORTED_CACHES = ["memcached.PyLibMCCache", "memcached.PyMemcacheCache", "filebased", "locmem", "redis"]
9-
if DJANGO_VERSION >= (4, 0):
10-
_SUPPORTED_CACHES.append("native_redis")
7+
_SUPPORTED_CACHES = [
8+
"memcached.PyLibMCCache",
9+
"memcached.PyMemcacheCache",
10+
"filebased",
11+
"locmem",
12+
"native_redis",
13+
"redis",
14+
]
1115

1216

1317
class TestCachesMetrics:
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from django.contrib import admin
2-
from django.urls import include, path, re_path
2+
from django.urls import include, path
33

44
from testapp import views
55

66
urlpatterns = [
7-
re_path(r"^$", views.index),
8-
re_path(r"^help$", views.help),
9-
re_path(r"^slow$", views.slow, name="slow"),
10-
re_path(r"^objection$", views.objection),
11-
re_path(r"^sql$", views.sql),
12-
re_path(r"^newlawn/(?P<location>[a-zA-Z0-9 ]+)$", views.newlawn),
13-
re_path(r"^file$", views.file),
7+
path("", views.index),
8+
path("help", views.help),
9+
path("slow", views.slow, name="slow"),
10+
path("objection", views.objection),
11+
path("sql", views.sql),
12+
path("newlawn/<str:location>", views.newlawn),
13+
path("file", views.file),
1414
path("", include("django_prometheus.urls")),
15-
re_path(r"^admin/", admin.site.urls),
15+
path("admin/", admin.site.urls),
1616
]

0 commit comments

Comments
 (0)