1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-22 22:03:54 +03:00

Added support to call custom methods as camelCase of snake_case (keeping backwards compat, but allowin new snake_case standard to be used)

This commit is contained in:
Adolfo Gómez García 2024-03-14 23:50:35 +01:00
parent df5d18710a
commit cf8660aa78
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
2 changed files with 41 additions and 41 deletions

View File

@ -117,15 +117,16 @@ class ServicesPools(ModelHandler):
table_row_style = types.ui.RowStyleInfo(prefix='row-state-', field='state')
custom_methods = [
('setFallbackAccess', True),
('getFallbackAccess', True),
('actionsList', True),
('listAssignables', True),
('set_fallback_access', True),
('get_fallback_access', True),
('actions_list', True),
('list_assignables', True),
('createFromAssignable', True),
('create_from_assignable', True),
]
def get_items(self, *args: typing.Any, **kwargs: typing.Any) -> typing.Generator[types.rest.ItemDictType, None, None]:
def get_items(
self, *args: typing.Any, **kwargs: typing.Any
) -> typing.Generator[types.rest.ItemDictType, None, None]:
# Optimized query, due that there is a lot of info needed for theee
d = sql_datetime() - datetime.timedelta(seconds=GlobalConfig.RESTRAINT_TIME.as_int())
return super().get_items(
@ -227,7 +228,7 @@ class ServicesPools(ModelHandler):
# Extended info
if not summary:
if hasattr(item, 'valid_count'):
valid_count = getattr(item, 'valid_count')
valid_count = getattr(item, 'valid_count')
preparing_count = getattr(item, 'preparing_count')
restrained = getattr(item, 'error_count') >= GlobalConfig.RESTRAINT_COUNT.as_int()
usage_count = getattr(item, 'usage_count')
@ -289,10 +290,7 @@ class ServicesPools(ModelHandler):
'name': 'service_id',
'choices': [gui.choice_item('', '')]
+ gui.sorted_choices(
[
gui.choice_item(v.uuid, v.provider.name + '\\' + v.name)
for v in Service.objects.all()
]
[gui.choice_item(v.uuid, v.provider.name + '\\' + v.name) for v in Service.objects.all()]
),
'label': gettext('Base service'),
'tooltip': gettext('Service used as base of this service pool'),
@ -303,9 +301,7 @@ class ServicesPools(ModelHandler):
{
'name': 'osmanager_id',
'choices': [gui.choice_item(-1, '')]
+ gui.sorted_choices(
[gui.choice_item(v.uuid, v.name) for v in OSManager.objects.all()]
),
+ gui.sorted_choices([gui.choice_item(v.uuid, v.name) for v in OSManager.objects.all()]),
'label': gettext('OS Manager'),
'tooltip': gettext('OS Manager used as base of this service pool'),
'type': types.ui.FieldType.CHOICE,
@ -368,10 +364,7 @@ class ServicesPools(ModelHandler):
'name': 'pool_group_id',
'choices': [gui.choice_image(-1, _('Default'), DEFAULT_THUMB_BASE64)]
+ gui.sorted_choices(
[
gui.choice_image(v.uuid, v.name, v.thumb64)
for v in ServicePoolGroup.objects.all()
]
[gui.choice_image(v.uuid, v.name, v.thumb64) for v in ServicePoolGroup.objects.all()]
),
'label': gettext('Pool group'),
'tooltip': gettext('Pool group for this pool (for pool classify on display)'),
@ -444,9 +437,7 @@ class ServicesPools(ModelHandler):
{
'name': 'account_id',
'choices': [gui.choice_item(-1, '')]
+ gui.sorted_choices(
[gui.choice_item(v.uuid, v.name) for v in Account.objects.all()]
),
+ gui.sorted_choices([gui.choice_item(v.uuid, v.name) for v in Account.objects.all()]),
'label': gettext('Accounting'),
'tooltip': gettext('Account associated to this service pool'),
'type': types.ui.FieldType.CHOICE,
@ -597,7 +588,7 @@ class ServicesPools(ModelHandler):
return []
# Set fallback status
def setFallbackAccess(self, item: 'Model') -> typing.Any:
def set_fallback_access(self, item: 'Model') -> typing.Any:
item = ensure.is_instance(item, ServicePool)
self.ensure_has_access(item, types.permissions.PermissionType.MANAGEMENT)
@ -608,12 +599,12 @@ class ServicesPools(ModelHandler):
item.save()
return item.fallbackAccess
def getFallbackAccess(self, item: 'Model') -> typing.Any:
def get_fallback_access(self, item: 'Model') -> typing.Any:
item = ensure.is_instance(item, ServicePool)
return item.fallbackAccess
# Returns the action list based on current element, for calendar
def actionsList(self, item: 'Model') -> list[types.calendar.CalendarAction]:
def actions_list(self, item: 'Model') -> list[types.calendar.CalendarAction]:
item = ensure.is_instance(item, ServicePool)
valid_actions: list[types.calendar.CalendarAction] = []
itemInfo = item.service.get_type()
@ -624,10 +615,14 @@ class ServicesPools(ModelHandler):
consts.calendar.CALENDAR_ACTION_MAX,
]
if itemInfo.uses_cache_l2 is True:
valid_actions += [consts.calendar.CALENDAR_ACTION_CACHE_L2,]
valid_actions += [
consts.calendar.CALENDAR_ACTION_CACHE_L2,
]
if itemInfo.publication_type is not None:
valid_actions += [consts.calendar.CALENDAR_ACTION_PUBLISH,]
valid_actions += [
consts.calendar.CALENDAR_ACTION_PUBLISH,
]
# Transport & groups actions
valid_actions += [
@ -648,15 +643,12 @@ class ServicesPools(ModelHandler):
return valid_actions
# Deprecated, use list_assignables
def listAssignables(self, item: 'Model') -> typing.Any:
def list_assignables(self, item: 'Model') -> typing.Any:
item = ensure.is_instance(item, ServicePool)
service = item.service.get_instance()
return list(service.enumerate_assignables())
def list_assignables(self, item: 'Model') -> typing.Any:
return self.listAssignables(item)
def createFromAssignable(self, item: 'Model') -> typing.Any:
def create_from_assignable(self, item: 'Model') -> typing.Any:
item = ensure.is_instance(item, ServicePool)
if 'user_id' not in self._params or 'assignable_id' not in self._params:
return self.invalid_request_response('Invalid parameters')

View File

@ -351,12 +351,22 @@ class ModelHandler(BaseModelHandler):
# if has custom methods, look for if this request matches any of them
for cm in self.custom_methods:
# Convert to snake case
snake_case_name = re.sub(r'(?<!^)(?=[A-Z])', '_', cm[0]).lower()
# And snake case to camel case (first letter lower case, rest upper case)
camel_case_name = ''.join(x.capitalize() for x in snake_case_name.split('_'))
camel_case_name = camel_case_name[0].lower() + camel_case_name[1:]
if nArgs > 1 and cm[1] is True: # Method needs parent (existing item)
if self._args[1] == cm[0]:
item = operation = None
if self._args[1] in (camel_case_name, snake_case_name):
item = None
# Check if operation method exists
operation = getattr(self, snake_case_name) or getattr(self, camel_case_name)
try:
operation = getattr(self, self._args[1])
item = self.model.objects.get(uuid__iexact=self._args[0].lower())
if not operation:
raise Exception() # Operation not found
item = self.model.objects.get(uuid__iexact=self._args[0])
except self.model.DoesNotExist:
raise self.invalid_item_response()
except Exception as e:
logger.error(
'Invalid custom method exception %s/%s/%s: %s',
@ -369,12 +379,10 @@ class ModelHandler(BaseModelHandler):
return operation(item)
elif self._args[0] == cm[0]:
operation = None
try:
operation = getattr(self, self._args[0])
except Exception as e:
raise self.invalid_method_response() from e
elif self._args[0] in (snake_case_name, snake_case_name):
operation = getattr(self, snake_case_name) or getattr(self, snake_case_name)
if not operation:
raise self.invalid_method_response()
return operation()