Esempio n. 1
0
    def ajax_get_report_html(self, report_name=None):
        '''
        return HTML for specified custom report
        '''
        crm, pdata, auth_ok, msg = self.custom_report_auth_check(report_name)	# crm = CourseReport model
        if not auth_ok:
            return self.no_auth_sorry()

        if self.request.get('save_parameters'):
            self.session['edit_report_parameter_values'] = json.dumps({x:v for x,v in pdata.items() if v is not None})
            logging.info("Saved edit_report_parameter_values = %s" % self.session['edit_report_parameter_values'])

        parameters = {x:v for x,v in pdata.items() if v is not None}
        parameters['orgname'] = self.ORGNAME
        parameters['dashboard_mode'] = self.MODE	# 'mooc' or '' (empty meaning residential, non-mooc)
        parameters['feature_flags'] = self.FEATURE_FLAGS

        uuid = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))

        render_data = {'report_name': report_name,
                       'report_uuid': "%s_%s" % (report_name, uuid),
                       'orgname': self.ORGNAME,
                       'parameters': json.dumps(parameters),	# for js
                       'parameter_values': parameters,		# for html template variables
                       'custom_report': self.custom_report_container(self.is_authorized_for_custom_report, 
                                                                     **parameters),	# pass pdata so children of page also get parameters
                       'is_staff': self.is_superuser(),
                       'is_pm': self.is_pm(),
                       'cr_js_library': self.get_custom_report_library_javascript(),
                       }
        render_data.update(pdata)

        #html = crm.html
        html = JINJA_ENVIRONMENT.from_string(crm.html).render(render_data)	# allows custom reports to be included

        js_src = "<script type='text/javascript'>"
        js_src += "$(document).ready( function () {%s} );" % crm.javascript	# js goes in html
        js_src += "</script>" 

        try:
            js = Template(js_src).render(render_data)	# render template variables, ideally w/o global context
        except Exception as err:
            logging.info("Warning: could not render js in custom_reports, err=%s" % str(err))
            js = JINJA_ENVIRONMENT.from_string(js_src).render(render_data)
            
        #template = JINJA_ENVIRONMENT.from_string(html)

        # logging.info("get_report_html name=%s, parameters=%s" % (report_name, parameters))

        data = {'html': html + js,
                'js': crm.javascript,
                }

        self.response.headers['Content-Type'] = 'application/json'   
        self.response.out.write(json.dumps(data))
Esempio n. 2
0
    def get_custom_report_page(self, report_name):
        '''
        Single custom report which behaves as a HTML page
        '''
        crm, pdata, auth_ok, msg = self.custom_report_auth_check(
            report_name)  # crm = CourseReport model
        if not auth_ok:
            return self.no_auth_sorry()

        if not crm:
            self.response.write(msg)
            return

        html = crm.html
        html += "<script type='text/javascript'>"
        html += "$(document).ready( function () {%s} );" % crm.javascript  # js goes in html, and thus gets template vars rendered
        html += "</script>"

        template = JINJA_ENVIRONMENT.from_string(html)
        parameters = {x: v for x, v in pdata.items() if v is not None}
        parameters['orgname'] = self.ORGNAME
        parameters['feature_flags'] = self.FEATURE_FLAGS

        render_data = self.common_data.copy()

        if crm.meta_info.get('need_tags'):
            render_data['course_tags'] = self.get_course_listings_tags()

        if crm.meta_info.get('need_listings'):
            render_data['course_listings'] = self.get_course_listings()

        if ('course_id' in pdata) and pdata['course_id']:
            render_data['base'] = self.get_base(pdata['course_id'])
            logging.info('[page] base=%s' % render_data['base'])

        render_data.update({
            'report_name':
            report_name,
            'parameters':
            json.dumps(parameters),  # for js
            'parameter_values':
            parameters,  # for html template variables
            'custom_report':
            self.custom_report_container(
                self.is_authorized_for_custom_report,
                **pdata),  # pass pdata so children of page also get parameters
            'is_staff':
            self.is_superuser(),
            'is_pm':
            self.is_pm(),
            'msg':
            msg,
            'nav_is_active':
            self.nav_is_active(report_name),
            'cr_js_library':
            self.get_custom_report_library_javascript(),
        })
        render_data.update(pdata)
        self.response.out.write(template.render(render_data))
