def handle(self, action, user, workspace, actionManager): logManager.info("Execute action '{}' on view '{}'", action['viewAction'], action['view']) viewname = action['view'] if viewname in workspace.dataViews: print('found view', viewname, 'in', workspace.name, workspace.dataViews[viewname]) view = workspace.dataViews[viewname] # check if login required for this view if view.requireLogin is True and user is None: raise RequireLoginError else: # build actions to get view response_actions = [] response_data = None try: view.dataSyncs = [] dictionary = action response_data = view.executeViewActionHandler( user, workspace, ObjDict(dictionary)) notification_action = webclientActions.NotificationAction.generate( "Action '" + str(action['viewAction']) + "' executed", "info") response_actions.append(notification_action) self.db.session.commit() for v in view.dataSyncs: updateView = workspace.dataViews[v['view']] meta_data = updateView.getViewMetaHandler( user, workspace) entries = updateView.getViewHandler( user, workspace, None) properties = updateView.getProperties() uri = workspace.uri + '/' + updateView.uri loadviewaction = webclientActions.LoadViewAction.generate( uri, properties, entries, meta_data) response_actions.append(loadviewaction) except Exception as e: notification_action = webclientActions.NotificationAction.generate( "Action '" + str(action['viewAction']) + "' failed with: ", "error") response_actions = [notification_action] logManager.error( str(type(e).__name__) + 'in ExecuteViewActionsActionHandler', action['view']) traceback.print_exc(file=sys.stdout) # entries = view.getViewHandler(user, workspace) # properties = view.getProperties() # uri = view.uri # loadviewaction = webclientActions.LoadViewAction.generate(uri, properties, entries) if response_data is not None: return 'success', response_actions, response_data else: return 'success', response_actions # view not found notification_action = webclientActions.NotificationAction.generate( "View >" + viewname + "< not found", "error") return 'success', [notification_action]
def handle(self, action, user, workspace, actionManager): logManager.info("Execute update of view entry for '{}'", action['view']) viewname = action['view'] if viewname in workspace.dataViews: view = workspace.dataViews[viewname] # check if login required for this view if view.requireLogin is True and user is None: raise RequireLoginError else: # build actions to get view responseActions = [] try: if view.entrykey not in action['entry']: notification_action = webclientActions.NotificationAction.generate( "UpdateViewEntryActionHandler miss entrykey", "error") responseActions = [notification_action] else: view.dataSyncs = [] dictionary = action['entry'] view.updateViewEntryHandler( user, workspace, action['entry'][str(view.entrykey)], ObjDict(dictionary)) self.db.session.commit() for v in view.dataSyncs: updateView = workspace.dataViews[v['view']] entries = updateView.getViewHandler( user, workspace, None) meta_data = updateView.getViewMetaHandler( user, workspace) properties = updateView.getProperties() uri = workspace.uri + '/' + updateView.uri loadviewaction = webclientActions.LoadViewAction.generate( uri, properties, entries, meta_data) responseActions.append(loadviewaction) responseActions.append( webclientActions.NotificationAction.generate( "Updated successfully", "success")) return 'success', responseActions except Exception as e: notification_action = webclientActions.NotificationAction.generate( "UpdateViewEntry '" + str(action['view']) + "' failed with: " + str(e), "error") responseActions = [notification_action] logManager.error( str(type(e).__name__) + 'in ExecuteViewActionsActionHandler ' + action['view']) traceback.print_exc(file=sys.stdout) return 'success', responseActions notification_action = webclientActions.NotificationAction.generate( "View >" + viewname + "< not found", "error") return 'success', [notification_action]
def createEntry(self): return ObjDict(self.entryPropList.copy())
def createMeta(self): return ObjDict(self.metaDataList.copy())
def handleActionRequest(self, identity, expire_date, request): actions = request['actions'] response_actions = [] response_data = {} user = None print('identity = ', identity) for action in actions: if action['workspace'] in self.actionsMap: print('action workspace found') if action['action'] in self.actionsMap[action['workspace']]: print('action', action['action'], 'found in workspace') user = (self.userManager.getUser(identity)) workspace = self.workspacesMap[action['workspace']] try: handle_result = self.actionsMap[action['workspace']][ action['action']].handle(ObjDict(action), user, workspace, self) except RequireLoginError: route_action = webclientActions.RouteAction.generate( 'dashboard', delay=0) notification_action = webclientActions.NotificationAction.generate( "Login required", "error", delay=2) response_actions = [route_action, notification_action] return self.buildActionReply(response_actions, response_data) handle_result_len = len(handle_result) if handle_result_len == 1: state, actions, response = handle_result, [], {} elif handle_result_len == 2: state, actions, response = handle_result[ 0], handle_result[1], {} elif handle_result_len == 3: state, actions, response = handle_result else: state, actions, response = "error", [], {} self.db.session.commit() if state == 'success': pass else: logManager.error('Action {} failed', action['action']) response_intersection = response_data.keys() & response if len(response_intersection) != 0: logManager.warning( 'Action response data for {} overrided the following properties', action['action'], response_intersection) response_data = {**response_data, **response} response_actions = response_actions + actions else: logManager.error('action ' + action['action'] + ' not found in ' + action['workspace']) else: logManager.error('action workspace: "' + action['workspace'] + ' "not found') if expire_date is not None and identity is not None: difference = expire_date - datetime.datetime.now() remaining_minutes = difference.seconds / 60 session_expiration_minutes = self.config['SYSTEM'].get( 'session_expiration_minutes', 15) if remaining_minutes < session_expiration_minutes * 0.5: access_token = self.userManager.updateAccessToken(identity) response_actions.insert( 0, webclientActions.UpdateSessionTokenAction.generate( access_token)) return self.buildActionReply(response_actions, response_data)
def executeActionLink(self, hash, user): response_actions = [] al = self.actionLink.query.filter_by(hash=hash).first() if al is None: raise NotFoundError if al.expire_on_date < arrow.utcnow(): raise ExpiredError try: if str(al.workspace) in self.actionsMap: print('action workspace found') if al.action in self.actionsMap[al.workspace]: print('action', al.action, 'found in workspace') if user is None and al.need_login is True: response_actions.append( webclientActions.NotificationAction.generate( "Login needed you will be redirected.", "success")) response_actions.append( webclientActions.RouteAction.generate( "user/login?redirect=actionlink/" + hash, 3)) return response_actions if al.redirect_to != "": response_actions.append( webclientActions.RouteAction.generate( al.redirect_to, 2)) workspace = self.workspacesMap[al.workspace] print(user, workspace) param = al.action_data_json state, actions = self.actionsMap[al.workspace][ al.action].handle(ObjDict(param), user, workspace, self) if state == 'success': logManager.info( 'Actionlink succed with {} for user: {}', actions, user) else: logManager.error( 'Actionlink failed with {} for user: {}', actions, user) raise Exception( str('Action failed with {} for user: {}', actions, user)) response_actions = response_actions + actions return response_actions else: logManager.error('action ' + al.action + ' not found in ' + al.workspace) raise Exception( str('action workspace: "' + al.workspace + '" not found')) else: logManager.error('action workspace: "' + al.workspace + '"not found') raise Exception( str('action workspace: "' + al.workspace + '"not found')) except Exception as e: raise e finally: if al.run_only_once is True: self.db.session.delete(al)
def register_job(self, workspace, job_class, log_in_db=False): jobkey = "" workspace_name = None if workspace is not None: if type(workspace) is str: jobkey += workspace + '/' workspace_name = workspace.name else: jobkey += workspace.name + '/' workspace_name = workspace.name jobInstance = job_class() jobkey += jobInstance.name jobInstance.job_key = jobkey if workspace is not None: jobInstance.workspace = workspace.name job = { 'job_class': job_class, 'name': jobInstance.name, 'workspace': workspace_name, 'description': jobInstance.description, 'parameters': jobInstance.parameters, 'trigger': 'Internal', 'log_in_db': log_in_db, 'cron': jobInstance.cron, 'day': jobInstance.day, 'week': jobInstance.week, 'day_of_week': jobInstance.day_of_week, 'hour': jobInstance.hour, 'minute': jobInstance.minute, 'second': jobInstance.second } if jobInstance.cron is True: cron_list = [] if job_class.minute is None: cron_list.append("*") else: cron_list.append(job_class.minute) if job_class.hour is None: cron_list.append("*") else: cron_list.append(job_class.hour) if job_class.day is None: cron_list.append("*") else: cron_list.append(job_class.day) cron_list.append("*") if job_class.day_of_week is None: cron_list.append("*") else: cron_list.append(job_class.day_of_week) cron_string = " ".join(cron_list) options = Options() options.throw_exception_on_parse_error = False options.day_of_week_start_index_zero = True options.use_24hour_time_format = True options.casing_type = CasingTypeEnum.LowerCase descripter = ExpressionDescriptor(cron_string, options) logManager.info("Register repetitive job '{}' triggered {}".format( jobkey, descripter.get_description())) self.scheduler.add_job(jobInstance.start_job, kwargs=({ "job_id": str(jobkey) }), id=(str(jobkey)), trigger='cron', replace_existing=True, day=job_class.day, day_of_week=job_class.day_of_week, week=job_class.week, hour=job_class.hour, minute=job_class.minute, second=job_class.second) job['trigger'] = descripter.get_description() self.jobs[str(jobkey)] = ObjDict(job.copy())