fixed REST calendars for python 3.x

This commit is contained in:
Adolfo Gómez García 2019-09-11 11:27:12 +02:00
parent 060e2cd023
commit da0df6d407
8 changed files with 31 additions and 19 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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:

View File

@ -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'])

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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:
"""