def create_report(): form = CustomReportForm(request.form) if request.method == 'POST' and form.validate(): custom_report = CustomReport() custom_report.name = form.name.data custom_report.url = utils.parse_url(form.url.data) custom_report.description = form.description.data db_session.add(custom_report) db_session.commit() return redirect(url_for('custom_report.custom_report_index')) return render_template('reports/create_report.html', form=form)
def get_custom_report_metadata(self, report_name=None, collection=None, single=True): ''' return CustomReport ndb entry for report of specified name ''' if collection and report_name: crq = CustomReport.query(CustomReport.name==report_name, CustomReport.collection==collection) elif report_name: crq = CustomReport.query(CustomReport.name==report_name) elif collection: crq = CustomReport.query(CustomReport.collection==collection) else: crq = CustomReport.query().order(CustomReport.name) if crq and single: crmset = crq.fetch(1) if len(crmset): return crmset[0] logging.error('No custom report found with name=%s' % report_name) return None elif report_name and not crq: logging.error('No custom report found with name=%s' % report_name) return crq
def get_custom_report(self, msg=""): ''' custom reports page ''' if not self.user in self.AUTHORIZED_USERS: # require superuser return self.no_auth_sorry() if (self.request.POST.get('action') == 'Create new Custom Report'): title = self.request.POST.get('title') name = self.request.POST.get('name') existing_crm = self.get_custom_report_metadata(name, single=False) if existing_crm.count(): msg = "Cannot create report '%s', already exists" % name else: crm = CustomReport(title=title, name=name) #crm.html = """<div id="contain-{{report_name}}" style="min-width: 310px; height: 400px; margin: 0 auto"> crm.html = """<div id="contain-{{report_name}}" style="min-width: 310px; margin: 0 auto"> <img src="/images/loading_icon.gif"/>\n</div>""" jstemp, jsfn, uptodate = JINJA_ENVIRONMENT.loader.get_source( JINJA_ENVIRONMENT, 'custom_report_default.js') crm.javascript = str(jstemp) #jstemp = JINJA_ENVIRONMENT.get_template('custom_report_default.js') #crm.javascript = jstemp.render({}) logging.info("[cr] creating new custom report %s" % crm) crm.put() # return self.redirect('/custom/edit_report/%s' % name) # # because of how NDB may take awhile to store the new entry, we cannot go directly # to the report edit page, but instead, must go to a transition page asking # for the user to click on a button first. data = self.common_data.copy() data.update({ 'report': crm, }) template = JINJA_ENVIRONMENT.get_template( 'edit_custom_report_transition.html') self.response.out.write(template.render(data)) return elif (self.request.POST.get('action') == 'Edit this report'): name = self.request.POST.get('name') return self.redirect('/custom/edit_report/%s' % name) elif (self.request.POST.get('action') == 'Upload Custom Report(s)'): report_file_data = self.request.get('file') overwrite = (self.request.get('overwrite') == 'yes') msg += self.import_custom_report_from_file_data( report_file_data, overwrite) cr_page_title = "Custom Reports" try: cr_page_title = local_config.CUSTOM_REPORTS_PAGE_TITLE except Exception as err: pass data = self.common_data.copy() data.update({ 'is_staff': self.is_superuser(), 'reports': self.get_custom_report_metadata(single=False), 'msg': msg, 'custom_report': self.custom_report_container( self.is_authorized_for_custom_report, staff=True, group_tag="{{group_tag}}", ), 'cr_page_title': cr_page_title, }) template = JINJA_ENVIRONMENT.get_template('custom_reports.html') self.response.out.write(template.render(data))
def get_custom_report(self, msg=""): ''' custom reports page ''' if not self.user in self.AUTHORIZED_USERS: # require superuser return self.no_auth_sorry() if (self.request.POST.get('action')=='Create new Custom Report'): title = self.request.POST.get('title') name = self.request.POST.get('name') existing_crm = self.get_custom_report_metadata(name, single=False) if existing_crm.count(): msg = "Cannot create report '%s', already exists" % name else: crm = CustomReport(title=title, name=name) #crm.html = """<div id="contain-{{report_name}}" style="min-width: 310px; height: 400px; margin: 0 auto"> crm.html = """<div id="contain-{{report_name}}" style="min-width: 310px; margin: 0 auto"> <img src="/images/loading_icon.gif"/>\n</div>""" jstemp, jsfn, uptodate = JINJA_ENVIRONMENT.loader.get_source(JINJA_ENVIRONMENT, 'custom_report_default.js') crm.javascript = str(jstemp) #jstemp = JINJA_ENVIRONMENT.get_template('custom_report_default.js') #crm.javascript = jstemp.render({}) logging.info("[cr] creating new custom report %s" % crm) crm.put() # return self.redirect('/custom/edit_report/%s' % name) # # because of how NDB may take awhile to store the new entry, we cannot go directly # to the report edit page, but instead, must go to a transition page asking # for the user to click on a button first. data = self.common_data.copy() data.update({'report': crm, }) template = JINJA_ENVIRONMENT.get_template('edit_custom_report_transition.html') self.response.out.write(template.render(data)) return elif (self.request.POST.get('action')=='Edit this report'): name = self.request.POST.get('name') return self.redirect('/custom/edit_report/%s' % name) elif (self.request.POST.get('action')=='Upload Custom Report(s)'): report_file_data = self.request.get('file') overwrite = (self.request.get('overwrite')=='yes') msg += self.import_custom_report_from_file_data(report_file_data, overwrite) cr_page_title = "Custom Reports" try: cr_page_title = local_config.CUSTOM_REPORTS_PAGE_TITLE except Exception as err: pass data = self.common_data.copy() data.update({'is_staff': self.is_superuser(), 'reports': self.get_custom_report_metadata(single=False), 'msg': msg, 'custom_report': self.custom_report_container(self.is_authorized_for_custom_report, staff=True, group_tag = "{{group_tag}}", ), 'cr_page_title': cr_page_title, }) template = JINJA_ENVIRONMENT.get_template('custom_reports.html') self.response.out.write(template.render(data))