Beispiel #1
0
 def label_from_instance(self, obj):
     return label(obj)
Beispiel #2
0
 def label_from_instance(self, obj):
     return label(obj)
Beispiel #3
0
def display(field, value):
    """Returns html snippet appropriate for value and field"""
    try:
        value.last_name
        ##return ', '.join([value.last_name, value.first_name])
        return label(value)
    except AttributeError:
        if isinstance(field, models.ImageField):
            if value == '':
                return ''
            else:
                try:
                    return format_html(u'<A HREF="{0}"><IMG SRC="{1}" WIDTH="100"></A>',
                                       mark_safe(field.storage.url(value)),
                                       mark_safe(field.storage.url(value)))
                except NameError:
                    return mark_safe('<A HREF="' + field.storage.url(value) + '"><IMG SRC="' + field.storage.url(value) + '" WIDTH="100"></A>')
        elif isinstance(field, (models.ForeignKey, models.OneToOneField)):
            return displayLinkedData(value)
        elif isinstance(field, GenericForeignKey):
            if value is not None:
                return displayLinkedData(value)
            else:
                return value
        # elif isinstance(field, models.ManyToManyField):
        #     results = []
        #     if (len(value) > 100):
        #         for v in value[0:4]:
        #             results.append(displayLinkedData(field,v))
        #         results.append("...")
        #         for v in value[len(results)-4:len(results)]:
        #             results.append(displayLinkedData(field,v))

        #         results.append("("+str(len(value))+" records)")
        #     else:
        #         for v in value:
        #             results.append(displayLinkedData(field,v))
        #     return mark_safe(','.join(results))   
        elif isinstance(field, models.fields.files.FileField):
            if value.name:
                try:
                    return format_html(u'<A HREF="{0}">{1}</A>',mark_safe(field.storage.url(value)),value.name)
                except NameError:
                    return mark_safe(u'<A HREF="{0}">{1}</A>'.format(field.storage.url(value)),value.name)
            else:
                return ""
        elif isinstance(value, User):
            return ', '.join([value.last_name, value.first_name])
        elif isinstance(value, basestring):
            return value
        else:
            try:
                results = []
                try:
                    cnt = value.count()
                except (TypeError, AttributeError):
                    cnt = len(value)
                if (cnt > 100):
                    for v in value[0:4]:
                        results.append(displayLinkedData(v))
                    results.append("...")
#                    for v in value[len(results)-4:len(results)]:
#                       results.append(displayLinkedData(v))

                    results.append("("+str(len(value))+" records)")
                else:
                    for v in value:
                        results.append(displayLinkedData(v))
                return mark_safe(','.join(results))   
                # ##foo = ', '.join([displayLinkedData(v) for v in value])
                # foo = stringifyList(field,value)
                # return mark_safe(foo)
            except AttributeError:
                return "Error"
            except TypeError:
                return value
            except ValueError:
                return None