def _get_new_field_html(self, field_name): try: f, attr, value = lookup_field(field_name, self.org_obj, self) except (AttributeError, ObjectDoesNotExist): return EMPTY_CHANGELIST_VALUE else: allow_tags = False if f is None: allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True text = boolean_icon(value) else: text = smart_unicode(value) else: if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(self.org_obj, f.name) if field_val is None: text = EMPTY_CHANGELIST_VALUE else: text = field_val else: text = display_for_field(value, f) return mark_safe(text) if allow_tags else conditional_escape(text)
def init(self): self.label = label_for_field(self.field_name, self.obj.__class__, model_admin = self.admin_view, return_attr = False ) try: f, attr, value = lookup_field(self.field_name, self.obj, self.admin_view) except (AttributeError, ObjectDoesNotExist): self.text else: if f is None: self.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: self.allow_tags = True self.text = boolean_icon(value) else: self.text = smart_unicode(value) else: if isinstance(f.rel, models.ManyToOneRel): self.text = getattr(self.obj, f.name) else: self.text = display_for_field(value, f) self.field = f self.attr = attr self.value = value
def result_item(self, obj, field_name, row): """ Generates the actual list of data. """ item = ResultItem(field_name, row) try: f, attr, value = lookup_field(field_name, obj, self) except (AttributeError, ObjectDoesNotExist): item.text = EMPTY_CHANGELIST_VALUE else: if f is None: item.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: item.allow_tags = True item.text = boolean_icon(value) else: item.text = smart_unicode(value) else: if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(obj, f.name) if field_val is None: item.text = EMPTY_CHANGELIST_VALUE else: item.text = field_val else: item.text = display_for_field(value, f) if isinstance(f, models.DateField)\ or isinstance(f, models.TimeField)\ or isinstance(f, models.ForeignKey): item.classes.append('nowrap') item.field = f item.attr = attr item.value = value # If list_display_links not defined, add the link tag to the first field if (item.row['is_display_first'] and not self.list_display_links) \ or field_name in self.list_display_links: url = self.url_for_result(obj) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if self.to_field: attr = str(self.to_field) else: attr = self.opts.pk.attname value = obj.serializable_value(attr) result_id = repr(force_unicode(value))[1:] item.row['is_display_first'] = False item.wraps.append(u'<a href="%s"%s>%%s</a>' % \ (url, (self.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''))) return item
def get(self, request, name): if not self.data_charts.has_key(name): return HttpResponseNotFound() self.chart = self.data_charts[name] self.x_field = self.chart['x-field'] y_fields = self.chart['y-field'] self.y_fields = (y_fields,) if type(y_fields) not in (list, tuple) else y_fields datas = [{"data":[], "label": label_for_field(i, self.model, model_admin=self)} for i in self.y_fields] self.make_result_list() for obj in self.result_list: xf, attrs, value = lookup_field(self.x_field, obj, self) for i, yfname in enumerate(self.y_fields): yf, yattrs, yv = lookup_field(yfname, obj, self) datas[i]["data"].append((value, yv)) option = {'series': {'lines': { 'show': True }, 'points': { 'show': False }}, 'grid': { 'hoverable': True, 'clickable': True }} try: xfield = self.opts.get_field(self.x_field) if type(xfield) in (models.DateTimeField, models.DateField, models.TimeField): option['xaxis'] = { 'mode': "time", 'tickLength': 5} if type(xfield) is models.DateField: option['xaxis']['timeformat'] = "%y/%m/%d"; elif type(xfield) is models.TimeField: option['xaxis']['timeformat'] = "%H:%M:%S"; else: option['xaxis']['timeformat'] = "%y/%m/%d %H:%M:%S"; except Exception: pass option.update(self.chart.get('option', {})) content = {'data': datas, 'option': option} json = simplejson.dumps(content, cls=JSONEncoder, ensure_ascii=False) return HttpResponse(json)
def result_item(self, obj, field_name, row): """ Generates the actual list of data. """ item = ResultItem(field_name, row) try: f, attr, value = lookup_field(field_name, obj, self) except (AttributeError, ObjectDoesNotExist): item.text = EMPTY_CHANGELIST_VALUE else: if f is None: item.allow_tags = getattr(attr, "allow_tags", False) boolean = getattr(attr, "boolean", False) if boolean: item.allow_tags = True item.text = boolean_icon(value) else: item.text = smart_unicode(value) else: if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(obj, f.name) if field_val is None: item.text = EMPTY_CHANGELIST_VALUE else: item.text = field_val else: item.text = display_for_field(value, f) if ( isinstance(f, models.DateField) or isinstance(f, models.TimeField) or isinstance(f, models.ForeignKey) ): item.classes.append("nowrap") item.field = f item.attr = attr item.value = value # If list_display_links not defined, add the link tag to the first field if (item.row["is_display_first"] and not self.list_display_links) or field_name in self.list_display_links: url = self.url_for_result(obj) item.row["is_display_first"] = False item.wraps.append(u'<a href="%s">%%s</a>' % url) return item