Small XenApi Fixes, requests cleanup

This commit is contained in:
Adolfo Gómez García 2022-08-16 14:29:38 +02:00
parent 748d8d7464
commit 12846f9b1f
4 changed files with 19 additions and 22 deletions

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2018 Virtual Cable S.L.
# Copyright (c) 2012-2022 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -12,7 +12,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
@ -38,6 +38,7 @@ import logging
from django.db import connections
from django.db.backends.signals import connection_created
# from django.db.models.signals import post_migrate
from django.dispatch import receiver
@ -72,7 +73,7 @@ class UDSAppConfig(AppConfig):
# To make sure that the packages are initialized at this point
from . import services
from . import auths
from . import mfas # To make sure mfas are loaded on memory
from . import mfas
from . import osmanagers
from . import notifiers
from . import transports
@ -86,7 +87,7 @@ class UDSAppConfig(AppConfig):
try:
with connections['persistent'].schema_editor() as schema_editor:
schema_editor.create_model(self.get_model('Notification'))
except Exception:
except Exception: # nosec: intentionally catching all exceptions
# If it fails, it's ok, it just means that it already exists
pass
@ -95,7 +96,6 @@ default_app_config = 'uds.UDSAppConfig'
# Sets up several sqlite non existing methods
@receiver(connection_created)
def extend_sqlite(connection=None, **kwargs):
if connection and connection.vendor == "sqlite":
@ -108,4 +108,3 @@ def extend_sqlite(connection=None, **kwargs):
connection.connection.create_function("MIN", 2, min)
connection.connection.create_function("MAX", 2, max)
connection.connection.create_function("CEIL", 1, math.ceil)

View File

@ -41,9 +41,6 @@ from uds.models import User
logger = logging.getLogger(__name__)
_requests: typing.Dict[int, typing.Tuple[weakref.ref, datetime.datetime]] = {}
class ExtendedHttpRequest(HttpRequest):
ip: str
ip_proxy: str

View File

@ -31,24 +31,25 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import typing
import json
from django.contrib.sessions.serializers import JSONSerializer
class SessionSerializer(JSONSerializer):
class SessionSerializer:
"""
Serializer for django sessions.
"""
def dumps(self, data):
def dumps(self, data) -> bytes:
"""
Serialize data for storage in a session.
"""
return JSONSerializer.dumps(self, data)
return json.dumps(data).encode()
def loads(self, data):
def loads(self, data: bytes) -> typing.Dict[str, typing.Any]:
"""
Deserialize data from a session.
"""
try:
return JSONSerializer.loads(self, data)
return json.loads(data)
except Exception:
return {} # If pickle session was used, we get an exception, so we return an empty dict

View File

@ -164,13 +164,13 @@ class Session(xmlrpclib.ServerProxy):
)
and ignore_ssl
):
ctx = ssl._create_unverified_context()
ctx = ssl._create_unverified_context() # nosec: Xen Server will not have a valid cert
xmlrpclib.ServerProxy.__init__(
self, uri, transport, encoding, verbose, allow_none, context=ctx
self, uri, transport, encoding, bool(verbose), bool(allow_none), context=ctx
)
else:
xmlrpclib.ServerProxy.__init__(
self, uri, transport, encoding, verbose, allow_none
self, uri, transport, encoding, bool(verbose), bool(allow_none)
)
self.transport = transport
self._session = None
@ -229,10 +229,10 @@ class Session(xmlrpclib.ServerProxy):
self.API_version = API_VERSION_1_1
def _get_api_version(self):
pool = self.xenapi.pool.get_all()[0]
host = self.xenapi.pool.get_master(pool)
major = self.xenapi.host.get_API_version_major(host)
minor = self.xenapi.host.get_API_version_minor(host)
pool = self.xenapi.pool.get_all()[0] # type: ignore
host = self.xenapi.pool.get_master(pool) # type: ignore
major = self.xenapi.host.get_API_version_major(host) # type: ignore
minor = self.xenapi.host.get_API_version_minor(host) # type: ignore
return "%s.%s" % (major, minor)
def __getattr__(self, name):