Skip to content

Commit

Permalink
Merge pull request #217 from rtibbles/django_unchained
Browse files Browse the repository at this point in the history
Upgrade Django to 3.2
  • Loading branch information
bjester committed Mar 7, 2024
2 parents 594138c + d79e774 commit 64b4790
Show file tree
Hide file tree
Showing 28 changed files with 239 additions and 295 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def process_docstring(app, what, name, obj, options, lines):

# Add the field's type to the docstring
if isinstance(field, models.ForeignKey):
to = field.rel.to
to = field.remote_field.model
lines.append(u':type %s: %s to :class:`~%s`' % (field.attname, type(field).__name__, to))
else:
lines.append(u':type %s: %s' % (field.attname, type(field).__name__))
Expand Down
7 changes: 6 additions & 1 deletion morango/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.core.exceptions import ValidationError
from django.utils import timezone
from ipware.ip import get_ip
from ipware import get_client_ip
from rest_framework import mixins
from rest_framework import pagination
from rest_framework import response
Expand Down Expand Up @@ -60,6 +60,11 @@ def controller_signal_logger(context=None):
session_controller.signals.connect(controller_signal_logger)


def get_ip(request):
client_ip, _ = get_client_ip(request)
return client_ip


class CertificateChainViewSet(viewsets.ViewSet):
permissions = (permissions.CertificatePushPermissions,)

Expand Down
28 changes: 28 additions & 0 deletions morango/migrations/0024_auto_20240129_1757.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.23 on 2024-01-29 17:57
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
('morango', '0023_add_instance_id_fields'),
]

operations = [
migrations.AlterField(
model_name='certificate',
name='level',
field=models.PositiveIntegerField(editable=False),
),
migrations.AlterField(
model_name='certificate',
name='lft',
field=models.PositiveIntegerField(editable=False),
),
migrations.AlterField(
model_name='certificate',
name='rght',
field=models.PositiveIntegerField(editable=False),
),
]
6 changes: 0 additions & 6 deletions morango/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
from morango.models.fields.crypto import SharedKey
from morango.models.fields.uuids import UUIDModelMixin
from morango.models.manager import SyncableModelManager
from morango.models.morango_mptt import MorangoMPTTModel
from morango.models.morango_mptt import MorangoMPTTTreeManager
from morango.models.morango_mptt import MorangoTreeQuerySet
from morango.models.query import SyncableModelQuerySet
from morango.registry import syncable_models

Expand All @@ -41,9 +38,6 @@
"SyncableModelManager",
"SyncableModelQuerySet",
"syncable_models",
"MorangoTreeQuerySet",
"MorangoMPTTTreeManager",
"MorangoMPTTModel",
"DatabaseIDModel",
"InstanceIDModel",
"SyncSession",
Expand Down
7 changes: 3 additions & 4 deletions morango/models/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from django.db import models
from django.db import transaction
from django.utils import timezone
from django.utils.six import string_types

from .fields.crypto import Key
from .fields.crypto import PrivateKeyField
Expand Down Expand Up @@ -195,7 +194,7 @@ def check_certificate(self):
def save_certificate_chain(cls, cert_chain, expected_last_id=None):

# parse the chain from json if needed
if isinstance(cert_chain, string_types):
if isinstance(cert_chain, str):
cert_chain = json.loads(cert_chain)

# start from the bottom of the chain
Expand Down Expand Up @@ -320,15 +319,15 @@ def get_scope(self, params):
return Scope(definition=self, params=params)

def get_description(self, params):
if isinstance(params, string_types):
if isinstance(params, str):
params = json.loads(params)
return string.Template(self.description).safe_substitute(params)


class Filter(object):
def __init__(self, template, params={}):
# ensure params have been deserialized
if isinstance(params, string_types):
if isinstance(params, str):
params = json.loads(params)
self._template = template
self._params = params
Expand Down
34 changes: 13 additions & 21 deletions morango/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from django.db.models.expressions import CombinedExpression
from django.db.models.fields.related import ForeignKey
from django.db.models.functions import Cast
from django.utils import six
from django.utils import timezone
from django.utils.functional import cached_property

