From 6ede2d4cb52d852fe8da91521ad2c428d0a5f8a1 Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Sat, 13 Jul 2024 11:45:59 -0700 Subject: [PATCH] experimental aggregate columns in Grid --- py4web/utils/grid.py | 49 +++++++++++++--------------------------- py4web/utils/populate.py | 2 +- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/py4web/utils/grid.py b/py4web/utils/grid.py index 762150520..4a78335f4 100644 --- a/py4web/utils/grid.py +++ b/py4web/utils/grid.py @@ -657,47 +657,28 @@ def process(self): # if no column specified use all fields self.param.columns = [field for field in table if field.readable] - # if any columns are Expression but not Field, get the field info from 'first' attribute - converted_columns = [] - for col in self.param.columns: - if isinstance(col, Expression) and not isinstance(col, Field): - converted_columns.append(col.first) - else: - converted_columns.append(col) - self.param.columns = converted_columns - if not self.param.columns: self.needed_fields = self.param.columns[:] - elif any(isinstance(col, Column) for col in self.param.columns): - # if we use columns we have to get all fields and assume a single table - self.needed_fields = [field for field in db[self.tablename]] - for col in self.param.columns: - if isinstance(col, Column): - for rf in col.required_fields: - if rf.longname not in [x.longname for x in self.needed_fields]: - self.needed_fields.append(rf) - elif any(isinstance(col, FieldVirtual) for col in self.param.columns): - # if virtual fields are specified the fields may come from a join + else: needed_fields = set() - for col in self.param.columns: - if isinstance(col, Field): - needed_fields.add(col) + for col in self.param.columns: + print("Column", col) + if isinstance(col, Column): + if col.required_fields: + needed_fields |= set(col.required_fields) + else: + needed_fields |= set(db[self.tablename]) elif isinstance(col, FieldVirtual): - for field in db[col.tablename]: - needed_fields.add(field) + # if virtual fields are specified the fields may come from a join + needed_fields |= set(db[col.tablename]) + else: + needed_fields.add(col) self.needed_fields = list(needed_fields) - else: - self.needed_fields = self.param.columns[:] - # make sure all specified fields are available - if self.param.columns: - for col in self.param.columns: - if not isinstance(col, (Column, FieldVirtual)): - if col.longname not in [x.longname for x in self.needed_fields]: - self.needed_fields.append(col) + print(self.needed_fields) # except the primary key may be missing and must be fetched even if not displayed - if not any(col.name == table._id.name for col in self.needed_fields): + if not any(getattr(col, "name", None) == table._id.name for col in self.needed_fields): self.needed_fields.insert(0, table._id) self.referrer = None @@ -855,6 +836,8 @@ def process(self): self.page_end = self.total_number_of_rows # get the data + print(self.needed_fields) + print(select_params) self.rows = db(query).select(*self.needed_fields, **select_params) self.number_of_pages = self.total_number_of_rows // self.param.rows_per_page diff --git a/py4web/utils/populate.py b/py4web/utils/populate.py index 09cb0b967..d9112c370 100644 --- a/py4web/utils/populate.py +++ b/py4web/utils/populate.py @@ -280,7 +280,7 @@ def populate_generator(table, default=True, compute=False, contents=None, ell=No record[fieldname] = random.randint(2000, 2013) else: record[fieldname] = random.randint(0, 1000) - elif field.type == "double" or str(field.type).startswith("decimal"): + elif field.type in ("float", "double") or str(field.type).startswith("decimal"): if hasattr(field.requires, "minimum"): rand = random.random() if str(field.type).startswith("decimal"):