def main(data): action = data.get("action", "read") input_data = data.get("data", {}) result = {} if action == "create": widget = Widget() widget.fill_from_json(input_data) widget.save(b64source=input_data.get("b64source", None)) result[widget.guid] = widget.to_json() elif action == "read": for wid_guid in input_data: widget = Widget.get(guid=wid_guid) result[wid_guid] = widget.to_json( include=["b64source"]) if widget else None elif action == "update": for widget_guid in input_data: widget = Widget.get(guid=widget_guid) wid_json = input_data[widget_guid] if widget: widget.fill_from_json(wid_json) widget.save(b64source=input_data.get("b64source", None)) result[widget_guid] = widget.to_json(include=["b64source"]) elif action == "delete": for widget_guid in input_data: widget = Widget.get(guid=widget_guid) if widget: widget.delete() result[widget_guid] = "deleted" else: result[widget_guid] = "not_found" write_response(result)
def widget_test(): print_data('widgets objects', br=False) workspaces = [] for index in range(3): w = Workspace() w.name = 'New workspace name' w.description = 'Some new description' w.save() workspaces.append(w) for index in range(3): w = Widget() w.source = '<source>some stuff</source>' w.workspace_id = workspaces[0].guid if index in [0, 1] else workspaces[1].guid w.save() widgets = Widget.all() print_data('new objects -> model.all()', widgets) w.source = '<b>UDPATED</b>' w.save() w.reload() print_data('UPDATED', [w.source]) widgets = Widget.get(id=w.id) print_data('GET -> model.get()', [widgets]) widgets = Widget.filter(source='<source>some stuff</source>') print_data('FILTER -> model.filter()', widgets) for index, w in enumerate(workspaces): widgets = w.widgets print_data('workspace %s -> workspace.widgets' % str(index), widgets) [w.delete() for w in Widget.all()] objects = Widget.all() + Workspace.all() print_data('cleaned', objects) widgets = Widget.filter(include_deleted=True) print_data('cleaned with deleted if exists', widgets)
from models import Workspace, Widget widget_id = request.arguments.get('widget_id', '') command = request.arguments.get('command', 'update') widget = Widget.get(guid=widget_id) if widget: disabled = '2' if command == 'delete' else '0' form = self.dialog_update.form_update form.title.value = widget.name form.title.mode = disabled form.workspace_id.value = widget.workspace_id form.widget_id.value = widget.guid form.command.value = command self.dialog_update.form_update.btn_update.label = command.title() form.btn_update.action("setLabel", [command.title()]) form.btn_update.action( "setClass", ['btn btn-danger' if command == 'delete' else 'btn btn-success']) self.dialog_update.title = 'Widget {}'.format(command.title()) self.dialog_update.show = '1'
raise Exception('Workspace ID is not provided') workspace_id = request.arguments.get('workspace_id') command = request.arguments.get('command', u'') workspace = Workspace.get(guid=workspace_id) if not workspace: self.action('goTo', ['/main']) elif command in ['delete', 'update']: if 'widget_id' not in request.arguments: raise Exception(u'Widget ID is not provided') widget_id = request.arguments['widget_id'] widget = Widget.get(guid=widget_id, workspace_id=workspace.guid) if widget: if command == 'delete': widget.delete() else: title = request.arguments['title'] if title: widget.name = title widget.save() self.dialog_update.action('hide', ['0']) elif command == 'create': title = request.arguments['title'] if title: