def init(self): if not self.adapter: self.adapter = ObjectSqlAdapter(self.app, self.cls) super(ObjectTable, self).init() for this, that, fobj in self.filter_specs: self.adapter.add_value_filter(this) for column, vattr, type in self.like_specs: self.adapter.add_like_filter(column.attr) if self.sort.default is None: for col in self.columns: if col.sortable: self.set_default_sort_column(col) break # Now that all columns have been initialized, and we # have a sortable column if possible, set direction self.set_default_sort_direction()
class ObjectTable(DataTable): def __init__(self, app, name, cls): super(ObjectTable, self).__init__(app, name) assert isinstance(cls, RosemaryClass), cls self.cls = cls self.update_enabled = True # (RosemaryAttribute this, RosemaryAttribute that, Attribute object) self.filter_specs = list() # ((RosemaryAttribute this, Attribute value, SqlLikeFilter.CONTAINS) self.like_specs = list() self.export = None def init(self): if not self.adapter: self.adapter = ObjectSqlAdapter(self.app, self.cls) super(ObjectTable, self).init() for this, that, fobj in self.filter_specs: self.adapter.add_value_filter(this) for column, vattr, type in self.like_specs: self.adapter.add_like_filter(column.attr) if self.sort.default is None: for col in self.columns: if col.sortable: self.set_default_sort_column(col) break # Now that all columns have been initialized, and we # have a sortable column if possible, set direction self.set_default_sort_direction() def add_attribute_column(self, attr): assert isinstance(attr, RosemaryAttribute), attr col = ObjectTableColumn(self.app, attr.name, attr) self.add_column(col) return col def add_filter(self, attribute, this, that=None): if not that: that = this assert isinstance(attribute, Attribute), attribute assert isinstance(this, RosemaryAttribute), this assert isinstance(that, RosemaryAttribute), that self.filter_specs.append((this, that, attribute)) def add_reference_filter(self, attribute, ref): assert isinstance(ref, RosemaryReference), ref this = ref that = ref.that_cls._id self.add_filter(attribute, this, that) def add_like_filter(self, attribute, column, type=SqlLikeFilter.CONTAINS): assert isinstance(attribute, Attribute), attribute # assert isinstance(column, DataTableColumn), column self.like_specs.append((column, attribute, type)) def get_data_values(self, session): values = dict() for this, that, fobj in self.filter_specs: obj = fobj.get(session) values[this.name] = getattr(obj, that.name) for this, vattr, type in self.like_specs: value = vattr.get(session) # if the user typed a %, don't add another pre = "" post = "%" if not "%" in value: pre = type == SqlLikeFilter.CONTAINS and "%%" or "" else: post = "" values[this.attr.name] = "%s%s%s" % (pre, value, post) return values def render_title(self, session): return "%ss" % self.cls._title def render_footer_options(self, session): if self.export: return self.export.render(session)