Updated signatures for type checking on all models

This commit is contained in:
Adolfo Gómez García 2022-04-24 19:07:44 +02:00
parent f3af6a13ff
commit 6a178478b4
34 changed files with 100 additions and 57 deletions

View File

@ -42,7 +42,7 @@ logger = logging.getLogger(__name__)
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.models import CalendarRule, CalendarAction
from uds.models import CalendarRule, CalendarAction, CalendarAccess
class Calendar(UUIDModel, TaggingMixin):
@ -52,9 +52,10 @@ class Calendar(UUIDModel, TaggingMixin):
modified = models.DateTimeField(auto_now=True)
# "fake" declarations for type checking
objects: 'models.BaseManager[Calendar]'
rules: 'models.QuerySet[CalendarRule]'
calendaraction_set: 'models.QuerySet[CalendarAction]'
objects: 'models.manager.Manager[Calendar]'
rules: 'models.manager.RelatedManager[CalendarRule]'
calendaraction_set: 'models.manager.RelatedManager[CalendarAction]'
calendaraccess_set: 'models.manager.RelatedManager[CalendarAccess]'
class Meta:
"""

View File

@ -114,7 +114,7 @@ class CalendarRule(UUIDModel):
)
# "fake" declarations for type checking
objects: 'models.BaseManager[CalendarRule]'
objects: 'models.manager.Manager[CalendarRule]'
class Meta:
"""

View File

@ -53,7 +53,7 @@ class Config(models.Model):
field_type = models.IntegerField(default=-1)
# "fake" declarations for type checking
objects: 'models.BaseManager[Config]'
objects: 'models.manager.Manager[Config]'
class Meta:
"""

View File

@ -52,7 +52,7 @@ class DBFile(UUIDModel):
modified = models.DateTimeField()
# "fake" declarations for type checking
objects: 'models.BaseManager[DBFile]'
objects: 'models.manager.Manager[DBFile]'
@property
def data(self) -> bytes:

View File

@ -59,7 +59,7 @@ class DelayedTask(models.Model):
execution_time = models.DateTimeField(db_index=True)
# "fake" declarations for type checking
objects: 'models.BaseManager[DelayedTask]'
objects: 'models.manager.Manager[DelayedTask]'
class Meta:
"""

View File

@ -46,7 +46,7 @@ from .util import UnsavedForeignKey, getSqlDatetime
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import auths
from uds.models import ServicePool
from uds.models import ServicePool, Permissions
logger = logging.getLogger(__name__)
@ -71,6 +71,7 @@ class Group(UUIDModel):
# "fake" declarations for type checking
objects: 'models.manager.Manager[Group]'
deployedServices: 'models.manager.RelatedManager[ServicePool]'
permissions: 'models.manager.RelatedManager[Permissions]'
class Meta:
"""

View File

@ -46,6 +46,8 @@ from .util import getSqlDatetime
logger = logging.getLogger(__name__)
if typing.TYPE_CHECKING:
from uds.models import ServicePool, MetaPool, ServicePoolGroup
class Image(UUIDModel):
"""
@ -67,7 +69,11 @@ class Image(UUIDModel):
height = models.IntegerField(default=0)
# "fake" declarations for type checking
objects: 'models.BaseManager[Image]'
objects: 'models.manager.RelatedManager[Image]'
deployedServices: 'models.manager.RelatedManager[ServicePool]'
metaPools: 'models.manager.RelatedManager[MetaPool]'
servicesPoolsGroup: 'models.manager.RelatedManager[ServicePoolGroup]'
class Meta:
"""

View File

@ -55,7 +55,7 @@ class Log(models.Model):
data = models.CharField(max_length=255, default='')
# "fake" declarations for type checking
objects: 'models.BaseManager[Log]'
objects: 'models.manager.Manager[Log]'
class Meta:
"""

View File

@ -70,7 +70,7 @@ class ManagedObjectModel(UUIDModel):
"""
return Environment.getEnvForTableElement(self._meta.verbose_name, self.id)
def deserialize(self, obj: Module, values: typing.Optional[typing.Dict[str, str]]):
def deserialize(self, obj: Module, values: typing.Optional[typing.Mapping[str, str]]):
"""
Conditionally deserializes obj if not initialized via user interface and data holds something
"""

View File

@ -41,7 +41,6 @@ from .transport import Transport
from .authenticator import Authenticator
from .uuid_model import UUIDModel
from .tag import TaggingMixin
from uds.models import authenticator
logger = logging.getLogger(__name__)
@ -64,7 +63,7 @@ class Network(UUIDModel, TaggingMixin): # type: ignore
)
# "fake" declarations for type checking
objects: 'models.BaseManager[Network]'
objects: 'models.manager.Manager[Network]'
class Meta(UUIDModel.Meta):
"""

