def export_file(filename, table_name, file_type=None): """ Export a table to a csv file. """ if not file_type: # Get file's extension name. file_type = os.path.splitext(filename)[1].lower() if len(file_type) > 0: file_type = file_type[1:] writer_class = writers.get_writer(file_type) if not writer_class: raise (MudderyError(ERR.export_data_error, "Unsupport file type %s" % file_type)) writer = writer_class(filename) if not writer: raise (MudderyError(ERR.export_data_error, "Can not export table %s" % table_name)) fields = general_query_mapper.get_all_fields(table_name) header = [field.name for field in fields] writer.writeln(header) records = general_query_mapper.get_all_records(table_name) for record in records: line = [ str(record.serializable_value(field.get_attname())) for field in fields ] writer.writeln(line) writer.save()
def query_record(table_name, record_id): """ Query a record of a table. """ fields = general_query_mapper.get_all_fields(table_name) record = general_query_mapper.get_record_by_id(table_name, record_id) return [str(record.serializable_value(field.name)) for field in fields]
def export_file(filename, table_name, file_type=None): """ Export a table to a csv file. """ if not file_type: # Get file's extension name. file_type = os.path.splitext(filename)[1].lower() if len(file_type) > 0: file_type = file_type[1:] writer_class = writers.get_writer(file_type) if not writer_class: raise(MudderyError(ERR.export_data_error, "Unsupport file type %s" % file_type)) writer = writer_class(filename) if not writer: raise(MudderyError(ERR.export_data_error, "Can not export table %s" % table_name)) fields = general_query_mapper.get_all_fields(table_name) header = [field.name for field in fields] writer.writeln(header) records = general_query_mapper.get_all_records(table_name) for record in records: line = [str(record.serializable_value(field.get_attname())) for field in fields] writer.writeln(line) writer.save()
def query_fields(table_name): """ Query table's data. """ fields = general_query_mapper.get_all_fields(table_name) return [{"name": field.get_attname(), "label": field.verbose_name, "default": field.get_default(), "editable": field.editable, "help_text": field.help_text, "type": field.__class__.__name__} for field in fields]