def GenerateNormalResponse(self): if self.IsJsonFormat(): data = {'configs': self.configs} data.update(self.content) dataStr = json.dumps(self.content, encoding='utf-8', indent=2, sort_keys=True) self.response.headers['Content-Type'] = 'application/json' if self.HasAllowedOrigin(): self.response.headers[ 'Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return dataStr if self.IsCSVFormat(): f = cStringIO.StringIO() writer = UnicodeWriter(f) writer.writerow([d['name'] for d in self.header]) for record in self.table: row = [record[d['name']] for d in self.header] writer.writerow(row) dataStr = f.getvalue() f.close() self.response.headers['Content-Type'] = 'text/csv; charset=utf-8' if self.HasAllowedOrigin(): self.response.headers[ 'Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return dataStr if self.IsTSVFormat(): headerStr = u'\t'.join(d['name'] for d in self.header) rowStrs = [] for record in self.table: rowStrs.append(u'\t'.join(u'{}'.format(record[d['name']]) for d in self.header)) tableStr = u'\n'.join(rowStrs) dataStr = u'{}\n{}\n'.format(headerStr, tableStr).encode('utf-8') self.response.headers[ 'Content-Type'] = 'text/tab-separated-values; charset=utf-8' if self.HasAllowedOrigin(): self.response.headers[ 'Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return dataStr data = { 'configs': self.configs, 'menus': self.menus, 'params': self.params } data.update(self.content) data['content'] = json.dumps(self.content, encoding='utf-8', indent=2, sort_keys=True) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' if self.HasAllowedOrigin(): self.response.headers[ 'Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return data
def WriteCSV(rows): with open(filename, 'w') as f: writer = UnicodeWriter(f) writer.writerow(all_field_names) for row in rows: doc_index = row.doc_index doc_id = row.doc_id doc_content = self.SanitizeText(row.doc_content.decode('utf-8')) values = [u''] * field_count for d in self.db(self.db.metadata.doc_index == doc_index).select(self.db.metadata.field_index, self.db.metadata.value): values[d.field_index] = self.SanitizeText(d.value.decode('utf-8')) all_values = [ doc_id, doc_content ] + values writer.writerow(all_values)
def WriteCSV(rows): with open(filename, 'w') as f: writer = UnicodeWriter(f) writer.writerow(all_field_names) for row in rows: doc_index = row.doc_index doc_id = row.doc_id doc_content = self.SanitizeText(row.doc_content.decode('utf-8')) values = [u''] * field_count for d in self.db(self.db.metadata.doc_index == doc_index).select(self.db.metadata.field_index, self.db.metadata.value): values[d.field_index] = self.SanitizeText(d.value.decode('utf-8')) all_values = [ doc_id, doc_content ] + values writer.writerow(all_values)
def GenerateNormalResponse( self ): if self.IsJsonFormat(): data = { 'configs' : self.configs } data.update(self.content) dataStr = json.dumps( self.content, encoding = 'utf-8', indent = 2, sort_keys = True ) self.response.headers['Content-Type'] = 'application/json' if self.HasAllowedOrigin(): self.response.headers['Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return dataStr if self.IsCSVFormat(): f = cStringIO.StringIO() writer = UnicodeWriter(f) writer.writerow( [ d['name'] for d in self.header ] ) for record in self.table: row = [ record[d['name']] for d in self.header ] writer.writerow(row) dataStr = f.getvalue() f.close() self.response.headers['Content-Type'] = 'text/csv; charset=utf-8' if self.HasAllowedOrigin(): self.response.headers['Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return dataStr if self.IsTSVFormat(): headerStr = u'\t'.join( d['name'] for d in self.header ) rowStrs = [] for record in self.table: rowStrs.append( u'\t'.join( u'{}'.format( record[d['name']]) for d in self.header ) ) tableStr = u'\n'.join(rowStrs) dataStr = u'{}\n{}\n'.format( headerStr, tableStr ).encode('utf-8') self.response.headers['Content-Type'] = 'text/tab-separated-values; charset=utf-8' if self.HasAllowedOrigin(): self.response.headers['Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return dataStr data = { 'configs' : self.configs, 'menus' : self.menus, 'params' : self.params } data.update( self.content ) data['content'] = json.dumps( self.content, encoding = 'utf-8', indent = 2, sort_keys = True ) self.response.headers['Content-Type'] = 'text/html; charset=utf-8' if self.HasAllowedOrigin(): self.response.headers['Access-Control-Allow-Origin'] = self.GetAllowedOrigin() return data