1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-11 05:17:55 +03:00

Now we can create (but not edit) scheduled actions

This commit is contained in:
Adolfo Gómez García 2016-03-29 15:42:41 +02:00
parent fcf030e693
commit 8f9d042cdd
5 changed files with 53 additions and 50 deletions

View File

@ -147,6 +147,6 @@ class CalendarRules(DetailHandler): # pylint: disable=too-many-public-methods
def getTitle(self, parent): def getTitle(self, parent):
try: try:
return _('Services of {0}').format(parent.name) return _('Rules of {0}').format(parent.name)
except Exception: except Exception:
return _('Current rules') return _('Current rules')

View File

@ -39,6 +39,7 @@ from django.utils.translation import ugettext as _
from uds.models import CalendarAccess, CalendarAction, Calendar 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.State import State
from uds.core.util.model import processUuid from uds.core.util.model import processUuid
from uds.core.util import log from uds.core.util import log
@ -46,8 +47,7 @@ from uds.REST.model import DetailHandler
from uds.REST import ResponseError from uds.REST import ResponseError
from uds.core.util import permissions from uds.core.util import permissions
import json
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -93,7 +93,7 @@ class AccessCalendars(DetailHandler):
def saveItem(self, parent, item): def saveItem(self, parent, item):
# If already exists # 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'])) calendar = Calendar.objects.get(uuid=processUuid(self._params['calendarId']))
access = self._params['access'].upper() access = self._params['access'].upper()
@ -126,10 +126,10 @@ class ActionsCalendars(DetailHandler):
'id': item.uuid, 'id': item.uuid,
'calendarId': item.calendar.uuid, 'calendarId': item.calendar.uuid,
'name': item.calendar.name, 'name': item.calendar.name,
'action': item.action, 'action': CALENDAR_ACTION_DICT[item.action]['description'],
'atStart': item.atStart, 'atStart': item.atStart,
'offset': item.eventOffset, 'offset': item.eventsOffset,
'params': item.params 'params': json.loads(item.params)
} }
def getItems(self, parent, item): def getItems(self, parent, item):
@ -150,18 +150,22 @@ class ActionsCalendars(DetailHandler):
return [ return [
{'name': {'title': _('Name')}}, {'name': {'title': _('Name')}},
{'action': {'title': _('Action')}}, {'action': {'title': _('Action')}},
{'atStart': {'title': _('Referer')}}, {'params': {'title': _('Parameters')}},
{'atStart': {'title': _('Relative to')}},
{'offset': {'title': _('Time offset')}}, {'offset': {'title': _('Time offset')}},
] ]
def saveItem(self, parent, item): def saveItem(self, parent, item):
# If already exists # 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'])) calendar = Calendar.objects.get(uuid=processUuid(self._params['calendarId']))
action = self._params['action'].upper() action = self._params['action'].upper()
eventOffset = int(self._params['eventOffset']) eventsOffset = int(self._params['eventsOffset'])
atStart = (self._params['atStart'] == 'true') 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: if uuid is not None:
calAction = CalendarAction.objects.get(uuid=uuid) calAction = CalendarAction.objects.get(uuid=uuid)
@ -169,10 +173,11 @@ class ActionsCalendars(DetailHandler):
calAction.servicePool = parent calAction.servicePool = parent
calAction.action = action calAction.action = action
calAction.atStart = atStart calAction.atStart = atStart
calAction.eventOffset = eventOffset calAction.eventsOffset = eventsOffset
calAction.params = params
calAction.save() calAction.save()
else: 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() return self.success()

View File

@ -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_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_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): class CalendarAction(UUIDModel):
calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE) calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE)
servicePool = models.ForeignKey(ServicePool, on_delete=models.CASCADE) servicePool = models.ForeignKey(ServicePool, on_delete=models.CASCADE)

View File

