def step2_new(self, app, action, **params): owner = splunk.auth.getCurrentUser()['name'] saved_search = SavedSearch.get(params.get('id')) dashboard = Dashboard(app, owner, None) dashboard.metadata.sharing = 'app' dashboards = Dashboard.filter_by_can_write_simple_xml(app) template_args = dict(app=app, saved_search=saved_search, dashboard=dashboard, dashboards=dashboards, dashboard_action=None, panel_type='event', panel_title=None) return self.render_template('dashboardwizard/step2.html', template_args)
def step2_create(self, app, step, action, **params): # saved search models saved_search = SavedSearch.get(params.get('id')) # dashboard model dashboard_action = params.get('dashboard.action') owner = splunk.auth.getCurrentUser()['name'] if dashboard_action=='get': try: dashboard = Dashboard.get(params.get('dashboard.id')) except: dashboard = Dashboard(app, owner, None) dashboard.errors = [_('Please choose an existing dashboard.')] else: dashboard_name = params.get('dashboard.name', '') try: dashboard_name.encode('ascii') except: date = str(splunk.util.dt2epoch(datetime.datetime.now())).replace('.', '_') dashboard_name = '%s_%s' % (splunk.auth.getCurrentUser()['name'], date) dashboard = Dashboard(app, owner, dashboard_name) dashboard.label = params.get('dashboard.label') dashboard.metadata.sharing = params.get('sharing', 'user') if not dashboard.errors and saved_search.passive_save() and dashboard.passive_save(): # update saved search only on save success if dashboard.metadata.sharing=='app': try: saved_search.share_app() except Exception: saved_search.errors = [_('Search %s cannot be shared because it already exists. Try using another search name in the previous step.') % saved_search.name ] else: try: saved_search.unshare() except Exception: saved_search.errors = [_('Search %s cannot be private because it already exists. Try using another search name in the previous step.') % saved_search.name] if not saved_search.errors: raise cherrypy.HTTPRedirect(self.make_url(['dashboardwizard', app, 'step3'], _qs=dict(search_id=saved_search.id, dashboard_id=dashboard.id, dashboard_action=dashboard_action)), 303) dashboards = Dashboard.filter_by_can_write_simple_xml() template_args = dict(app=app, saved_search=saved_search, dashboard=dashboard, dashboards=dashboards, dashboard_action=dashboard_action) return self.render_template('dashboardwizard/step2.html', template_args)
def createContainer(self, namespace, view_id='', view_label='', container_type='dashboard'): ''' Handles dashboard creation GET /<namespace> ==> HTML template form to input create data POST /<namespace> ==> Saves the HTML input values from GET == returns JSON response ''' # # serve template # if cherrypy.request.method == 'GET': return self.render_template('viewmaster/create_dashboard.html', {'namespace': namespace}) # # handle create view # output = jsonresponse.JsonResponse() # clean inputs view_id = re.sub(r'[^\w]', '', view_id) view_label = view_label.strip() or view_id if view_id == '': output.success = False output.addError(_('Dashboard ID cannot be empty')) return self.render_json(output) # check that view doesn't already exist try: username = cherrypy.session['user'].get('name') dash_id = en.buildEndpoint(VIEW_ENTITY_CLASS, view_id, namespace=namespace, owner=username) Dashboard.get(dash_id) output.success = False output.addError( _('Cannot create new %(container_type)s: %(view_id)s already exists') \ % {'container_type': container_type, 'view_id': view_id}) return self.render_json(output) except splunk.ResourceNotFound: pass # generate new try: view = Dashboard(namespace, username, view_id) view.label = view_label view.save() output.data = {'view_id': view_id, 'view_label': view_label} output.addInfo(_('Successfully created new %(container_type)s: %(view_id)s') \ % {'container_type': container_type, 'view_id': view_id}) logger.info('Created new %s: namespace=%s id=%s label=%s' % (container_type, namespace, view_id, view_label)) except Exception, e: logger.exception(e) output.success = False output.addError(_('Unable to create dashboard: %s') % e)