View File

@ -101,7 +101,7 @@ class Notifier(ManagedObjectModel, TaggingMixin):
level = models.PositiveSmallIntegerField(default=NotificationLevel.ERROR)
# "fake" declarations for type checking
objects: 'models.BaseManager[Notifier]'
objects: 'models.manager.Manager[Notifier]'
class Meta:
"""

View File

@ -52,8 +52,8 @@ class OSManager(ManagedObjectModel, TaggingMixin):
"""
# "fake" declarations for type checking
objects: 'models.BaseManager[OSManager]'
deployedServices: 'models.QuerySet[ServicePool]'
objects: 'models.manager.Manager[OSManager]'
deployedServices: 'models.manager.RelatedManager[ServicePool]'
class Meta(ManagedObjectModel.Meta):
"""

View File

@ -84,7 +84,7 @@ class Permissions(UUIDModel):
permission = models.SmallIntegerField(default=PERMISSION_NONE, db_index=True)
# "fake" declarations for type checking
objects: 'models.BaseManager[Permissions]'
objects: 'models.manager.Manager[Permissions]'
@staticmethod
def permissionAsString(perm: int) -> str:

View File

@ -56,8 +56,8 @@ class Provider(ManagedObjectModel, TaggingMixin): # type: ignore
maintenance_mode = models.BooleanField(default=False, db_index=True)
# "fake" declarations for type checking
objects: 'models.BaseManager[Provider]'
services: 'models.QuerySet[Service]'
objects: 'models.manager.Manager[Provider]'
services: 'models.manager.RelatedManager[Service]'
class Meta(ManagedObjectModel.Meta):
"""

View File

@ -57,7 +57,7 @@ class Proxy(UUIDModel, TaggingMixin): # type: ignore
check_cert = models.BooleanField(default=False)
# "fake" declarations for type checking
objects: 'models.BaseManager[Proxy]'
objects: 'models.manager.Manager[Proxy]'
class Meta:
"""

View File

@ -73,8 +73,8 @@ class Scheduler(models.Model):
# primary key id declaration (for type checking)
# "fake" declarations for type checking
objects: 'models.BaseManager[Scheduler]'
id: int # Primary key (Autogenerated by model)
objects: 'models.manager.Manager[Scheduler]'
id: int # Primary key (Autogenerated by model, just for type checking)
class Meta:
"""

View File

@ -91,9 +91,9 @@ class Service(ManagedObjectModel, TaggingMixin): # type: ignore
_cachedInstance: typing.Optional['services.Service'] = None
# "fake" declarations for type checking
objects: 'models.BaseManager[Service]'
deployedServices: 'models.QuerySet[ServicePool]'
aliases: 'models.QuerySet[ServiceTokenAlias]'
objects: 'models.manager.Manager[Service]'
deployedServices: 'models.manager.RelatedManager[ServicePool]'
aliases: 'models.manager.RelatedManager[ServiceTokenAlias]'
class Meta(ManagedObjectModel.Meta):

View File

@ -71,6 +71,7 @@ if typing.TYPE_CHECKING:
Proxy,
MetaPoolMember,
CalendarAccess,
CalendarAction
)
logger = logging.getLogger(__name__)
@ -154,14 +155,14 @@ class ServicePool(UUIDModel, TaggingMixin): # type: ignore
current_pub_revision = models.PositiveIntegerField(default=1)
# "fake" declarations for type checking
objects: 'models.BaseManager[ServicePool]'
publications: 'models.QuerySet[ServicePoolPublication]'
memberOfMeta: 'models.QuerySet[MetaPoolMember]'
userServices: 'models.QuerySet[UserService]'
calendarAccess: 'models.QuerySet[CalendarAccess]'
changelog: 'models.QuerySet[ServicePoolPublicationChangelog]'
objects: 'models.manager.Manager[ServicePool]'
publications: 'models.manager.RelatedManager[ServicePoolPublication]'
memberOfMeta: 'models.manager.RelatedManager[MetaPoolMember]'
userServices: 'models.manager.RelatedManager[UserService]'
calendarAccess: 'models.manager.RelatedManager[CalendarAccess]'
calendaraction_set: 'models.manager.RelatedManager[CalendarAction]'
changelog: 'models.manager.RelatedManager[ServicePoolPublicationChangelog]'
calendaraction_set: typing.Any
class Meta(UUIDModel.Meta):
"""

View File

@ -63,7 +63,7 @@ class ServicePoolGroup(UUIDModel):
)
# "fake" declarations for type checking
objects: 'models.BaseManager[ServicePoolGroup]'
objects: 'models.manager.Manager[ServicePoolGroup]'
class Meta(UUIDModel.Meta):
"""

