def _generate_for_options(self, report_name, options): from ckanext.report.report_registry import ReportRegistry registry = ReportRegistry.instance() report = registry.get_report(report_name) all_options = report.add_defaults_to_options(options, report.option_defaults) report.refresh_cache(all_options)
def report_data_get(context=None, data_dict=None): """ Returns the data for the report The data may have been cached in the database or may have been generated on demand so the date when the data was generated is also returned :param id: The name of the report :type id: string :param options: Dictionary of options to pass to the report (optional) :type options: dict :returns: A list containing the data and the date on which it was created :rtype: list """ logic.check_access('report_data_get', context, data_dict) id = logic.get_or_bust(data_dict, 'id') options = data_dict.get('options', {}) report = ReportRegistry.instance().get_report(id) data, date = report.get_fresh_report(**options) return data, date.isoformat()
def _generate(self, report_list=None): from ckanext.report.report_registry import ReportRegistry registry = ReportRegistry.instance() if report_list: for report_name in report_list: registry.get_report(report_name).refresh_cache_for_all_options() else: registry.refresh_cache_for_all_reports()
def _list(self): from ckanext.report.report_registry import ReportRegistry registry = ReportRegistry.instance() for plugin, report_name, report_title in registry.get_names(): report = registry.get_report(report_name) date = report.get_cached_date() print "%s: %s %s" % (plugin, report_name, date.strftime("%d/%m/%Y %H:%M") if date else "(not cached)")
def _generate(self, report_list=None): from ckanext.report.report_registry import ReportRegistry registry = ReportRegistry.instance() if report_list: for report_name in report_list: registry.get_report( report_name).refresh_cache_for_all_options() else: registry.refresh_cache_for_all_reports()
def _list(self): from ckanext.report.report_registry import ReportRegistry registry = ReportRegistry.instance() for plugin, report_name, report_title in registry.get_names(): report = registry.get_report(report_name) date = report.get_cached_date() print '%s: %s %s' % (plugin, report_name, date.strftime('%d/%m/%Y %H:%M') if date else '(not cached)')
def report_data_get(context=None, data_dict=None): id = get_or_bust(data_dict, 'id') report = ReportRegistry.instance().get_report(id) if hasattr(report, 'authorize'): user = context['auth_user_obj'] options = data_dict['options'] if not report.authorize(user, options): return {'success': False} return {'success': True}
def report_list(context=None, data_dict=None): """ Lists all available reports :returns: A list of report dictionaries (see report_show) :rtype: list """ logic.check_access('report_list', context, data_dict) registry = ReportRegistry.instance() reports = registry.get_reports() user = context['auth_user_obj'] reports = filter(lambda r: r.is_visible_to_user(user), reports) return [report.as_dict() for report in reports]
def report_refresh(context=None, data_dict=None): """ Causes the cached data of the report to be refreshed :param id: The name of the report :type id: string :param options: Dictionary of options to pass to the report :type options: dict """ logic.check_access('report_refresh', context, data_dict) id = logic.get_or_bust(data_dict, 'id') options = data_dict.get('options') report = ReportRegistry.instance().get_report(id) report.refresh_cache(options)
def _generate(self, report_list=None): import time from ckanext.report.report_registry import ReportRegistry timings = {} registry = ReportRegistry.instance() if report_list: for report_name in report_list: s = time.time() registry.get_report( report_name).refresh_cache_for_all_options() timings[report_name] = time.time() - s else: s = time.time() registry.refresh_cache_for_all_reports() timings["All Reports"] = time.time() - s self.log.info("Report generation complete %s", timings)
def explicit_default_options(report_name): '''Returns the options that are needed for URL parameters to load a report with the default options. Normally you can just load a report at /report/<name> but there is an exception for checkboxes that default to True. e.g. include_sub_organizations. If you uncheck the checkbox and submit the form then rather than sending you to /report/<name>?include_sub_organizations=0 it misses out the parameter completely. Therefore the absence of the parameter must be taken to mean include_sub_organizations=0, and when we want the (default) value of 1 we have to be explicit. ''' explicit_defaults = {} options = ReportRegistry.instance().get_report(report_name).option_defaults for key in options: if options[key] is True: explicit_defaults[key] = 1 return explicit_defaults
def report_key_get(context=None, data_dict=None): """ Returns a key that will identify the report and options :param id: The name of the report :type id: string :param options: Dictionary of options to pass to the report :type options: dict :returns: A key to identify the report :rtype: string """ logic.check_access('report_key_get', context, data_dict) id = logic.get_or_bust(data_dict, 'id') options = data_dict.get('options') report = ReportRegistry.instance().get_report(id) return report.generate_key(options).replace('?', '_')
def report_show(context=None, data_dict=None): """ Shows a single report Does not provide the data for the report which must be obtained by a separate call to report_data_get. :param id: The name of the report :type id: string :returns: A dictionary of information about the report :rtype: dictionary """ logic.check_access('report_show', context, data_dict) id = logic.get_or_bust(data_dict, 'id') try: report = ReportRegistry.instance().get_report(id) except KeyError: raise p.toolkit.ObjectNotFound('Report not found: %s' % id) return report.as_dict()
def view(self, report_name, organization=None, refresh=False): c.report_name = report_name try: report = ReportRegistry.instance().get_report(report_name) except KeyError: t.abort(404, 'Report not found') # ensure correct url is being used if 'organization' in t.request.environ['pylons.routes_dict'] and \ 'organization' not in report.option_defaults: t.redirect_to(dguhelpers.relative_url_for(organization=None)) elif 'organization' not in t.request.environ['pylons.routes_dict'] and\ 'organization' in report.option_defaults and \ report.option_defaults['organization']: org = report.option_defaults['organization'] t.redirect_to(dguhelpers.relative_url_for(organization=org)) if 'organization' in t.request.params: # organization should only be in the url - let the param overwrite # the url. t.redirect_to(dguhelpers.relative_url_for()) # options c.options = report.add_defaults_to_options(t.request.params) if 'format' in c.options: format = c.options.pop('format') else: format = None if 'organization' in report.option_defaults: c.options['organization'] = organization c.offer_organization_index = \ report.option_defaults['organization'] is None c.options_html = {} for option in c.options: try: c.options_html[option] = \ t.render_snippet('report/option_%s.html' % option) except TemplateNotFound: continue c.report_title = report.title c.report_description = report.description # Refresh the cache if requested if t.request.method == 'POST' and not format: if not (c.userobj and c.userobj.sysadmin): t.abort(401) report.refresh_cache(c.options) # Alternative way to refresh the cache - not in the UI, but is # handy for testing try: refresh = t.asbool(t.request.params.get('refresh')) except ValueError: refresh = False if refresh: if not (c.userobj and c.userobj.sysadmin): t.abort(401) c.options.pop('refresh') report.refresh_cache(c.options) # Don't want the refresh=1 in the url once it is done t.redirect_to(dguhelpers.relative_url_for(refresh=None)) # Check for any options not allowed by the report for key in c.options: if key not in report.option_defaults: t.abort(400, 'Option not allowed by report: %s' % key) try: c.data, c.report_date = report.get_fresh_report(**c.options) except t.ObjectNotFound: t.abort(404) if format and format != 'html': ensure_data_is_dicts(c.data) anonymise_user_names(c.data, organization=c.options.get('organization')) if format == 'csv': filename = 'report_%s.csv' % report.generate_key(c.options).replace('?', '_') t.response.headers['Content-Type'] = 'application/csv' t.response.headers['Content-Disposition'] = str('attachment; filename=%s' % (filename)) return make_csv_from_dicts(c.data['table']) elif format == 'json': t.response.headers['Content-Type'] = 'application/json' c.data['generated_at'] = c.report_date return json.dumps(c.data, cls=DateTimeJsonEncoder) else: t.abort(400, 'Format not known - try html, json or csv') c.are_some_results = bool(c.data['table'] if 'table' in c.data else c.data) if c.are_some_results: # you can't pass args into genshi template, so it will just look # for c.data c.report_snippet = t.render_snippet(report.get_template()) return t.render('report/view.html')
def index(self): registry = ReportRegistry.instance() c.reports = registry.get_reports() return t.render('report/index.html')
def view(self, report_name, organization=None, refresh=False): c.report_name = report_name try: report = ReportRegistry.instance().get_report(report_name) except KeyError: t.abort(404, 'Report not found') # ensure correct url is being used if 'organization' in t.request.environ['pylons.routes_dict'] and \ 'organization' not in report.option_defaults: t.redirect_to(dguhelpers.relative_url_for(organization=None)) elif 'organization' not in t.request.environ['pylons.routes_dict'] and\ 'organization' in report.option_defaults and \ report.option_defaults['organization']: org = report.option_defaults['organization'] t.redirect_to(dguhelpers.relative_url_for(organization=org)) if 'organization' in t.request.params: # organization should only be in the url - let the param overwrite # the url. t.redirect_to(dguhelpers.relative_url_for()) # options c.options = report.add_defaults_to_options(t.request.params) if 'format' in c.options: format = c.options.pop('format') else: format = None if 'organization' in report.option_defaults: c.options['organization'] = organization c.offer_organization_index = \ report.option_defaults['organization'] is None c.options_html = {} for option in c.options: try: c.options_html[option] = \ t.render_snippet('report/option_%s.html' % option) except TemplateNotFound: continue c.report_title = report.title c.report_description = report.description # Refresh the cache if requested if t.request.method == 'POST' and not format: if not (c.userobj and c.userobj.sysadmin): t.abort(401) report.refresh_cache(c.options) # Alternative way to refresh the cache - not in the UI, but is # handy for testing try: refresh = t.asbool(t.request.params.get('refresh')) except ValueError: refresh = False if refresh: if not (c.userobj and c.userobj.sysadmin): t.abort(401) c.options.pop('refresh') report.refresh_cache(c.options) # Don't want the refresh=1 in the url once it is done t.redirect_to(dguhelpers.relative_url_for(refresh=None)) # Check for any options not allowed by the report for key in c.options: if key not in report.option_defaults: t.abort(400, 'Option not allowed by report: %s' % key) try: c.data, c.report_date = report.get_fresh_report(**c.options) except t.ObjectNotFound: t.abort(404) if format and format != 'html': ensure_data_is_dicts(c.data) anonymise_user_names(c.data, organization=c.options.get('organization')) if format == 'csv': filename = 'report_%s.csv' % report.generate_key( c.options).replace('?', '_') t.response.headers['Content-Type'] = 'application/csv' t.response.headers['Content-Disposition'] = str( 'attachment; filename=%s' % (filename)) return make_csv_from_dicts(c.data['table']) elif format == 'json': t.response.headers['Content-Type'] = 'application/json' c.data['generated_at'] = c.report_date return json.dumps(c.data, cls=DateTimeJsonEncoder) else: t.abort(400, 'Format not known - try html, json or csv') c.are_some_results = bool(c.data['table'] if 'table' in c.data else c.data) if c.are_some_results: # you can't pass args into genshi template, so it will just look # for c.data c.report_snippet = t.render_snippet(report.get_template()) return t.render('report/view.html')