Skip to content

Commit

Permalink
experimental aggregate columns in Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Jul 13, 2024
1 parent 7e93aa4 commit 6ede2d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
49 changes: 16 additions & 33 deletions py4web/utils/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion py4web/utils/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down

0 comments on commit 6ede2d4

Please sign in to comment.