def add_staff_table_entry(self, data): ''' Add staff table entry. Works only with the google spreadsheet staff backend. ''' scdt = getattr(local_config, 'STAFF_COURSE_TABLE', None) m = re.match('docs:([^:]+):([^:]+)', scdt) if not m: logging.error('Attempted to add staff table entry %s, but not using google spreadsheet backend!' % data) return fname = m.group(1) sheet = m.group(2) stable = self.get_staff_table() fields = ['username', 'role', 'course_id', 'notes'] newrow = [data[x] for x in fields] newpos = stable[-1]['sid']+1 gsdata.append_row_to_datasheet(fname, sheet, newrow) # flush cache, to force reload memset = 'docs:%s.%s' % (fname, sheet) mem.delete(memset)
def add_staff_table_entry(self, data): ''' Add staff table entry. Works only with the google spreadsheet staff backend. ''' scdt = getattr(local_config, 'STAFF_COURSE_TABLE', None) m = re.match('docs:([^:]+):([^:]+)', scdt) if not m: logging.error( 'Attempted to add staff table entry %s, but not using google spreadsheet backend!' % data) return fname = m.group(1) sheet = m.group(2) stable = self.get_staff_table() fields = ['username', 'role', 'course_id', 'notes'] newrow = [data[x] for x in fields] newpos = stable[-1]['sid'] + 1 gsdata.append_row_to_datasheet(fname, sheet, newrow) # flush cache, to force reload memset = 'docs:%s.%s' % (fname, sheet) mem.delete(memset)
def export_custom_report_metadata(self, ignore_cache=False, collection=None, report_name=None, download=False): ''' Export custom report metadata to source specified for the collection (in local_config) ''' crq = self.get_custom_report_metadata(report_name=report_name, collection=collection, single=False) fields = {'name': str, 'title': str, 'description': str, 'author': str, 'date': str, 'table_name': str, 'sql': str, 'depends_on': str, 'html': str, 'javascript': str, 'icon': str, 'group_tags': list, 'meta_info': dict} def strip_eol_spaces(code): return '\n'.join([ x.rstrip() for x in code.split('\n')]).expandtabs() def fix_type(xtype, datum): if xtype==dict and not datum: return {} if xtype==list and not datum: return [] try: xdt = xtype(datum) return xdt except Exception as err: msg = "Oops, failed in exporting custom report metadata on xtype=%s, datum=%s" % (xtype, datum) msg += "\nError: %s" % str(err) logging.error(msg) raise Exception(msg) if download: if crq: ret = [] for crm in crq: try: data = {x: fix_type(xtype, getattr(crm, x)) for x, xtype in fields.items()} except Exception as err: logging.error("cannot create data for custom report %s" % crm) raise for cset in ['sql', 'javascript', 'html']: data[cset] = strip_eol_spaces(data[cset] or '') ret.append(data) return ret else: logging.error("No custom report found for report_name=%s, cannot produce json" % report_name) custom_reports_source = self.get_collection_metadata('CUSTOM_REPORTS', collection=collection) if not custom_reports_source: logging.error("no custom reports available for collection %s" % collection) return (fname, sheet) = custom_reports_source[5:].split(':',1) # TODO: this is hardwired for google docs destination = sheet + " Export" # temporarily hardcoded; can do better cnt = 0 for crm in crq: newrow = [str(getattr(crm, x)) for x in fields] try: gsdata.append_row_to_datasheet(fname, destination, newrow) except Exception as err: logging.error("Oops! Failed to append to gsdata %s.%s, cnt=%d, newrow=%s, err=%s" % (fname, destination, cnt, newrow, err)) raise cnt += 1 return cnt, destination