Esempio n. 3
0
    def get_custom_report_page(self, report_name):
        '''
        Single custom report which behaves as a HTML page
        '''
        crm, pdata, auth_ok, msg = self.custom_report_auth_check(report_name)	# crm = CourseReport model
        if not auth_ok:
            return self.no_auth_sorry()

        if not crm:
            self.response.write(msg)
            return

        html = crm.html
        html += "<script type='text/javascript'>"
        html += "$(document).ready( function () {%s} );" % crm.javascript	# js goes in html, and thus gets template vars rendered
        html += "</script>" 

        template = JINJA_ENVIRONMENT.from_string(html)
        parameters = {x:v for x,v in pdata.items() if v is not None}
        parameters['orgname'] = self.ORGNAME
        parameters['feature_flags'] = self.FEATURE_FLAGS

        render_data = self.common_data.copy()

        if crm.meta_info.get('need_tags'):
            render_data['course_tags'] = self.get_course_listings_tags()

        if crm.meta_info.get('need_listings'):
            render_data['course_listings'] = self.get_course_listings()

        if ('course_id' in pdata) and pdata['course_id']:
            render_data['base'] = self.get_base(pdata['course_id'])
            logging.info('[page] base=%s' % render_data['base'])

        render_data.update({'report_name': report_name,
                            'parameters': json.dumps(parameters),	# for js
                            'parameter_values': parameters,		# for html template variables
                            'custom_report': self.custom_report_container(self.is_authorized_for_custom_report, 
                                                                          **pdata),	# pass pdata so children of page also get parameters
                            'is_staff': self.is_superuser(),
                            'is_pm': self.is_pm(),
                            'msg': msg,
                            'nav_is_active': self.nav_is_active(report_name),
                            'cr_js_library': self.get_custom_report_library_javascript(),
                        })
        render_data.update(pdata)
        self.response.out.write(template.render(render_data))
Esempio n. 4
0
    def ajax_get_report_html(self, report_name=None):
        '''
        return HTML for specified custom report
        '''
        crm, pdata, auth_ok, msg = self.custom_report_auth_check(
            report_name)  # crm = CourseReport model
        if not auth_ok:
            return self.no_auth_sorry()

        if self.request.get('save_parameters'):
            self.session['edit_report_parameter_values'] = json.dumps(
                {x: v
                 for x, v in pdata.items() if v is not None})
            logging.info("Saved edit_report_parameter_values = %s" %
                         self.session['edit_report_parameter_values'])

        parameters = {x: v for x, v in pdata.items() if v is not None}
        parameters['orgname'] = self.ORGNAME
        parameters[
            'dashboard_mode'] = self.MODE  # 'mooc' or '' (empty meaning residential, non-mooc)
        parameters['feature_flags'] = self.FEATURE_FLAGS

        uuid = ''.join(
            random.choice(string.ascii_uppercase + string.digits)
            for _ in range(10))

        render_data = {
            'report_name':
            report_name,
            'report_uuid':
            "%s_%s" % (report_name, uuid),
            'orgname':
            self.ORGNAME,
            'parameters':
            json.dumps(parameters),  # for js
            'parameter_values':
            parameters,  # for html template variables
            'custom_report':
            self.custom_report_container(
                self.is_authorized_for_custom_report, **parameters
            ),  # pass pdata so children of page also get parameters
            'is_staff':
            self.is_superuser(),
            'is_pm':
            self.is_pm(),
            'cr_js_library':
            self.get_custom_report_library_javascript(),
        }
        render_data.update(pdata)

        #html = crm.html
        html = JINJA_ENVIRONMENT.from_string(crm.html).render(
            render_data)  # allows custom reports to be included

        js_src = "<script type='text/javascript'>"
        js_src += "$(document).ready( function () {%s} );" % crm.javascript  # js goes in html
        js_src += "</script>"

        try:
            js = Template(js_src).render(
                render_data
            )  # render template variables, ideally w/o global context
        except Exception as err:
            logging.info(
                "Warning: could not render js in custom_reports, err=%s" %
                str(err))
            js = JINJA_ENVIRONMENT.from_string(js_src).render(render_data)

        #template = JINJA_ENVIRONMENT.from_string(html)

        # logging.info("get_report_html name=%s, parameters=%s" % (report_name, parameters))

        data = {
            'html': html + js,
            'js': crm.javascript,
        }

        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json.dumps(data))
