(core) Refactor Table.Record[Set] classes

Summary:
Dealing with some things that bothered and sometimes confused me:

Make Table.Record[Set] provide the table argument automatically
Remove the classes from UserTable because they're not used anywhere and the Table/UserTable distinction is already confusing. They're not documented for users and they don't show up in autocomplete.
Remove RecordSet.Record because it was confusing me where that attribute was being set, but also this means .Record will work properly for users with columns named 'Record'.

Test Plan: existing tests

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2913
This commit is contained in:
Alex Hall
2021-07-16 20:15:04 +02:00
parent 5b2666a88a
commit a9d5b4d5af
10 changed files with 48 additions and 26 deletions

View File

@@ -61,14 +61,19 @@ class UserTable(object):
self.Model = model_class
column_ids = {col for col in model_class.__dict__ if not col.startswith("_")}
column_ids.add('id')
self.Record = type('Record', (records.Record,), {})
self.RecordSet = type('RecordSet', (records.RecordSet,), {})
self.RecordSet.Record = self.Record
self.table = None
def _set_table_impl(self, table_impl):
self.table = table_impl
@property
def Record(self):
return self.table.Record
@property
def RecordSet(self):
return self.table.RecordSet
# Note these methods are named camelCase since they are a public interface exposed to formulas,
# and we decided camelCase was a more user-friendly choice for user-facing functions.
def lookupRecords(self, **field_value_pairs):
@@ -200,6 +205,18 @@ class Table(object):
# For a summary table, the name of the special helper column auto-added to the source table.
self._summary_helper_col_id = None
# Add Record and RecordSet subclasses which fill in this table as the first argument
class Record(records.Record):
def __init__(inner_self, *args, **kwargs): # pylint: disable=no-self-argument
super(Record, inner_self).__init__(self, *args, **kwargs)
class RecordSet(records.RecordSet):
def __init__(inner_self, *args, **kwargs): # pylint: disable=no-self-argument
super(RecordSet, inner_self).__init__(self, *args, **kwargs)
self.Record = Record
self.RecordSet = RecordSet
def _rebuild_model(self, user_table):
"""
Sets class-wide properties from a new Model class for the table (inner class within the table
@@ -207,8 +224,6 @@ class Table(object):
"""
self.user_table = user_table
self.Model = user_table.Model
self.Record = user_table.Record
self.RecordSet = user_table.RecordSet
new_cols = collections.OrderedDict()
new_cols['id'] = self._id_column
@@ -337,7 +352,7 @@ class Table(object):
row_ids = sorted(row_id_set, key=lambda r: self._get_col_value(sort_by, r, rel))
else:
row_ids = sorted(row_id_set)
return self.RecordSet(self, row_ids, rel, group_by=kwargs, sort_by=sort_by)
return self.RecordSet(row_ids, rel, group_by=kwargs, sort_by=sort_by)
def lookup_one_record(self, **kwargs):
return self.lookup_records(**kwargs).get_one()
@@ -416,7 +431,7 @@ class Table(object):
# user-actions caused by formula side-effects (e.g. as trigged by lookupOrAddDerived())
if row_id not in self.row_ids:
raise KeyError("'get_record' found no matching record")
return self.Record(self, row_id, None)
return self.Record(row_id, None)
def filter_records(self, **kwargs):
"""
@@ -427,7 +442,7 @@ class Table(object):
# See note in get_record() about using this call from formulas.
for row_id in self.filter(**kwargs):
yield self.Record(self, row_id, None)
yield self.Record(row_id, None)
# TODO: document everything here.