def __init__(self): """ Constructor @param manager: the S3ResourceController @todo 2.3: error message completion """ T = current.T self.ERROR = Storage( REPORTLAB_ERROR = T("%(module)s not installed") % dict(module="ReportLab"), NO_RECORDS = T("No records in this resource"), XLWT_ERROR = T("%(module)s not installed") % dict(module="xlwt"), ) self.xls = S3Codec.get_codec("xls").encode self.pdf = S3Codec.get_codec("pdf").encode
def __init__(self): """ Constructor @param manager: the S3ResourceController @todo 2.3: error message completion """ T = current.T self.ERROR = Storage( REPORTLAB_ERROR=T("%(module)s not installed") % dict(module="ReportLab"), NO_RECORDS=T("No records in this resource"), XLWT_ERROR=T("%(module)s not installed") % dict(module="xlwt"), ) self.xls = S3Codec.get_codec("xls").encode self.pdf = S3Codec.get_codec("pdf").encode
def __init__(self, manager): """ Constructor @param manager: the S3ResourceController @todo 2.3: error message completion """ self.manager = manager T = current.T self.s3 = self.manager.s3 self.ERROR = Storage( REPORTLAB_ERROR = T("ReportLab not installed"), GERALDO_ERROR = T("Geraldo not installed"), NO_RECORDS = T("No records in this resource"), XLWT_ERROR = T("Xlwt not installed"), ) self.xls = S3Codec.get_codec("xls").encode
def xls(self, *args, **kwargs): codec = S3Codec.get_codec("xls").encode return codec(*args, **kwargs)
def svg(self, *args, **kwargs): codec = S3Codec.get_codec("svg").encode return codec(*args, **kwargs)
def shp(self, *args, **kwargs): codec = S3Codec.get_codec("shp").encode return codec(*args, **kwargs)
def pdf(self, *args, **kwargs): codec = S3Codec.get_codec("pdf").encode return codec(*args, **kwargs)
def pdfcard(self, *args, **kwargs): codec = S3Codec.get_codec("card") return codec.encode(*args, **kwargs)
def pdf(self, *args, **kwargs): codec = S3Codec.get_codec("pdf").encode # args[0] = S3Request() return codec(*args, **kwargs)
def xls(self, *args, **kwargs): codec = S3Codec.get_codec("xls").encode # args[0] = S3Resource() return codec(*args, **kwargs)
def export_xls(self, r, **attr): """ Export nodes in the hierarchy in XLS format, including ancestor references. This is controlled by the hierarchy_export setting, which is a dict like: { "field": "name", - the field name for the ancestor reference "root": "Organisation" - the label for the root level "branch": "Branch" - the label for the branch levels "prefix": "Sub" - the prefix for the branch label } With this configuration, the ancestor columns would appear like: Organisation, Branch, SubBranch, SubSubBranch, SubSubSubBranch,... All parts of the setting can be omitted, the defaults are as follows: - "field" defaults to "name" - "root" is automatically generated from the resource name - "branch" defaults to prefix+root - "prefix" defaults to "Sub" @status: experimental """ resource = self.resource tablename = resource.tablename # Get the hierarchy h = S3Hierarchy(tablename=tablename) if not h.config: r.error(405, "No hierarchy configured for %s" % tablename) # Intepret the hierarchy_export setting for the resource setting = resource.get_config("hierarchy_export", {}) field = setting.get("field") if not field: field = "name" if "name" in resource.fields else resource._id.name prefix = setting.get("prefix", "Sub") root = setting.get("root") if not root: root = "".join(s.capitalize() for s in resource.name.split("_")) branch = setting.get("branch") if not branch: branch = "%s%s" % (prefix, root) rfield = resource.resolve_selector(field) # Get the list fields list_fields = resource.list_fields("export_fields", id_column=False) rfields = resource.resolve_selectors(list_fields, extra_fields=False)[0] # Selectors = the fields to extract selectors = [h.pkey.name, rfield.selector] # Columns = the keys for the XLS Codec to access the rows # Types = the data types of the columns (in same order!) columns = [] types = [] # Generate the headers and type list for XLS Codec headers = {} for rf in rfields: selectors.append(rf.selector) if rf.colname == rfield.colname: continue columns.append(rf.colname) headers[rf.colname] = rf.label if rf.ftype == "virtual": types.append("string") else: types.append(rf.ftype) # Get the root nodes if self.record_id: if r.component and h.pkey.name != resource._id.name: query = resource.table._id == self.record_id row = current.db(query).select(h.pkey, limitby=(0, 1)).first() if not row: r.error(404, current.ERROR.BAD_RECORD) roots = set([row[h.pkey]]) else: roots = set([self.record_id]) else: roots = h.roots # Find all child nodes all_nodes = h.findall(roots, inclusive=True) # ...and extract their data from a clone of the resource from s3query import FS query = FS(h.pkey.name).belongs(all_nodes) clone = current.s3db.resource(resource, filter=query) data = clone.select(selectors, represent=True, raw_data=True) # Convert into dict {hierarchy key: row} hkey = str(h.pkey) data_dict = dict((row._row[hkey], row) for row in data.rows) # Add hierarchy headers and types depth = max(h.depth(node_id) for node_id in roots) htypes = [] hcolumns = [] colprefix = "HIERARCHY" htype = "string" if rfield.ftype == "virtual" else rfield.ftype for level in xrange(depth + 1): col = "%s.%s" % (colprefix, level) if level == 0: headers[col] = root elif level == 1: headers[col] = branch else: headers[col] = "%s%s" % ("".join([prefix] * (level - 1)), branch) hcolumns.append(col) htypes.append(htype) # Generate the output for XLS Codec output = [headers, htypes + types] for node_id in roots: rows = h.export_node( node_id, prefix=colprefix, depth=depth, hcol=rfield.colname, columns=columns, data=data_dict ) output.extend(rows) # Encode in XLS format from s3codec import S3Codec codec = S3Codec.get_codec("xls") result = codec.encode(output, title=resource.name, list_fields=hcolumns + columns) # Reponse headers and file name are set in codec return result