Expand All @@ -37,7 +36,6 @@
from morango.models.fields.uuids import UUIDModelMixin
from morango.models.fsic_utils import remove_redundant_instance_counters
from morango.models.manager import SyncableModelManager
from morango.models.morango_mptt import MorangoMPTTModel
from morango.models.utils import get_0_4_system_parameters
from morango.models.utils import get_0_5_mac_address
from morango.models.utils import get_0_5_system_id
Expand Down Expand Up @@ -353,10 +351,10 @@ def delete_buffers(self):

def get_touched_record_ids_for_model(self, model):
if isinstance(model, SyncableModel) or (
isinstance(model, six.class_types) and issubclass(model, SyncableModel)
isinstance(model, type) and issubclass(model, SyncableModel)
):
model = model.morango_model_name
_assert(isinstance(model, six.string_types), "Model must resolve to string")
_assert(isinstance(model, str), "Model must resolve to string")
return Store.objects.filter(
model_name=model, last_transfer_session_id=self.id
).values_list("id", flat=True)
Expand Down Expand Up @@ -420,7 +418,7 @@ def char_ids_list(self):
self.annotate(id_cast=Cast("id", TextField()))
# remove dashes from char uuid
.annotate(
fixed_id=Func(F("id_cast"), Value("-"), Value(""), function="replace")
fixed_id=Func(F("id_cast"), Value("-"), Value(""), function="replace", output_field=TextField())
)
# return as list
.values_list("fixed_id", flat=True)
Expand Down Expand Up @@ -610,7 +608,7 @@ def update_fsics(cls, fsics, sync_filter, v2_format=False):
)
else:
updated_fsic = {}
for key, value in six.iteritems(fsics):
for key, value in fsics.items():
if key in internal_fsic:
# if same instance id, update fsic with larger value
if fsics[key] > internal_fsic[key]:
Expand All @@ -620,7 +618,7 @@ def update_fsics(cls, fsics, sync_filter, v2_format=False):
updated_fsic[key] = fsics[key]