View File

@ -65,7 +65,7 @@ class ServicePoolPublicationChangelog(models.Model):
log = models.TextField(default='')
# "fake" declarations for type checking
objects: 'models.BaseManager[ServicePoolPublicationChangelog]'
objects: 'models.manager.Manager[ServicePoolPublicationChangelog]'
class Meta(UUIDModel.Meta):
"""
@ -106,8 +106,8 @@ class ServicePoolPublication(UUIDModel):
revision = models.PositiveIntegerField(default=1)
# "fake" declarations for type checking
objects: 'models.BaseManager[ServicePoolPublication]'
userServices: 'models.QuerySet[UserService]'
objects: 'models.manager.Manager[ServicePoolPublication]'
userServices: 'models.manager.RelatedManager[UserService]'
class Meta(UUIDModel.Meta):
"""

View File

@ -56,7 +56,7 @@ class StatsCounters(models.Model):
value = models.IntegerField(db_index=True, default=0)
# "fake" declarations for type checking
objects: 'models.BaseManager[StatsCounters]'
objects: 'models.manager.Manager[StatsCounters]'
class Meta:
"""

View File

@ -60,7 +60,7 @@ class StatsEvents(models.Model):
fld4 = models.CharField(max_length=128, default='')
# "fake" declarations for type checking
objects: 'models.BaseManager[StatsEvents]'
objects: 'models.manager.Manager[StatsEvents]'
class Meta:
"""

View File

@ -51,7 +51,7 @@ class Storage(models.Model):
)
# "fake" declarations for type checking
objects: 'models.BaseManager[Storage]'
objects: 'models.manager.Manager[Storage]'
class Meta:
"""

View File

@ -30,6 +30,7 @@
"""
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
"""
import typing
import logging
@ -38,6 +39,21 @@ from .uuid_model import UUIDModel
logger = logging.getLogger(__name__)
if typing.TYPE_CHECKING:
from uds.models import (
Account,
Authenticator,
Calendar,
MetaPool,
Network,
Notifier,
OSManager,
Provider,
Service,
ServicePool,
Transport,
)
class Tag(UUIDModel):
"""
@ -50,7 +66,21 @@ class Tag(UUIDModel):
tag = models.CharField(max_length=32, db_index=True, unique=True)
# "fake" declarations for type checking
objects: 'models.BaseManager[Tag]'
objects: 'models.manager.Manager[Tag]'
# Every single related class has a relation with this
# Its inverse is "xxx_set" class
account_set: 'models.manager.RelatedManager[Account]'
authenticator_set: 'models.manager.RelatedManager[Authenticator]'
calendar_set: 'models.manager.RelatedManager[Calendar]'
metapool_set: 'models.manager.RelatedManager[MetaPool]'
network_set: 'models.manager.RelatedManager[Network]'
notifier_set: 'models.manager.RelatedManager[Notifier]'
osmanager_set: 'models.manager.RelatedManager[OSManager]'
provider_set: 'models.manager.RelatedManager[Provider]'
service_set: 'models.manager.RelatedManager[Service]'
servicepool_set: 'models.manager.RelatedManager[ServicePool]'
transport_set: 'models.manager.RelatedManager[Transport]'
class Meta:
"""

View File

@ -71,7 +71,7 @@ class TicketStore(UUIDModel):
) # Associated validator for this ticket
# "fake" declarations for type checking
objects: 'models.BaseManager[TicketStore]'
objects: 'models.manager.Manager[TicketStore]'
class InvalidTicket(Exception):
pass

View File

@ -71,10 +71,10 @@ class Transport(ManagedObjectModel, TaggingMixin):
label = models.CharField(max_length=32, default='', db_index=True)
# "fake" declarations for type checking
objects: 'models.BaseManager[Transport]'
objects: 'models.manager.Manager[Transport]'
deployedServices: 'models.QuerySet[ServicePool]'
networks: 'models.QuerySet[Network]'
deployedServices: 'models.manager.RelatedManager[ServicePool]'
networks: 'models.manager.RelatedManager[Network]'
class Meta(ManagedObjectModel.Meta):
"""

