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

Properly upgrade to Django 2.2 #2315

Merged
merged 4 commits into from
Nov 26, 2021
Merged
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
6 changes: 3 additions & 3 deletions bin/smsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import sys
import time

from django.utils.encoding import smart_text
from nav.compatibility import smart_str

import nav.config
import nav.daemon
Expand Down Expand Up @@ -431,8 +431,8 @@ def backoffaction(error, retrylimitaction):
for index, msg in enumerate(msgs):
error_message += u'\n%s: "%s" --> %s' % (
index + 1,
smart_text(msg['msg']),
smart_text(msg['name']),
smart_str(msg['msg']),
smart_str(msg['name']),
)

error_message += u"\nError message: %s" % error
Expand Down
2 changes: 1 addition & 1 deletion python/nav/alertengine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from datetime import datetime, timedelta

from django.db import transaction, reset_queries
from django.utils.lru_cache import lru_cache

from nav.compatibility import lru_cache
from nav.models.profiles import (
Account,
AccountAlertQueue,
Expand Down
6 changes: 3 additions & 3 deletions python/nav/auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from __future__ import unicode_literals, absolute_import

import logging
from nav.compatibility import force_str

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.encoding import force_text
from django.utils.timezone import now as utcnow

from nav.models.fields import VarcharField, LegacyGenericForeignKey
Expand Down Expand Up @@ -99,8 +99,8 @@ def add_log_entry(
self.target_pk = target.pk if target else None
self.timestamp = utcnow()
self.subsystem = subsystem if subsystem else None
self.before = force_text(before)
self.after = force_text(after)
self.before = force_str(before)
self.after = force_str(after)
self.save()
return self

Expand Down
4 changes: 2 additions & 2 deletions python/nav/auditlog/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

from django.utils.encoding import force_text
from nav.compatibility import force_str
from django.db.models import Q

from . import find_modelname
Expand All @@ -26,7 +26,7 @@ def get_auditlog_entries(
if pks is None:
pks = []
if queryset is not None and queryset.exists():
qs_pks = set(force_text(o.pk) for o in queryset)
qs_pks = set(force_str(o.pk) for o in queryset)
if qs_pks:
if pks:
pks = qs_pks.intersection(pks)
Expand Down
25 changes: 25 additions & 0 deletions python/nav/compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Django 2.2 has only *_text, Django 3.2 has both *_text and *_str,
# Django 4.0 has only *_str. These are imported so many places that
# it is better to do it once, hence this file

# When no longer supporting 2.2:
# s/nav.compatibility \(import \w+_str\)/django.utils.encoding \1/
try:
from django.utils.encoding import force_str
except ImportError:
from django.utils.encoding import force_text as force_str

try:
from django.utils.encoding import smart_str
except ImportError:
from django.utils.encoding import smart_text as smart_str

# lru_cache isn't used that much but one application of sed is faster
# than changing a block into a line three times.

# When no longer supporting 2.2:
# s/nav.compatibility import lru_cache/functools import lru_cache/
try:
from functools import lru_cache
except ImportError:
from django.utils.lru_cache import lru_cache
2 changes: 1 addition & 1 deletion python/nav/django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

"""Utility methods for django used in NAV"""
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import FieldDoesNotExist
from django.urls import reverse
from django.utils.http import urlencode

Expand Down
3 changes: 2 additions & 1 deletion python/nav/metrics/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

import re

from django.utils.lru_cache import lru_cache
from nav.compatibility import lru_cache
from django.utils.six import iteritems

from nav.models.manage import Netbox, Interface, Prefix, Sensor


Expand Down
4 changes: 2 additions & 2 deletions python/nav/mibs/cisco_process_mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.utils.six import itervalues, iteritems
from twisted.internet import defer

from django.utils.encoding import smart_text
from nav.compatibility import smart_str

from nav.smidumps import get_mib
from nav.mibs import mibretriever
Expand Down Expand Up @@ -62,7 +62,7 @@ def _get_cpu_names(self, indexes):
names = yield self.agent_proxy.get(oids)
self._logger.debug("cpu name result: %r", names)
names = {
OID(oid)[-1]: smart_text(value) for oid, value in names.items() if value
OID(oid)[-1]: smart_str(value) for oid, value in names.items() if value
}
defer.returnValue(names)

Expand Down
4 changes: 2 additions & 2 deletions python/nav/mibs/itw_mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Uses the vendor-specifica IT-WATCHDOGS-MIB to detect and collect
sensor-information.
"""
from django.utils.encoding import smart_text
from nav.compatibility import smart_str
from django.utils.six import itervalues
from twisted.internet import defer

Expand Down Expand Up @@ -346,7 +346,7 @@ def _make_result_dict(
if not sensor_oid or not base_oid or not serial or not desc:
return {}
oid = OID(base_oid) + OID(sensor_oid)
internal_name = smart_text(serial) + desc
internal_name = smart_str(serial) + desc
res = {
'oid': oid,
'unit_of_measurement': u_o_m,
Expand Down
4 changes: 2 additions & 2 deletions python/nav/web/api/v1/alert_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import urlize
from django.urls import reverse
from django.utils.encoding import force_text
from nav.compatibility import force_str
from django.utils.html import strip_tags
from rest_framework import serializers

Expand Down Expand Up @@ -91,7 +91,7 @@ class AlertSerializerBase(serializers.ModelSerializer):
@staticmethod
def get_subject(obj):
"""Return textual description of object"""
return force_text(obj.get_subject())
return force_str(obj.get_subject())

@staticmethod
def get_subject_url(obj):
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.http import HttpResponse, JsonResponse
from django.db.models import Q
from django.db.models.fields.related import ManyToOneRel as _RelatedObject
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import FieldDoesNotExist
import iso8601

from django_filters.rest_framework import DjangoFilterBackend, FilterSet
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/ipdevinfo/host_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@


import IPy
from django.utils.lru_cache import lru_cache
from nav import asyncdns
from nav.compatibility import lru_cache

from nav.util import is_valid_ip

Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/seeddb/utils/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from django.db.models import Model
from django.shortcuts import render
from django.db.models.fields import FieldDoesNotExist
from django.core.exceptions import FieldDoesNotExist
from django.urls import reverse
from django.utils.six import iteritems

Expand Down
4 changes: 2 additions & 2 deletions python/nav/web/useradmin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from datetime import date, timedelta

from django import forms
from django.utils.encoding import force_text
from nav.compatibility import force_str

from crispy_forms.helper import FormHelper
from crispy_forms_foundation.layout import (
Expand Down Expand Up @@ -300,7 +300,7 @@ def __init__(self, *args, **kwargs):
def clean_endpoints(self):
"""Convert endpoints from list to dictionary"""
endpoints = self.cleaned_data.get('endpoints')
return {x: force_text(self.available_endpoints.get(x)) for x in endpoints}
return {x: force_str(self.available_endpoints.get(x)) for x in endpoints}

class Meta(object):
model = APIToken
Expand Down
Loading