diff --git a/server/src/uds/REST/methods/accountsusage.py b/server/src/uds/REST/methods/accountsusage.py index 981cc5f4..75d0c671 100644 --- a/server/src/uds/REST/methods/accountsusage.py +++ b/server/src/uds/REST/methods/accountsusage.py @@ -53,7 +53,7 @@ class AccountsUsage(DetailHandler): # pylint: disable=too-many-public-methods """ @staticmethod - def usageToDict(item: 'AccountUsage', perm): + def usageToDict(item: 'AccountUsage', perm: int) -> typing.Dict[str, typing.Any]: """ Convert an account usage to a dictionary :param item: Account usage item (db) diff --git a/server/src/uds/REST/methods/authenticators.py b/server/src/uds/REST/methods/authenticators.py index 945578f5..f0b221fc 100644 --- a/server/src/uds/REST/methods/authenticators.py +++ b/server/src/uds/REST/methods/authenticators.py @@ -97,7 +97,7 @@ class Authenticators(ModelHandler): except Exception: raise NotFound('type not found') - def item_as_dict(self, item: Authenticator): + def item_as_dict(self, item: Authenticator) -> typing.Dict[str, typing.Any]: type_ = item.getType() return { 'numeric_id': item.id, diff --git a/server/src/uds/REST/methods/calendarrules.py b/server/src/uds/REST/methods/calendarrules.py index 4b3ed047..4441ef30 100644 --- a/server/src/uds/REST/methods/calendarrules.py +++ b/server/src/uds/REST/methods/calendarrules.py @@ -30,8 +30,9 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import logging import datetime +import logging +import typing from django.utils.translation import ugettext as _ from django.db import IntegrityError @@ -45,6 +46,9 @@ from uds.core.util.model import processUuid from uds.REST.model import DetailHandler from uds.REST import RequestError +# Not imported at runtime, just for type checking +if typing.TYPE_CHECKING: + from uds.models import Calendar logger = logging.getLogger(__name__) @@ -55,7 +59,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods """ @staticmethod - def ruleToDict(item, perm): + def ruleToDict(item: CalendarRule, perm: int): """ Convert a calRule db item to a dict for a rest response :param item: Rule item (db) @@ -76,7 +80,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods return retVal - def getItems(self, parent, item): + def getItems(self, parent: 'Calendar', item: typing.Optional[str]): # Check what kind of access do we have to parent provider perm = permissions.getEffectivePermission(self._user, parent) try: @@ -89,7 +93,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods logger.exception('itemId %s', item) self.invalidItemException() - def getFields(self, parent): + def getFields(self, parent: 'Calendar') -> typing.List[typing.Any]: return [ {'name': {'title': _('Rule name')}}, {'start': {'title': _('Starts'), 'type': 'datetime'}}, @@ -100,7 +104,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods {'comments': {'title': _('Comments')}}, ] - def saveItem(self, parent, item): + def saveItem(self, parent: 'Calendar', item: typing.Optional[str]) -> None: # Extract item db fields # We need this fields for all logger.debug('Saving rule %s / %s', parent, item) @@ -114,7 +118,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods if fields['end'] is not None: fields['end'] = datetime.datetime.fromtimestamp(fields['end']) - calRule = None + calRule: CalendarRule try: if item is None: # Create new calRule = parent.rules.create(**fields) @@ -132,7 +136,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods return self.getItems(parent, calRule.uuid) - def deleteItem(self, parent, item): + def deleteItem(self, parent: 'Calendar', item: str) -> None: logger.debug('Deleting rule %s from %s', item, parent) try: calRule = parent.rules.get(uuid=processUuid(item)) @@ -143,9 +147,7 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods logger.exception('Exception') self.invalidItemException() - return 'deleted' - - def getTitle(self, parent): + def getTitle(self, parent: 'Calendar') -> str: try: return _('Rules of {0}').format(parent.name) except Exception: diff --git a/server/src/uds/REST/methods/calendars.py b/server/src/uds/REST/methods/calendars.py index 510af803..dd82090a 100644 --- a/server/src/uds/REST/methods/calendars.py +++ b/server/src/uds/REST/methods/calendars.py @@ -31,6 +31,7 @@ @itemor: Adolfo Gómez, dkmaster at dkmon dot com """ import logging +import typing from django.utils.translation import ugettext_lazy as _ from uds.models import Calendar @@ -62,7 +63,7 @@ class Calendars(ModelHandler): {'tags': {'title': _('tags'), 'visible': False}}, ] - def item_as_dict(self, item): + def item_as_dict(self, item: Calendar) -> typing.Dict[str, typing.Any]: return { 'id': item.uuid, 'name': item.name, @@ -72,5 +73,5 @@ class Calendars(ModelHandler): 'permission': permissions.getEffectivePermission(self._user, item) } - def getGui(self, type_): + def getGui(self, type_: str) -> typing.List[typing.Any]: return self.addDefaultFields([], ['name', 'comments', 'tags']) diff --git a/server/src/uds/models/calendar.py b/server/src/uds/models/calendar.py index c58cf21d..2cc86cfd 100644 --- a/server/src/uds/models/calendar.py +++ b/server/src/uds/models/calendar.py @@ -30,6 +30,7 @@ .. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com """ import logging +import typing from django.db import models from .uuid_model import UUIDModel @@ -39,12 +40,20 @@ from .tag import TaggingMixin logger = logging.getLogger(__name__) +# Not imported at runtime, just for type checking +if typing.TYPE_CHECKING: + from uds.models import CalendarRule, CalendarAccess, CalendarAction + + class Calendar(UUIDModel, TaggingMixin): name = models.CharField(max_length=128, default='') comments = models.CharField(max_length=256, default='') modified = models.DateTimeField(auto_now=True) + # Sobmodels + rules: CalendarRule + class Meta: """ Meta class to declare db table diff --git a/server/src/uds/models/calendar_access.py b/server/src/uds/models/calendar_access.py index c33b5179..ccbe147f 100644 --- a/server/src/uds/models/calendar_access.py +++ b/server/src/uds/models/calendar_access.py @@ -47,8 +47,8 @@ logger = logging.getLogger(__name__) class CalendarAccess(UUIDModel): - calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE) - service_pool = models.ForeignKey(ServicePool, related_name='calendarAccess', on_delete=models.CASCADE) + calendar: Calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE) + service_pool: ServicePool = models.ForeignKey(ServicePool, related_name='calendarAccess', on_delete=models.CASCADE) access = models.CharField(max_length=8, default=states.action.DENY) priority = models.IntegerField(default=0, db_index=True) diff --git a/server/src/uds/models/calendar_action.py b/server/src/uds/models/calendar_action.py index beb4fc57..dc4114eb 100644 --- a/server/src/uds/models/calendar_action.py +++ b/server/src/uds/models/calendar_action.py @@ -76,8 +76,8 @@ CALENDAR_ACTION_DICT: typing.Dict[str, typing.Dict] = {c['id']: c for c in ( class CalendarAction(UUIDModel): - calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE) - service_pool = models.ForeignKey(ServicePool, on_delete=models.CASCADE) + calendar: Calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE) + service_pool: ServicePool = models.ForeignKey(ServicePool, on_delete=models.CASCADE) action = models.CharField(max_length=64, default='') at_start = models.BooleanField(default=False) # If false, action is done at end of event events_offset = models.IntegerField(default=0) # In minutes diff --git a/server/src/uds/models/calendar_rule.py b/server/src/uds/models/calendar_rule.py index 92ba7722..6894d534 100644 --- a/server/src/uds/models/calendar_rule.py +++ b/server/src/uds/models/calendar_rule.py @@ -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 = models.ForeignKey(Calendar, related_name='rules', on_delete=models.CASCADE) + calendar: Calendar = models.ForeignKey(Calendar, related_name='rules', on_delete=models.CASCADE) class Meta: """