@ -21,8 +21,18 @@ gui.servicesPools.actionsCalendars = (servPool, info) ->
return true 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) -> 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.calendars.overview (data) ->
api.servicesPools.actionsList servPool.id, (actionsList) -> api.servicesPools.actionsList servPool.id, (actionsList) ->
modalId = gui.launchModal(gettext("Add scheduled action"), api.templates.evaluate(tmpl, modalId = gui.launchModal(gettext("Add scheduled action"), api.templates.evaluate(tmpl,
@ -31,17 +41,22 @@ gui.servicesPools.actionsCalendars = (servPool, info) ->
calendarId: '' calendarId: ''
actionsList: actionsList actionsList: actionsList
action: '' action: ''
eventOffset: 0 eventsOffset: 0
atStart: true atStart: true
)) ))
$(modalId + " .button-accept").on "click", (event) -> $(modalId + " .button-accept").on "click", (event) ->
priority = $(modalId + " #id_priority").val() offset = $(modalId + " #id_offset").val()
calendar = $(modalId + " #id_calendar_select").val() calendar = $(modalId + " #id_calendar_select").val()
action = $(modalId + " #id_action_select").val() action = $(modalId + " #id_action_select").val()
atStart = $(modalId + " #atStart_field").is(":checked")
actionsCalendars.rest.create actionsCalendars.rest.create
calendarId: calendar calendarId: calendar
action: action action: action
priority: priority eventsOffset: offset
atStart: atStart
action: action
params: readParamsFromInputs(modalId)
, (data) -> , (data) ->
$(modalId).modal "hide" $(modalId).modal "hide"
refreshFnc() refreshFnc()
@ -62,7 +77,11 @@ gui.servicesPools.actionsCalendars = (servPool, info) ->
defval = '1' defval = '1'
else else
defval = '' defval = ''
html += '<div class="form-group"><label for="fld_' + j['name'] + '" class="col-sm-3 control-label">' + j['description'] + '</label><div class="col-sm-9"><input type="' + j['type'] + '" class="modal_field_data" id="fld_' + j['name'] + '" value="' + defval + '"></div></div>' html += '<div class="form-group"><label for="fld_' + j['name'] +
'" class="col-sm-3 control-label">' + j['description'] +
'</label><div class="col-sm-9"><input type="' + j['type'] +
'" class="action_parameters" name="' + j['name'] +
'" value="' + defval + '"></div></div>'
$(modalId + " #parameters").html(html) $(modalId + " #parameters").html(html)
gui.tools.applyCustoms modalId gui.tools.applyCustoms modalId
return return

View File

@ -2,15 +2,15 @@
{% verbatim %} {% verbatim %}
<form class="form-horizontal" role="form"> <form class="form-horizontal" role="form">
<div class="form-group"> <div class="form-group">
<label for="id_priority" class="col-sm-3 control-label">{% endverbatim %}{% trans 'Priority' %}{% verbatim %}</label> <label for="id_priority" class="col-sm-2 control-label">{% endverbatim %}{% trans 'Priority' %}{% verbatim %}</label>
<div class="col-sm-9"> <div class="col-sm-10">
<input type="numeric" class="modal_field_data" id="id_priority" value="{{ priority }}"> <input type="numeric" class="modal_field_data" id="id_priority" value="{{ priority }}">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="id_calendar_select" class="col-sm-3 control-label">{% endverbatim %}{% trans 'Calendar' %}{% verbatim %}</label> <label for="id_calendar_select" class="col-sm-2 control-label">{% endverbatim %}{% trans 'Calendar' %}{% verbatim %}</label>
<div class="col-sm-9"> <div class="col-sm-10">
<select id="id_calendar_select" class="selectpicker show-menu-arrow show-tick modal_field_data" data-style="btn-default" data-width="100%"> <select id="id_calendar_select" class="selectpicker show-menu-arrow show-tick modal_field_data" data-style="btn-default" data-width="100%">
<option value="-1"></option> <option value="-1"></option>
{{# each calendars }} {{# each calendars }}
@ -21,37 +21,14 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="id_offset" class="col-sm-3 control-label">{% endverbatim %}{% trans 'Time Offset (min.)' %}{% verbatim %}</label> <label for="id_action_select" class="col-sm-2 control-label">{% endverbatim %}{% trans 'Access action' %}{% verbatim %}</label>
<div class="col-sm-9"> <div class="col-sm-10">
<input type="numeric" class="modal_field_data" id="id_offset" value="{{ eventOffset }}"> <select id="id_access_select" class="selectpicker show-menu-arrow show-tick modal_field_data" data-style="btn-default" data-width="100%">
</div> {{# each accessList }}
</div> <option value="{{ this }}"{{# ifequals this ../access }} selected{{/ ifequals }}>{{ this }}</option>
<div class="form-group">
<label for="atStart_field" class="col-sm-3 control-label">{% endverbatim %}{% trans 'At interval beginning?' %}{% verbatim %}</label>
<div class="col-sm-9">
<input type="checkbox"
data-on-text="{% endverbatim %}{% trans 'Yes' %}{% verbatim %}"
data-off-text="{% endverbatim %}{% trans 'No' %}{% verbatim %}"
class="modal_field_data"
id="atStart_field"{{# ifequals atStart true }} checked{{/ ifequals }}>
</div>
</div>
<div class="form-group">
<label for="id_action_select" class="col-sm-3 control-label">{% endverbatim %}{% trans 'Action' %}{% verbatim %}</label>
<div class="col-sm-9">
<select id="id_action_select" class="selectpicker show-menu-arrow show-tick modal_field_data" data-style="btn-default" data-width="100%">
<option value="-1"></option>
{{# each actionsList }}
<option value="{{ id }}"{{# ifequals id ../action }} selected{{/ ifequals }}>{{ description }}</option>
{{/ each }} {{/ each }}
</select> </select>
</div> </div>
</div> </div>
<div id="parameters">
</div>
</form> </form>
{% endverbatim %} {% endverbatim %}