def getAttributeFields(model, related_name_prefix=None, initially_hidden=False): ''' Return report fields for all attributes of a given model. ''' from freppledb.common.report import GridFieldText, GridFieldBool, GridFieldNumber from freppledb.common.report import GridFieldInteger, GridFieldDate, GridFieldDateTime from freppledb.common.report import GridFieldDuration, GridFieldTime result = [] for field_name, label, fieldtype in _register.get("%s.%s" % (model.__module__, model.__name__), []): if related_name_prefix: field_name = "%s__%s" % (related_name_prefix, field_name) label = "%s - %s" % (related_name_prefix.split('__')[-1], label) else: label = "%s - %s" % (model.__name__, label) if fieldtype == 'string': result.append( GridFieldText(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'boolean': result.append( GridFieldBool(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'number': result.append( GridFieldNumber(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'integer': result.append( GridFieldInteger(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'date': result.append( GridFieldDate(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'datetime': result.append( GridFieldDateTime(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'duration': result.append( GridFieldDuration(field_name, title=label, initially_hidden=initially_hidden) ) elif fieldtype == 'time': result.append( GridFieldTime(field_name, title=label, initially_hidden=initially_hidden) ) else: raise Exception("Invalid attribute type '%s'." % fieldtype) return result
def rows(self, request, *args, **kwargs): cols = [] if args: for c in ( SQLColumn.objects.using(request.database) .filter(report=args[0]) .order_by("sequence") ): if c.format == "number": cols.append(GridFieldNumber(_(c.name), editable=False)) elif c.format == "datetime": cols.append(GridFieldDateTime(_(c.name), editable=False)) elif c.format == "date": cols.append(GridFieldDate(_(c.name), editable=False)) elif c.format == "integer": cols.append(GridFieldInteger(_(c.name), editable=False)) elif c.format == "duration": cols.append(GridFieldDuration(_(c.name), editable=False)) elif c.format == "text": cols.append(GridFieldText(_(c.name), editable=False)) elif c.format == "character": cols.append(GridFieldText(_(c.name), editable=False)) elif c.format == "bool": cols.append(GridFieldBool(_(c.name), editable=False)) elif c.format == "currency": cols.append(GridFieldCurrency(_(c.name), editable=False)) return cols
def getAttributeFields(model, related_name_prefix=None, initially_hidden=False): """ Return report fields for all attributes of a given model. """ from freppledb.common.report import GridFieldText, GridFieldBool, GridFieldNumber from freppledb.common.report import ( GridFieldInteger, GridFieldDate, GridFieldDateTime, ) from freppledb.common.report import GridFieldDuration, GridFieldTime result = [] for field_name, label, fieldtype, editable, hidden in getAttributes(model): if related_name_prefix: field_name = "%s__%s" % (related_name_prefix, field_name) label = "%s - %s" % (related_name_prefix.split("__")[-1], label) else: label = "%s - %s" % (model.__name__, label) if fieldtype == "string": result.append( GridFieldText( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "boolean": result.append( GridFieldBool( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "number": result.append( GridFieldNumber( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "integer": result.append( GridFieldInteger( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "date": result.append( GridFieldDate( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "datetime": result.append( GridFieldDateTime( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "duration": result.append( GridFieldDuration( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "time": result.append( GridFieldTime( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) elif fieldtype == "jsonb": result.append( GridFieldText( field_name, title=label, initially_hidden=hidden or initially_hidden, editable=editable, )) else: raise Exception("Invalid attribute type '%s'." % fieldtype) return result