# load database max counters
for (key, value) in six.iteritems(updated_fsic):
for (key, value) in updated_fsic.items():
for f in sync_filter:
DatabaseMaxCounter.objects.update_or_create(
instance_id=key, partition=f, defaults={"counter": value}
Expand Down Expand Up @@ -843,10 +841,8 @@ def delete(
with transaction.atomic():
if hard_delete:
# set hard deletion for all related models
for model, instances in six.iteritems(collector.data):
if issubclass(model, SyncableModel) or issubclass(
model, MorangoMPTTModel
):
for model, instances in collector.data.items():
if issubclass(model, SyncableModel):
for obj in instances:
obj._update_hard_deleted_models()
return collector.delete()
Expand Down Expand Up @@ -930,15 +926,8 @@ def serialize(self):
continue
if f.attname in self._morango_internal_fields_not_to_serialize:
continue
# case if model is morango mptt
if f.attname in getattr(
self,
"_internal_mptt_fields_not_to_serialize",
"_internal_fields_not_to_serialize",
):
continue
if hasattr(f, "value_from_object_json_compatible"):
data[f.attname] = f.value_from_object_json_compatible(self)
if getattr(f, "morango_serialize_to_string", False):
data[f.attname] = f.value_to_string(self)
else:
data[f.attname] = f.value_from_object(self)
return data
Expand All @@ -949,7 +938,10 @@ def deserialize(cls, dict_model):
kwargs = {}
for f in cls._meta.concrete_fields:
if f.attname in dict_model:
kwargs[f.attname] = dict_model[f.attname]
if getattr(f, "morango_serialize_to_string", False):
kwargs[f.attname] = f.to_python(dict_model[f.attname])
else:
kwargs[f.attname] = dict_model[f.attname]
return cls(**kwargs)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions morango/models/fields/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def deconstruct(self):


class PublicKeyField(RSAKeyBaseField):
def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
if not value:
return None
return Key(public_key_string=value)
Expand All @@ -363,7 +363,7 @@ def get_prep_value(self, value):


class PrivateKeyField(RSAKeyBaseField):
def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
if not value:
return None
return Key(private_key_string=value)
Expand Down
3 changes: 2 additions & 1 deletion morango/models/fields/uuids.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid

from django.db import models

from morango.utils import _assert


Expand Down Expand Up @@ -41,7 +42,7 @@ def get_db_prep_value(self, value, connection, prepared=False):
raise TypeError(self.error_messages["invalid"] % {"value": value})
return value.hex

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection):
return self.to_python(value)

def to_python(self, value):
Expand Down
33 changes: 0 additions & 33 deletions morango/models/morango_mptt.py

This file was deleted.

11 changes: 5 additions & 6 deletions morango/models/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import hashlib
import ifcfg
import os
import platform
import socket
import subprocess
import sys
import uuid

from .fields.uuids import sha2_uuid

import ifcfg
from django.conf import settings
from django.utils import six

from .fields.uuids import sha2_uuid


def _get_database_path():
Expand Down Expand Up @@ -109,7 +108,7 @@ def _get_android_uuid():
def _do_salted_hash(value):
if not value:
return ""
if not isinstance(value, six.string_types):
if not isinstance(value, str):
value = str(value)
try:
value = value.encode()
Expand Down Expand Up @@ -185,7 +184,7 @@ def _get_mac_address_flags(mac):
"""
See: https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local
"""
if isinstance(mac, six.integer_types):
if isinstance(mac, int):
mac = _mac_int_to_ether(mac)

first_octet = int(mac[:2], base=16)
Expand Down
5 changes: 2 additions & 3 deletions morango/proquint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
functions. For tighter control over the output, see :class:`HumanHasher`.
"""
import uuid
from django.utils import six

# Copyright (c) 2014 SUNET. All rights reserved.
#
Expand Down Expand Up @@ -59,7 +58,7 @@ def from_int(data):
:type data: int
:rtype: string
"""
if not isinstance(data, six.integer_types):
if not isinstance(data, int):
raise TypeError("Input must be integer")

res = []
Expand All @@ -84,7 +83,7 @@ def to_int(data):
:type data: string
:rtype: int
"""
if not isinstance(data, six.string_types):
if not isinstance(data, str):
raise TypeError("Input must be string")

res = 0
Expand Down
33 changes: 1 addition & 32 deletions morango/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from collections import OrderedDict

from django.db.models.fields.related import ForeignKey
from django.utils import six

from morango.constants import transfer_stages
from morango.errors import InvalidMorangoModelConfiguration
Expand Down Expand Up @@ -111,36 +110,6 @@ def populate(self): # noqa: C901
raise InvalidMorangoModelConfiguration(
"Syncing models with more than 1 self referential ForeignKey is not supported."
)
try:
from mptt import models
from morango.models.morango_mptt import (
MorangoMPTTModel,
MorangoMPTTTreeManager,
MorangoTreeQuerySet,
)

# mptt syncable model checks
if issubclass(model, models.MPTTModel):
if not issubclass(model, MorangoMPTTModel):
raise InvalidMorangoModelConfiguration(
"{} that inherits from MPTTModel, should instead inherit from MorangoMPTTModel.".format(
name
)
)
if not isinstance(model.objects, MorangoMPTTTreeManager):
raise InvalidMorangoModelConfiguration(
"Manager for {} must inherit from MorangoMPTTTreeManager.".format(
name
)
)
if not isinstance(model.objects.none(), MorangoTreeQuerySet):
raise InvalidMorangoModelConfiguration(
"Queryset for {} model must inherit from MorangoTreeQuerySet.".format(
name
)
)
except ImportError:
pass
# syncable model checks
if not isinstance(model.objects, SyncableModelManager):
raise InvalidMorangoModelConfiguration(
Expand Down Expand Up @@ -178,7 +147,7 @@ def populate(self): # noqa: C901
self._insert_model_in_dependency_order(model, profile)

# for each profile, create a dict mapping from morango_model_name to model class
for profile, model_list in six.iteritems(self.profile_models):
for profile, model_list in self.profile_models.items():
mapping = OrderedDict()
for model in model_list:
mapping[model.morango_model_name] = model
Expand Down
Loading

0 comments on commit 64b4790

Please sign in to comment.