def _select_method(methods, form_values=None, **attr): """ Returns a SELECT of aggregation methods @param methods: list of methods to show @param attr: the HTML attributes for the SELECT """ supported_methods = S3Report.METHODS if methods: methods = [(m, supported_methods[m]) for m in methods if m in supported_methods] else: methods = supported_methods.items() name = attr["_name"] if form_values: value = form_values[name] else: value = None options = [] for method, label in methods: options.append((method, label)) dummy_field = Storage(name=name, requires=IS_IN_SET(options)) return OptionsWidget.widget(dummy_field, value, **attr)
def _select_field(self, list_fields, form_values=None, **attr): """ Returns a SELECT of field names @param list_fields: the fields to include in the options list @param attr: the HTML attributes for the SELECT """ name = attr["_name"] if form_values: value = form_values.get(name, "") else: value = "" table = self.table lfields, joins, left, distinct = self.resource.resolve_selectors(list_fields) options = [] for f in lfields: if (f.field is None or f.field.name != table._id.name) and f.show: options.append((f.selector, f.label)) dummy_field = Storage(name=name, requires=IS_IN_SET(options)) return OptionsWidget.widget(dummy_field, value, **attr)
def _field_select(self, name, options=None, form_values=None, **attr): """ Returns a SELECT of field names @param name: the element name @param options: the report options @param form_values: the form values to populate the widget @param attr: the HTML attributes for the widget """ resource = self.resource if options and name in options: fields = options[name] else: fields = self._config("list_fields", None) if not fields: fields = [f.name for f in resource.readable_fields()] prefix = lambda v: "%s.%s" % (resource.alias, v) \ if "." not in v.split("$", 1)[0] \ else v.replace("~", resource.alias) attr = Storage(attr) if "_name" not in attr: attr["_name"] = name if "_id" not in attr: attr["_id"] = "report-%s" % name if form_values: value = form_values.get(name, "") else: value = "" if value: value = prefix(value) table = self.table rfields, j, l, d = resource.resolve_selectors(fields) opts = [ (prefix(f.selector), f.label) for f in rfields if f.show and (f.field is None or f.field.name != table._id.name) ] dummy_field = Storage(name=name, requires=IS_IN_SET(opts)) return OptionsWidget.widget(dummy_field, value, **attr)
def _method_select(self, name, options, form_values=None, **attr): """ Returns a SELECT of (field:method) options @param name: the element name @param options: the report options @param form_values: the form values to populate the widget @param attr: the HTML attributes for the widget """ T = current.T RECORDS = T("Records") if "methods" in options: all_methods = options["methods"] else: all_methods = S3Report.METHODS resource = self.resource if options and name in options: methods = options[name] else: methods = self._config("list_fields", None) if not methods: methods = [f.name for f in resource.readable_fields()] prefix = lambda v: "%s.%s" % (resource.alias, v) \ if "." not in v.split("$", 1)[0] \ else v.replace("~", resource.alias) attr = Storage(attr) if "_name" not in attr: attr["_name"] = name if "_id" not in attr: attr["_id"] = "report-%s" % name if form_values: value = form_values.get(name, "") else: value = "" if value: value = prefix(value) # Backward-compatiblity: render aggregate if given if "aggregate" in form_values and ":" not in value: value = "%s:%s" % (form_values["aggregate"], value) # Resolve selectors, add method options opts = [] for o in methods: if type(o) is tuple and isinstance(o[0], lazyT): # Backward compatibility opt = [o] else: opt = list(o) if isinstance(o, (tuple, list)) else [o] s = opt[0] if isinstance(s, tuple): label, selector = s else: label, selector = None, s selector = prefix(selector) rfield = resource.resolve_selector(selector) if not rfield.field and not rfield.virtual: continue if label is not None: rfield.label = label else: label = rfield.label if rfield.label != "Id" else RECORDS if len(opt) == 1: is_amount = None # Only field given -> auto-detect aggregation methods ftype = rfield.ftype if ftype == "integer": is_amount = True requires = rfield.requires if not isinstance(requires, (list, tuple)): requires = [requires] for r in requires: if isinstance(r, IS_IN_SET) or \ isinstance(r, IS_EMPTY_OR) and \ isinstance(r.other, IS_IN_SET): is_amount = False elif ftype == "double": is_amount = True elif ftype[:9] == "reference" or \ ftype[:5] == "list:" or \ ftype in ("id", "string", "text"): is_amount = False if ftype in ("datetime", "date", "time"): mopts = ["min", "max", "list"] elif is_amount is None: mopts = ["sum", "min", "max", "avg", "count", "list"] elif is_amount: mopts = ["sum", "min", "max", "avg"] else: mopts = ["count", "list"] opts.extend([(rfield, opt[0], m) for m in mopts if m in all_methods]) else: opt.insert(0, rfield) opts.append(opt) # Construct missing labels _methods = [] for opt in opts: if len(opt) == 3: # field+method -> construct label mlabel = self.mname(opt[2]) flabel = opt[0].label if opt[0].label != "Id" else RECORDS label = T("%s (%s)") % (flabel, mlabel) _methods.append((opt[0].selector, opt[2], label)) else: _methods.append((opt[0].selector, opt[2], opt[3])) # Build the widget opts = [("%s:%s" % (m[1], m[0]), m[2]) for m in _methods] if len(opts) == 1: opt = opts[0] return opt[1], {name: opt[0]} dummy_field = Storage(name=name, requires=IS_IN_SET(opts)) return OptionsWidget.widget(dummy_field, value, **attr), {}