mendfixed REST metapools for python 3.x

This commit is contained in:
Adolfo Gómez García 2019-09-11 12:51:54 +02:00
parent 6eccb757ad
commit 4cd051f50a
3 changed files with 22 additions and 28 deletions

View File

@ -31,6 +31,7 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import logging
import typing
from django.utils.translation import ugettext, ugettext_lazy as _
from uds.models import MetaPool, Image, ServicePoolGroup
@ -79,9 +80,7 @@ class MetaPools(ModelHandler):
custom_methods = [('setFallbackAccess', True), ('getFallbackAccess', True)]
def item_as_dict(self, item: MetaPool):
if not self._user:
return {}
def item_as_dict(self, item: MetaPool) -> typing.Dict[str, typing.Any]:
# if item does not have an associated service, hide it (the case, for example, for a removed service)
# Access from dict will raise an exception, and item will be skipped
poolGroupId = None
@ -119,11 +118,10 @@ class MetaPools(ModelHandler):
return val
# Gui related
def getGui(self, type_):
def getGui(self, type_: str) -> typing.List[typing.Any]:
localGUI = self.addDefaultFields([], ['name', 'short_name', 'comments', 'tags'])
g = self.addDefaultFields([], ['name', 'short_name', 'comments', 'tags'])
for f in [{
for field in [{
'name': 'policy',
'values': [gui.choiceItem(k, str(v)) for k, v in MetaPool.TYPES.items()],
'label': ugettext('Policy'),
@ -155,11 +153,11 @@ class MetaPools(ModelHandler):
'order': 123,
'tab': ugettext('Display'),
}]:
self.addField(g, f)
self.addField(localGUI, field)
return g
return localGUI
def beforeSave(self, fields):
def beforeSave(self, fields: typing.Any) -> None:
# logger.debug(self._params)
try:
# **** IMAGE ***
@ -191,18 +189,11 @@ class MetaPools(ModelHandler):
logger.debug('Fields: %s', fields)
def deleteItem(self, item):
def deleteItem(self, item: MetaPool) -> None:
item.delete()
# Logs
def getLogs(self, item):
try:
return log.getLogs(item)
except Exception:
return []
# Set fallback status
def setFallbackAccess(self, item):
def setFallbackAccess(self, item: MetaPool):
self.ensureAccess(item, permissions.PERMISSION_MANAGEMENT)
fallback = self._params.get('fallbackAccess')
@ -211,10 +202,10 @@ class MetaPools(ModelHandler):
item.save()
return ''
def getFallbackAccess(self, item):
def getFallbackAccess(self, item: MetaPool):
return item.fallbackAccess
# Returns the action list based on current element, for calendar
def actionsList(self, item):
# Returns the action list based on current element, for calendars (nothing right now for metapools)
def actionsList(self, item: MetaPool):
validActions = ()
return validActions

View File

@ -599,7 +599,7 @@ class ModelHandler(BaseModelHandler):
# By default, filter is empty
fltr: typing.Optional[str] = None
# This is an array of tuples of two items, where first is method and second inticates if method needs parent id
# This is an array of tuples of two items, where first is method and second inticates if method needs parent id (normal behavior is it needs it)
# For example ('services', True) -- > .../id_parent/services
# ('services', False) --> ..../services
custom_methods: typing.ClassVar[typing.Iterable[typing.Tuple[str, bool]]] = [] # If this model respond to "custom" methods, we will declare them here
@ -658,10 +658,13 @@ class ModelHandler(BaseModelHandler):
return found
# log related
def getLogs(self, item: models.Model):
def getLogs(self, item: models.Model) -> typing.List[typing.Dict]:
self.ensureAccess(item, permissions.PERMISSION_READ)
logger.debug('Default getLogs invoked')
try:
return log.getLogs(item)
except Exception as e:
logger.warning('Exception getting logs for %s: %s', item, e)
return []
# gui related
def getGui(self, type_: str) -> typing.List[typing.Any]:

View File

@ -119,7 +119,7 @@ class gui:
return []
@staticmethod
def choiceItem(id_: str, text: str) -> 'gui.ChoiceType':
def choiceItem(id_: typing.Union[str, int], text: str) -> 'gui.ChoiceType':
"""
Helper method to create a single choice item.
@ -138,7 +138,7 @@ class gui:
return {'id': str(id_), 'text': str(text)}
@staticmethod
def choiceImage(id_: str, text: str, img: str) -> typing.Dict[str, str]:
def choiceImage(id_: typing.Union[str, int], text: str, img: bytes) -> typing.Dict[str, typing.Union[str, bytes]]:
return {'id': str(id_), 'text': str(text), 'img': img}
@staticmethod