Пример #1
0
def tolist(qs, add_header=True, list_display=()):
    model = qs.model
    data = []
    fields = []
    header = []
    if type(list_display) == bool or not list_display:
        list_display = get_metadata(model, 'list_display', fields)
    if list_display:
        for field_name in list_display:
            header.append(get_fiendly_name(model, field_name))
            fields.append(field_name)
    else:
        for field in get_metadata(model, 'fields'):
            header.append(field.verbose_name)
            fields.append(field.name)
    if add_header:
        data.append(header)

    for obj in qs:
        row = []
        for field in fields:
            val = getattr2(obj, field)
            if callable(val):
                val = val()
            row.append(format_value(val, False))
        data.append(row)

    return data
Пример #2
0
 def _format_value(self, value):
     if value:
         if type(value) in [str, str]:
             return value
         else:
             return format_value(value)
     return ''
Пример #3
0
def format2(value, request=None):
    if value and hasattr(value, 'id') and hasattr(value, '_meta') and hasattr(value, 'fieldsets'):
        app = get_metadata(value.__class__, 'app_label')
        cls = value.__class__.__name__.lower()
        if request and request.user.has_perm('%s.%s' % (app, cls)):
            return link(value)
    return mark_safe(format_value(value).replace('\n', '<br />'))
Пример #4
0
def serialize(attr_value, recursive=False):
    if isinstance(attr_value, types.MethodType) or isinstance(
            attr_value, types.FunctionType):
        attr_value = attr_value()
    if isinstance(attr_value, QuerySet) or hasattr(attr_value, 'all'):
        serialized_value = []
        for obj in attr_value.all():
            serialized_value.append(serialize(obj))
    elif isinstance(attr_value, Model):
        obj = attr_value
        obj_dict = dict(id=obj.pk)
        if recursive:
            obj_dict['text'] = str(obj)
        else:
            for attr_name in get_metadata(type(attr_value), 'list_display'):
                obj_dict[attr_name] = serialize(getattr(obj, attr_name),
                                                recursive=True)
        serialized_value = obj_dict
    elif type(attr_value).__name__ == 'QueryStatistics':
        query_statistics_table = attr_value.as_table()
        serialized_value = dict(header=query_statistics_table.header,
                                rows=query_statistics_table.rows,
                                footer=query_statistics_table.footer)
    else:
        serialized_value = format_value(attr_value, False)
    return serialized_value
Пример #5
0
def format2(value, request=None):
    if value and hasattr(value, 'id') and hasattr(value, '_meta') and hasattr(
            value, 'fieldsets'):
        app = get_metadata(value.__class__, 'app_label')
        cls = value.__class__.__name__.lower()
        if request and request.user.has_perm('{}.{}'.format(app, cls)):
            return link(value)
    return formatter.format_value(value)
Пример #6
0
 def _format_value(self, value):
     if type(value) in [unicode, str]:
         return value
     else:
         return format_value(value)
Пример #7
0
def print_format(value):
    return mark_safe(format_value(value).replace('\n', '<br />'))