def summary(self): """Renders a summary with aggregation in the end of the grid. Uses the meta attributes: * show_summary * summary_fields * summary_td_template * summary_tr_template """ fields = self._meta.fields or self.get_model_fields() qs = self.get_queryset() tsummary = [] for f_name in fields: if f_name in self._meta.summary_fields: val = self._meta.summary_fields[f_name](qs) else: val = ' ' if isinstance(val, decimal.Decimal): val = moneyformat(val, None, app_settings.THOUSANDS_SEPARATOR) tsummary.append(self._meta.summary_td_template % { 'field_name': f_name, 'value': val, }) return self._meta.summary_tr_template % { 'cells': ''.join(tsummary) + self.render_button_cell_summary(), }
def render_single_object(self, obj, tr_template=None, td_template=None): """Renders a single object. It is util internally or can be used for granulary customizations""" # Customized templates tr_template = tr_template or self._meta.tr_template td_template = td_template or self._meta.td_template fields = self._meta.fields or self.get_model_fields() row = [] for i, f_name in enumerate(fields): f_value = self.get_field_display_value(f_name, obj) f_value = self.get_linkable_field_value( obj, f_name, f_value, force=not self._meta.list_display_links and i == 0, ) if isinstance(f_value, decimal.Decimal): f_value = moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) if self._meta.show_if_none or f_value is not None: row.append(td_template%{'field_name': f_name, 'value': f_value}) else: row.append(td_template%{'field_name': f_name, 'value': ' '}) ret = tr_template%(''.join(row) + self.render_buttons_cell(obj)) return ret
def summary(self): """Renders a summary with aggregation in the end of the grid. Uses the meta attributes: * show_summary * summary_fields * summary_td_template * summary_tr_template """ fields = self._meta.fields or self.get_model_fields() qs = self.get_queryset() tsummary = [] for f_name in fields: if f_name in self._meta.summary_fields: val = self._meta.summary_fields[f_name](qs) else: val = " " if isinstance(val, decimal.Decimal): val = moneyformat(val, None, app_settings.THOUSANDS_SEPARATOR) tsummary.append(self._meta.summary_td_template % {"field_name": f_name, "value": val}) return self._meta.summary_tr_template % {"cells": "".join(tsummary) + self.render_button_cell_summary()}
def localize(f_value): if isinstance(f_value, datetime): return dateformat.format(f_value, datetime_format) if isinstance(f_value, time): return dateformat.time_format(f_value, time_format) if isinstance(f_value, date): return dateformat.format(f_value, date_format) if isinstance(f_value, decimal.Decimal): return moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) return value
def localize(f_value): if isinstance(f_value, datetime): return dateformat.format(f_value, datetime_format) if isinstance(f_value, time): return dateformat.time_format(f_value, time_format) if isinstance(f_value, date): return dateformat.format(f_value, date_format) if isinstance(f_value, decimal.Decimal): return moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) return value
def render(self, name, value, attrs=None): # Formats decimal value with period instead dot if moneyformat does it if isinstance(value, Decimal): value = moneyformat(value, self.decimal_places) if self.display is None: if self.object: method_name = 'get_%s_value' % name r = getattr(self.object, name) if hasattr(self.object, method_name): r = getattr(self.object, method_name)(r) else: r = value else: r = self.display try: display = getattr(self.object, 'get_%s_display' % name)() except AttributeError: if type(r) == datetime.date: value = display = format(r, settings.DATE_FORMAT) elif type(r) == datetime.datetime: value = display = format(r, settings.DATETIME_FORMAT) elif self.choices: display = dict(self.choices)[value] else: s = force_unicode(r, strings_only=False) display = truncatewords_html(linebreaksbr(s), 50) if isinstance(value, models.Model): value = value.pk # Avoid "None" value on front end display = display != 'None' and display or '' # Avoid None value value = value is not None and value or '' return mark_safe( '<span class="value read-only-widget">%s</span> <input type="hidden" name="%s" value="%s" id="%s">' % ( display, self.show_input and name or '', value, attrs.get('id', 'id_' + name), ))
def render(self, name, value, attrs=None): # Formats decimal value with period instead dot if moneyformat does it if isinstance(value, Decimal): value = moneyformat(value, self.decimal_places) if self.display is None: if self.object: method_name = 'get_%s_value'%name r = getattr(self.object, name) if hasattr(self.object, method_name): r = getattr(self.object, method_name)(r) else: r = value else: r = self.display try: display = getattr(self.object, 'get_%s_display'%name)() except AttributeError: if type(r) == datetime.date: value = display = format(r, settings.DATE_FORMAT) elif type(r) == datetime.datetime: value = display = format(r, settings.DATETIME_FORMAT) elif self.choices: display = dict(self.choices)[value] else: s = force_unicode(r, strings_only=False) display = truncatewords_html(linebreaksbr(s), 50) if isinstance(value, models.Model): value = value.pk # Avoid "None" value on front end display = display != 'None' and display or '' # Avoid None value value = value is not None and value or '' return mark_safe('<span class="value read-only-widget">%s</span> <input type="hidden" name="%s" value="%s" id="%s">'%( display, self.show_input and name or '', value, attrs.get('id', 'id_'+name), ))
def render_single_object(self, obj, tr_template=None, td_template=None): """Renders a single object. It is util internally or can be used for granulary customizations""" # Customized templates tr_template = tr_template or self._meta.tr_template td_template = td_template or self._meta.td_template fields = self._meta.fields or self.get_model_fields() row = [] for i, f_name in enumerate(fields): f_value = self.get_field_display_value(f_name, obj) f_value = self.get_linkable_field_value( obj, f_name, f_value, force=not self._meta.list_display_links and i == 0, ) if isinstance(f_value, decimal.Decimal): f_value = moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) if self._meta.show_if_none or f_value is not None: row.append(td_template % { 'field_name': f_name, 'value': f_value }) else: row.append(td_template % { 'field_name': f_name, 'value': ' ' }) tds = ''.join(row) + self.render_buttons_cell(obj) return self.format_tr_by_template(obj, tr_template, tds)
def summary(self): """Renders a summary with aggregation in the end of the grid. Uses the meta attributes: * show_summary * summary_fields * summary_td_template * summary_tr_template """ fields = self._meta.fields or self.get_model_fields() try: qs = self.queryset.all() except AttributeError: qs = self.queryset tsummary = [] for f_name in fields: if f_name in self._meta.summary_fields: val = self._meta.summary_fields[f_name](qs) else: val = ' ' if isinstance(val, decimal.Decimal): val = moneyformat(val, None, app_settings.THOUSANDS_SEPARATOR) tsummary.append(self._meta.summary_td_template%{ 'field_name': f_name, 'value': val, }) return self._meta.summary_tr_template%{ 'cells': ''.join(tsummary) + self.render_button_cell_summary(), }
return yesno(f_value) date_format, datetime_format, time_format = get_date_formats() if f_value: if isinstance(f_value, datetime): return dateformat.format(f_value, datetime_format) if isinstance(f_value, time): return dateformat.time_format(f_value, time_format) if isinstance(f_value, date): return dateformat.format(f_value, date_format) if isinstance(f_value, decimal.Decimal): return moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) if isinstance(f_value, models.Manager): return ', '.join(map(unicode, f_value.all())) if field and isinstance(field, models.TextField): if self._meta.auto_urlize: f_value = urlize(f_value) if self._meta.auto_linebreaks: f_value = linebreaksbr(f_value) return f_value def get_linkable_field_value(self, instance, f_name, f_value, force=False): url = '' if isinstance(instance, models.Model) and hasattr(instance, 'get_absolute_url'): url = instance.get_absolute_url()
return yesno(f_value) date_format, datetime_format, time_format = get_date_formats() if f_value: if isinstance(f_value, datetime): return dateformat.format(f_value, datetime_format) if isinstance(f_value, time): return dateformat.time_format(f_value, time_format) if isinstance(f_value, date): return dateformat.format(f_value, date_format) if isinstance(f_value, decimal.Decimal): return moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) if isinstance(f_value, models.Manager): return ', '.join(map(unicode, f_value.all())) if field and isinstance(field, models.TextField): if self._meta.auto_urlize: f_value = urlize(f_value) if self._meta.auto_linebreaks: f_value = linebreaksbr(f_value) return f_value def get_linkable_field_value(self, instance, f_name, f_value, force=False): url = '' if isinstance(instance, models.Model) and hasattr( instance, 'get_absolute_url'):