updating type checking

This commit is contained in:
Adolfo Gómez García 2020-11-11 14:36:31 +01:00
parent 1ab534c3aa
commit 65b47686db
9 changed files with 25 additions and 28 deletions

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2016-2019 Virtual Cable S.L.
# Copyright (c) 2016-2020 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -52,7 +52,9 @@ class Calendar(UUIDModel, TaggingMixin):
modified = models.DateTimeField(auto_now=True)
# Sobmodels
rules: 'CalendarRule'
# "fake" relations declarations for type checking
rules: 'models.QuerySet[CalendarRule]'
calendaraction_set: 'models.QuerySet[CalendarAction]'
class Meta:
"""

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2019 Virtual Cable S.L.
# Copyright (c) 2016-2020 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -99,7 +99,7 @@ class CalendarRule(UUIDModel):
duration = models.IntegerField(default=0) # Duration in minutes
duration_unit = models.CharField(choices=dunits, default='MINUTES', max_length=32)
calendar: Calendar = models.ForeignKey(Calendar, related_name='rules', on_delete=models.CASCADE)
calendar: 'models.ForeignKey[CalendarRule, Calendar]' = models.ForeignKey(Calendar, related_name='rules', on_delete=models.CASCADE)
class Meta:
"""

View File

@ -58,5 +58,5 @@ class Config(models.Model):
unique_together = (('section', 'key'),)
app_label = 'uds'
def __str__(self):
def __str__(self) -> str:
return "Config {} = {}".format(self.key, self.value)

View File

@ -39,8 +39,6 @@ from uds.core.util import encoders
from .uuid_model import UUIDModel
logger = logging.getLogger(__name__)
class DBFile(UUIDModel):
@ -65,7 +63,7 @@ class DBFile(UUIDModel):
return b''
@data.setter
def data(self, value: typing.Union[str, bytes]):
def data(self, value: bytes):
self.size = len(value)
self.content = typing.cast(str, encoders.encode(encoders.encode(value, 'zip'), 'base64', asText=True))

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019 Virtual Cable S.L.
# Copyright (c) 2012-2020 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -61,5 +61,5 @@ class DelayedTask(models.Model):
"""
app_label = 'uds'
def __str__(self):
def __str__(self) -> str:
return "Run Queue task {0} owned by {3},inserted at {1} and with {2} seconds delay".format(self.type, self.insert_date, self.execution_delay, self.execution_time)

View File

@ -34,7 +34,6 @@ import logging
import typing
from django.db import models
from django.db.models import signals
from uds.core.util.state import State
from uds.core.util import log
@ -86,7 +85,7 @@ class Group(UUIDModel):
"""
return self.manager.getInstance()
def __str__(self):
def __str__(self) -> str:
if self.is_meta:
return "Meta group {}(id:{}) with groups {}".format(self.name, self.id, list(self.groups.all()))
@ -114,4 +113,4 @@ class Group(UUIDModel):
logger.debug('Deleted group %s', toDelete)
signals.pre_delete.connect(Group.beforeDelete, sender=Group)
models.signals.pre_delete.connect(Group.beforeDelete, sender=Group)

View File

@ -33,10 +33,9 @@ import logging
import typing
from PIL import Image as PILImage
import PIL.Image
from django.db import models
from django.db.models import signals
from django.http import HttpResponse
from uds.core.util import encoders
@ -46,7 +45,6 @@ from .uuid_model import UUIDModel
from .util import getSqlDatetime
logger = logging.getLogger(__name__)
@ -85,12 +83,12 @@ class Image(UUIDModel):
def prepareForDb(data: bytes) -> bytes:
try:
stream = io.BytesIO(data)
image = PILImage.open(stream)
image = PIL.Image.open(stream)
except Exception: # Image data is incorrect, fix as a simple transparent image
image = PILImage.new('RGBA', (128, 128))
image = PIL.Image.new('RGBA', (128, 128))
# Max image size, keeping aspect and using antialias
image.thumbnail(Image.MAX_IMAGE_SIZE, PILImage.ANTIALIAS)
image.thumbnail(Image.MAX_IMAGE_SIZE, PIL.Image.ANTIALIAS)
output = io.BytesIO()
image.save(output, 'png')
return output.getvalue()
@ -103,7 +101,7 @@ class Image(UUIDModel):
return Image.encode64(self.data)
@data64.setter
def data64(self, value: str):
def data64(self, value: str) -> None:
"""
Sets the value of image (data) from a base 64 encoded string
"""
@ -117,22 +115,22 @@ class Image(UUIDModel):
return Image.encode64(self.thumb)
@thumb64.setter
def thumb64(self, value: str):
def thumb64(self, value: str) -> None:
"""
Sets the value of image (data) from a base 64 encoded string
"""
self.thumb = Image.decode64(value)
@property
def image(self) -> PILImage:
def image(self) -> PIL.Image.Image:
"""
Returns an image (PIL Image)
"""
try:
data = io.BytesIO(self.data)
return PILImage.open(data)
return PIL.Image.open(data)
except Exception: # Image data is incorrect, fix as a simple transparent image
return PILImage.new('RGBA', Image.MAX_IMAGE_SIZE)
return PIL.Image.new('RGBA', Image.MAX_IMAGE_SIZE)
@property
def size(self) -> typing.Tuple[int, int]:
@ -144,7 +142,7 @@ class Image(UUIDModel):
def updateThumbnail(self) -> None:
thumb = self.image
self.width, self.height = thumb.size
thumb.thumbnail(Image.THUMBNAIL_SIZE, PILImage.ANTIALIAS)
thumb.thumbnail(Image.THUMBNAIL_SIZE, PIL.Image.ANTIALIAS)
output = io.BytesIO()
thumb.save(output, 'png')
self.thumb = output.getvalue()
@ -191,4 +189,4 @@ class Image(UUIDModel):
logger.debug('Deleted image %s', toDelete)
signals.pre_delete.connect(Image.beforeDelete, sender=Image)
models.signals.pre_delete.connect(Image.beforeDelete, sender=Image)

View File

@ -61,5 +61,5 @@ class Log(models.Model):
db_table = 'uds_log'
app_label = 'uds'
def __str__(self):
def __str__(self) -> str:
return "Log of {}({}): {} - {} - {} - {}".format(self.owner_type, self.owner_id, self.created, self.source, self.level, self.data)

View File

@ -58,7 +58,7 @@ class ManagedObjectModel(UUIDModel):
class Meta(UUIDModel.Meta):
"""
Defines this is an abstract clas
Defines this is an abstract class
"""
abstract = True