From 8f9d042cdd8b34480eb331340c795bd2e0267dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Tue, 29 Mar 2016 15:42:41 +0200 Subject: [PATCH] Now we can create (but not edit) scheduled actions --- server/src/uds/REST/methods/calendarrules.py | 2 +- .../REST/methods/services_pool_calendars.py | 29 +++++++------ server/src/uds/models/CalendarAction.py | 2 + .../adm/js/gui-d-servicespools-actions.coffee | 29 ++++++++++--- .../uds/admin/tmpl/pool_add_access.html | 41 ++++--------------- 5 files changed, 53 insertions(+), 50 deletions(-) diff --git a/server/src/uds/REST/methods/calendarrules.py b/server/src/uds/REST/methods/calendarrules.py index 53aec822f..9974e8a96 100644 --- a/server/src/uds/REST/methods/calendarrules.py +++ b/server/src/uds/REST/methods/calendarrules.py @@ -147,6 +147,6 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods def getTitle(self, parent): try: - return _('Services of {0}').format(parent.name) + return _('Rules of {0}').format(parent.name) except Exception: return _('Current rules') diff --git a/server/src/uds/REST/methods/services_pool_calendars.py b/server/src/uds/REST/methods/services_pool_calendars.py index 914c34848..db873c652 100644 --- a/server/src/uds/REST/methods/services_pool_calendars.py +++ b/server/src/uds/REST/methods/services_pool_calendars.py @@ -39,6 +39,7 @@ from django.utils.translation import ugettext as _ from uds.models import CalendarAccess, CalendarAction, Calendar +from uds.models.CalendarAction import CALENDAR_ACTION_DICT from uds.core.util.State import State from uds.core.util.model import processUuid from uds.core.util import log @@ -46,8 +47,7 @@ from uds.REST.model import DetailHandler from uds.REST import ResponseError from uds.core.util import permissions - - +import json import logging logger = logging.getLogger(__name__) @@ -93,7 +93,7 @@ class AccessCalendars(DetailHandler): def saveItem(self, parent, item): # If already exists - uuid = processUuid(self._params['id']) if 'id' in self._params else None + uuid = processUuid(item) if item is not None else None calendar = Calendar.objects.get(uuid=processUuid(self._params['calendarId'])) access = self._params['access'].upper() @@ -126,10 +126,10 @@ class ActionsCalendars(DetailHandler): 'id': item.uuid, 'calendarId': item.calendar.uuid, 'name': item.calendar.name, - 'action': item.action, + 'action': CALENDAR_ACTION_DICT[item.action]['description'], 'atStart': item.atStart, - 'offset': item.eventOffset, - 'params': item.params + 'offset': item.eventsOffset, + 'params': json.loads(item.params) } def getItems(self, parent, item): @@ -150,18 +150,22 @@ class ActionsCalendars(DetailHandler): return [ {'name': {'title': _('Name')}}, {'action': {'title': _('Action')}}, - {'atStart': {'title': _('Referer')}}, + {'params': {'title': _('Parameters')}}, + {'atStart': {'title': _('Relative to')}}, {'offset': {'title': _('Time offset')}}, ] def saveItem(self, parent, item): # If already exists - uuid = processUuid(self._params['id']) if 'id' in self._params else None + uuid = processUuid(item) if item is not None else None calendar = Calendar.objects.get(uuid=processUuid(self._params['calendarId'])) action = self._params['action'].upper() - eventOffset = int(self._params['eventOffset']) - atStart = (self._params['atStart'] == 'true') + eventsOffset = int(self._params['eventsOffset']) + atStart = self._params['atStart'] not in ('false', False, '0', 0) + params = json.dumps(self._params['params']) + + logger.debug('Got parameters: {} {} {} {} ----> {}'.format(calendar, action, eventsOffset, atStart, params)) if uuid is not None: calAction = CalendarAction.objects.get(uuid=uuid) @@ -169,10 +173,11 @@ class ActionsCalendars(DetailHandler): calAction.servicePool = parent calAction.action = action calAction.atStart = atStart - calAction.eventOffset = eventOffset + calAction.eventsOffset = eventsOffset + calAction.params = params calAction.save() else: - CalendarAction.objects.create(calendar=calendar, servicePool=parent, action=action, atStart=atStart, eventOffset=eventOffset) + CalendarAction.objects.create(calendar=calendar, servicePool=parent, action=action, atStart=atStart, eventsOffset=eventsOffset, params=params) return self.success() diff --git a/server/src/uds/models/CalendarAction.py b/server/src/uds/models/CalendarAction.py index 3508502d9..e4d215e69 100644 --- a/server/src/uds/models/CalendarAction.py +++ b/server/src/uds/models/CalendarAction.py @@ -57,6 +57,8 @@ CALENDAR_ACTION_CACHE_L2 = { 'id': 'CACHEL2', 'description': _('Sets L2 cache si CALENDAR_ACTION_INITIAL = { 'id': 'INITIAL', 'description': _('Set initial services'), 'params': ({'type': 'numeric', 'name': 'size', 'description': _('Initial services') },) } CALENDAR_ACTION_MAX = { 'id': 'MAX', 'description': _('Change maximum number of services'), 'params': ({'type': 'numeric', 'name': 'size', 'description': _('Maximum services') },) } +CALENDAR_ACTION_DICT = dict(list((c['id'], c) for c in (CALENDAR_ACTION_PUBLISH, CALENDAR_ACTION_CACHE_L1, + CALENDAR_ACTION_CACHE_L2, CALENDAR_ACTION_INITIAL, CALENDAR_ACTION_MAX))) class CalendarAction(UUIDModel): calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE) servicePool = models.ForeignKey(ServicePool, on_delete=models.CASCADE) diff --git a/server/src/uds/static/adm/js/gui-d-servicespools-actions.coffee b/server/src/uds/static/adm/js/gui-d-servicespools-actions.coffee index 58d8d6ce8..72ac295b7 100644 --- a/server/src/uds/static/adm/js/gui-d-servicespools-actions.coffee +++ b/server/src/uds/static/adm/js/gui-d-servicespools-actions.coffee @@ -21,8 +21,18 @@ gui.servicesPools.actionsCalendars = (servPool, info) -> return true + onData: (data) -> + $.each data, (index, value) -> + value.params = ( k + "=" + value.params[k] for k in Object.keys(value.params)).toString() + value.atStart = if value.atStart then gettext('Beginning') else gettext('Ending') + onNew: (value, table, refreshFnc) -> - api.templates.get "pool_add_access", (tmpl) -> + readParamsFromInputs = (modalId) -> + a = {} + a[$(v).attr('name')] = $(v).val() for v in $(modalId + ' .action_parameters') + return a + + api.templates.get "pool_add_action", (tmpl) -> api.calendars.overview (data) -> api.servicesPools.actionsList servPool.id, (actionsList) -> modalId = gui.launchModal(gettext("Add scheduled action"), api.templates.evaluate(tmpl, @@ -31,17 +41,22 @@ gui.servicesPools.actionsCalendars = (servPool, info) -> calendarId: '' actionsList: actionsList action: '' - eventOffset: 0 + eventsOffset: 0 atStart: true )) $(modalId + " .button-accept").on "click", (event) -> - priority = $(modalId + " #id_priority").val() + offset = $(modalId + " #id_offset").val() calendar = $(modalId + " #id_calendar_select").val() action = $(modalId + " #id_action_select").val() + atStart = $(modalId + " #atStart_field").is(":checked") actionsCalendars.rest.create calendarId: calendar action: action - priority: priority + eventsOffset: offset + atStart: atStart + action: action + params: readParamsFromInputs(modalId) + , (data) -> $(modalId).modal "hide" refreshFnc() @@ -62,7 +77,11 @@ gui.servicesPools.actionsCalendars = (servPool, info) -> defval = '1' else defval = '' - html += '
' + html += '
' $(modalId + " #parameters").html(html) gui.tools.applyCustoms modalId return diff --git a/server/src/uds/templates/uds/admin/tmpl/pool_add_access.html b/server/src/uds/templates/uds/admin/tmpl/pool_add_access.html index 2adc0abf1..4d68fad66 100644 --- a/server/src/uds/templates/uds/admin/tmpl/pool_add_access.html +++ b/server/src/uds/templates/uds/admin/tmpl/pool_add_access.html @@ -2,15 +2,15 @@ {% verbatim %}
- -
+ +
- -
+ +
-
-
- -
- -
- -
-
- -
- -
- + {{# each accessList }} + {{/ each }}
- -
-
- {% endverbatim %}