Esempio n. 5
0
            def __getitem__(self, report_name):
                try:
                    crm = other.get_custom_report_metadata(report_name)
                    err = None
                except Exception as err:
                    crm = None
                if not crm:
                    logging.info("No custom report '%s' found, err=%s" % (report_name, err))
                    return "Missing custom report %s" % report_name

                # check access authorization
                # logging.info('[crc] checking auth for report %s, pdata=%s' % (crm.name, pdata))
                auth_ok, msg = is_authorized_for_custom_report(crm, pdata)
                if not auth_ok:
                    return ""			# return empty string if not authorized

                # logging.info('[cr] name=%s, title=%s' % (crm.name, crm.title))	# debugging

                title = JINJA_ENVIRONMENT.from_string(crm.title)
                try:
                    title_rendered = title.render(pdata)
                except Exception as err:
                    logging.error('[cr] Failed to render report %s title %s' % (crm.name, crm.title))
                    title = crm.title

                parameters = {x:v for x,v in pdata.items() if v is not None}
                parameters['orgname'] = other.ORGNAME
                parameters['dashboard_mode'] = other.MODE	# 'mooc' or '' (empty meaning residential, non-mooc)
                parameters['course_report'] = other.get_course_report_dataset()
                parameters['course_report_org'] = other.get_course_report_dataset(force_use_org=True)
                parameters['orgname'] = other.ORGNAME
                
                if 'require_table' in (crm.meta_info or []):
                    dataset = None
                    table = crm.meta_info['require_table']
                    if '{' in table:
                        try:
                            table = table.format(**parameters)
                        except Exception as err:
                            logging.error("Cannot substitute for parameters in require_table=%s, err=%s" % (table, err))
                    if '.' in table:
                        (dataset, table) = table.split('.', 1)
                    else:
                        course_id = parameters.get('course_id')
                        if course_id:
                            try:
                                dataset = bqutil.course_id2dataset(course_id, use_dataset_latest=other.use_dataset_latest())
                            except Exception as err:
                                logging.error("failed to get dataset for course_id=%s" % course_id)
                                raise 
                        else:
                            logging.info("Suppressing report %s because dataset not specifid in require_table %s" % (title, table))
                            dataset = None
                            
                    if dataset is not None:
                        try:
                            tinfo = bqutil.get_bq_table_info(dataset, table) or None
                        except Exception as err:
                            tinfo = None
                            if not "Not Found" in str(err):
                                logging.error(err)
                                logging.error(traceback.format_exc())
                        if not tinfo:
                            logging.info("Suppressing report %s because %s.%s doesn't exist" % (title, dataset, table))
                            return ""
                    else:
                        logging.info("Skipping require_table check")

                report_id = hashlib.sha224("%s %s" % (crm.name, json.dumps(pdata))).hexdigest()
                if crm.description:
                    try:
                        crm.description = crm.description.format(**parameters)
                    except Exception as err:
                        logging.info('[cr] %s cannot format description %s' % (crm.name, crm.description))

                if self.do_no_embed and 'embedded' in (crm.meta_info or {}):
                    crm.meta_info.pop('embedded')
                if self.force_embed:
                    crm.meta_info['embedded'] = True
                
                if self.do_always_show:
                    crm.meta_info['always_show'] = True

                template = JINJA_ENVIRONMENT.get_template('custom_report_container.html')
                data = {'is_staff': other.is_superuser(),
                        'report': crm,
                        'report_params': json.dumps(parameters),
                        'report_is_staff': pdata.get('staff'),
                        'report_meta_info': json.dumps(crm.meta_info or {}),
                        'immediate_view': json.dumps(self.immediate_view),
                        'do_embed' : (crm.meta_info or {}).get('embedded') or self.force_embed,
                        'always_show': self.do_always_show,
                        'title': title_rendered,
                        'id': report_id,
                }
                self.immediate_view = False	# return to non-immediate view by default
                self.do_no_embed = False		# return to default
                self.force_embed = False		# return to default
                return template.render(data)