class NameColumn(Column): """Name column.""" header = _("Name") def render_cell(self, item): return html.escape(get_name(item))
class EMailColumn(LinkColumn, GetAttrColumn): """Column to display mailto links""" header = _("E-Mail") attr_name = None # attribute name which contains the mail address default_value = "" # value which is rendered when no value is found link_content = None def get_link_url(self, item): """Get item URL""" return "mailto:%s" % self.get_value(item) def get_link_content(self, item): """Get link content""" if self.link_content: translate = self.request.localizer.translate return translate(self.link_content) return self.get_value(item) def render_cell(self, item): """Render column cell""" value = self.get_value(item) if value is self.default_value or value is None: return self.default_value return super(EMailColumn, self).render_cell(item)
def render(self): table = self.table prefix = table.prefix col_id = self.column.id # this may return a string 'id-name-idx' if coming from request, # otherwise in Table class it is intialised as a integer string current_sort_id = table.get_sort_on() try: current_sort_id = int(current_sort_id) except ValueError: current_sort_id = current_sort_id.rsplit("-", 1)[-1] current_sort_order = table.get_sort_order() sort_id = col_id.rsplit("-", 1)[-1] sort_order = table.sort_order if int(sort_id) == int(current_sort_id): # ordering the same column so we want to reverse the order if current_sort_order in table.reverse_sort_order_names: sort_order = "ascending" elif current_sort_order == "ascending": sort_order = table.reverse_sort_order_names[0] args = self.get_query_string_args() args.update({ "%s-sort-on" % prefix: col_id, "%s-sort-order" % prefix: sort_order }) query_string = "?%s" % (urlencode(sorted(args.items()))) translate = self.request.localizer.translate return '<a href="%s" title="%s">%s</a>' % ( query_string, translate(_("Sort")), translate(self.column.header))
class CheckBoxColumn(Column): """Checkbox column.""" header = _("X") weight = 10 @property def selected_items(self): """Get list of selected items""" return self.table.selected_items @selected_items.setter def selected_items(self, values): """Set values of selected items""" self.table.selected_items = values def get_sort_key(self, item): """Get item sort key""" return get_name(item) def get_item_key(self, item): # pylint: disable=unused-argument """Get item key""" return "%s-selected-items" % self.id def get_item_value(self, item): # pylint: disable=no-self-use """Get item value""" return get_name(item) def is_selected(self, item): """Test if item is selected""" value = self.request.params.get(self.get_item_key(item), []) value = ensure_list(value) if self.get_item_value(item) in value: return True return False def update(self): """Update column""" self.selected_items = [ item for item in self.table.values if self.is_selected(item) ] def render_cell(self, item): """Render column cell""" selected = "" if item in self.selected_items: selected = ' checked="checked"' return '<input type="checkbox" class="{}" name="{}" value="{}"{} />'.format( "checkbox-widget", html.escape(self.get_item_key(item)), html.escape(self.get_item_value(item)), selected)
class RadioColumn(Column): """Radio column.""" header = _("X") @property def selected_item(self): """Get selected item""" items = self.table.selected_items if len(items) > 0: return list(items).pop() return None @selected_item.setter def selected_item(self, value): """Set selected item value""" self.table.selected_items = [value] def get_sort_key(self, item): """Get item sort key""" return get_name(item) def get_item_key(self, item): # pylint: disable=unused-argument """Get item key""" return "%s-selected-item" % self.id @staticmethod def get_item_value(item): """Get item value""" return get_name(item) def update(self): """Column update""" items = [ item for item in self.table.values if self.get_item_value(item) in self.request.params.get( self.get_item_key(item), []) ] if len(items) > 0: self.selected_item = items.pop() def render_cell(self, item): """Render column cell""" selected = "" if item == self.selected_item: selected = ' checked="checked"' return '<input type="radio" class="%s" name="%s" value="%s"%s />' % ( "radio-widget", html.escape(self.get_item_key(item)), html.escape(self.get_item_value(item)), selected)
class ModifiedColumn(FormatterColumn, GetAttrColumn): """Created date column.""" header = _("Modified") weight = 110 attr_name = "modified" def render_cell(self, item): """Render column cell""" zdc = IZopeDublinCore(item, None) if zdc is not None: value = self.get_value(zdc) if value: value = self.get_formatted_value(value) return value return ''
class LinkColumn(Column): """Link column""" header = _("Name") link_name = None link_target = None link_content = None link_css = None link_title = None def get_link_url(self, item): """Get link URL""" if self.link_name is not None: return "%s/%s" % (absolute_url(item, self.request), self.link_name) return absolute_url(item, self.request) def get_link_css(self, item): # pylint: disable=unused-argument """Setup link CSS""" return ' class="%s"' % self.link_css if self.link_css else "" def get_link_title(self, item): # pylint: disable=unused-argument """Get link title""" return ' title="%s"' % html.escape( self.link_title) if self.link_title else "" def get_link_target(self, item): # pylint: disable=unused-argument """Get link target""" return ' target="%s"' % self.link_target if self.link_target else "" def get_link_content(self, item): """Get link content""" if self.link_content: translate = self.request.localizer.translate return translate(self.link_content) return get_name(item) def render_cell(self, item): """Render column cell""" # setup a tag return '<a href="%s"%s%s%s>%s</a>' % ( html.escape(self.get_link_url(item)), self.get_link_target(item), self.get_link_css(item), self.get_link_title(item), html.escape(self.get_link_content(item)))