View File

@ -48,7 +48,7 @@ class TunnelToken(models.Model):
stamp = models.DateTimeField() # Date creation or validation of this entry
# "fake" declarations for type checking
objects: 'models.BaseManager[TunnelToken]'
objects: 'models.manager.Manager[TunnelToken]'
class Meta:
app_label = 'uds'

View File

@ -50,7 +50,7 @@ class UniqueId(models.Model):
stamp = models.IntegerField(db_index=True, default=0)
# "fake" declarations for type checking
objects: 'models.BaseManager[UniqueId]'
objects: 'models.manager.Manager[UniqueId]'
class Meta:
"""

View File

@ -37,6 +37,7 @@ from django.db import models
from django.db.models import Count, Q, signals
from uds.core import auths
from uds.core.util import log
from uds.models import permissions
from .authenticator import Authenticator
from .util import NEVER, UnsavedForeignKey, getSqlDatetime
@ -44,7 +45,7 @@ from .uuid_model import UUIDModel
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.models import Group, UserService
from uds.models import Group, UserService, Permissions
from uds.core.util.request import ExtendedHttpRequest
@ -78,6 +79,7 @@ class User(UUIDModel):
objects: 'models.manager.Manager[User]'
groups: 'models.manager.RelatedManager[Group]'
userServices: 'models.manager.RelatedManager[UserService]'
permissions: 'models.manager.RelatedManager[Permissions]'
class Meta(UUIDModel.Meta):
"""

View File

@ -51,7 +51,7 @@ class UserPreference(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='preferences')
# "fake" declarations for type checking
objects: 'models.BaseManager[UserPreference]'
objects: 'models.manager.Manager[UserPreference]'
class Meta:
app_label = 'uds'

View File

@ -120,8 +120,8 @@ class UserService(UUIDModel): # pylint: disable=too-many-public-methods
)
# "fake" declarations for type checking
objects: 'models.BaseManager[UserService]'
properties: 'models.QuerySet[UserServiceProperty]'
objects: 'models.manager.Manager[UserService]'
properties: 'models.manager.RelatedManager[UserServiceProperty]'
accounting: 'AccountUsage'
class Meta(UUIDModel.Meta):

View File

@ -53,7 +53,7 @@ class UserServiceProperty(models.Model): # pylint: disable=too-many-public-meth
)
# "fake" declarations for type checking
objects: 'models.BaseManager[UserServiceProperty]'
objects: 'models.manager.Manager[UserServiceProperty]'
class Meta:
"""

View File

@ -31,11 +31,10 @@
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
"""
import logging
from datetime import datetime
from time import mktime
from datetime import datetime
from django.db import models
from django.db import connection
from django.db import connection, models
logger = logging.getLogger(__name__)

View File

@ -48,6 +48,7 @@ class UUIDModel(models.Model):
uuid = models.CharField(max_length=50, default=None, null=True, unique=True)
# Automatic field from Model without a defined specific primary_key
# Just a fake declaration to allow type checking
id: int
class Meta: # pylint: disable=too-few-public-methods
@ -58,7 +59,7 @@ class UUIDModel(models.Model):
# Override default save to add uuid
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
self, *args, **kwargs
):
if not self.uuid:
self.uuid = self.genUuid()
@ -66,8 +67,11 @@ class UUIDModel(models.Model):
self.uuid = (
self.uuid.lower()
) # If we modify uuid elsewhere, ensure that it's stored in lower case
if 'update_fields' in kwargs:
kwargs['update_fields'] = list(kwargs['update_fields']) + ['uuid']
return models.Model.save(self, force_insert, force_update, using, update_fields)
return models.Model.save(self, *args, **kwargs)
def __str__(self) -> str:
return 'Object of class {} with uuid {}'.format(self.__class__, self.uuid)