コード例 #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
ファイル: __init__.py プロジェクト: adelsonllima/djangoplus
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
ファイル: __init__.py プロジェクト: allysonbarros/djangoplus
def print_format(value):
    return mark_safe(format_value(value).replace('\n', '<br />'))