예제 #1
0
파일: reports.py 프로젝트: 5n1p/treeio
def is_field_number(report, field_name):
    model = loads(report.model)

    classobj = model.get_class_object()
    field = classobj._meta.get_field_by_name(field_name)[0]

    if number_field_regex.match(field.get_internal_type()):
        return True
    return False
예제 #2
0
def is_field_number(report, field_name):
    model = loads(report.model)

    classobj = model.get_class_object()
    field = classobj._meta.get_field_by_name(field_name)[0]

    if number_field_regex.match(field.get_internal_type()):
        return True
    return False
예제 #3
0
파일: views.py 프로젝트: 5n1p/treeio
def _get_report_content(report, request=None):
    model = loads(report.model)

    object = model.name
    object = object.split('.')

    module_name = object[0] + '.' + object[1] + '.' + object[2]
    import_name = object[3]

    module = __import__(module_name, globals(), locals(), [import_name], -1)
    classobj = getattr(module, import_name)

    if request:
        unfiltered_set = Object.filter_by_request(request, classobj.objects)
    else:
        unfiltered_set = classobj.objects.exclude(trash=True)

    # construct filter
    filters = {}
    excludes = {}
    for field in model.fields:
        for filter in field.filters:
            if filter['operand'] == 'is':
                filters.setdefault(field.name + '__in', []).append(filter['choice'])
            elif filter['operand'] == 'not':
                excludes.setdefault(field.name + '__in', []).append(filter['choice'])
            elif filter['operand'] == 'beforedate':
                filters[field.name + '__gte'] = datetime.date(datetime.strptime(filter['choice'], '%m/%d/%Y'))
            elif filter['operand'] == 'afterdate':
                filters[field.name + '__lte'] = datetime.date(datetime.strptime(filter['choice'], '%m/%d/%Y'))
            elif filter['operand'] == 'beforedatetime':
                filters[field.name + '__gte'] = datetime.strptime(filter['choice'], '%m/%d/%Y %H:%M')
            elif filter['operand'] == 'afterdatetime':
                filters[field.name + '__lte'] = datetime.strptime(filter['choice'], '%m/%d/%Y %H:%M')
            elif filter['operand'] == 'on':
                filters.setdefault(field.name + '__in', []).append(datetime.strptime(filter['choice'], '%m/%d/%Y'))

    set = unfiltered_set.filter(**filters).exclude(**excludes)

    # Check for group
    groupname = None
    groups = None
    for field in model.fields:
        if field.groupby == 1:
            groupname = field.name

    if groupname:
        xfield = classobj._meta.get_field_by_name(groupname)[0]
        xtype = xfield.get_internal_type()
        if xtype == 'ManyToManyField':
            set = sorted(set, key=lambda item: (
                ", ".join([unicode(i) for i in getattr(item, groupname).all()])), reverse=True)
            groups, groupnames = [], []
            for obj in set:
                for n in getattr(obj, groupname).all():
                    if n not in groupnames:
                        groupnames.append(n)
            for n in groupnames:
                l = []
                for obj in set:
                    if n in getattr(obj, groupname).all():
                        l.append(obj)
                groups.append((unicode(n), l))

        elif xtype == ('DateTimeField' or 'DateField'):
            set = set.order_by(groupname)
            #set = sorted(set, key = lambda item: getattr(item,groupname))
            # TODO: Fix this sort
            groups, groupnames, l, ng = [], [], [], []
            n = None

            if xtype == 'DateTimeField':
                def dt(ob):
                    return getattr(ob, groupname).date()
            else:
                def dt(ob):
                    return getattr(ob, groupname)

            for x in set:
                n = dt(x)
                if n:
                    break
            if n:
                for obj in set:
                    if getattr(obj, groupname):
                        if dt(obj) == n:
                            l.append(obj)
                        else:
                            groups.append((unicode(n), l))
                            l = []
                            n = dt(obj)
                            l.append(obj)
                    else:
                        ng.append(obj)
                if ng:
                    groups.append(('None', ng))

        else:
            set = sorted(set, key=lambda item: unicode(
                item.get_field_value(groupname)), reverse=True)
            groups = []
            for g, ks in groupby(set, key=lambda item: unicode(item.get_field_value(groupname))):
                groups.append((g, list(ks)))
            xfield = set[0]._meta.get_field_by_name(groupname)[0]

    # Count aggregate functions
    agg_funcs = {}
    for field in model.fields:  # get fields and aggregate functions for them
        if field.display and getattr(field, 'aggregation', None):
            xfield = classobj._meta.get_field_by_name(field.name)[0]
            if number_field_regex.match(xfield.get_internal_type()) \
                    and aggregate_functions.has_key(field.aggregation):
                agg_funcs[field.name] = aggregate_functions[
                    field.aggregation]['function']

    aggregations = {}
    if agg_funcs:
        for grouper, ls in groups if groups else (('set', set),):
            data = {}
            for s in ls:
                for key in agg_funcs:
                    data.setdefault(key, []).append(getattr(s, key, 0))
            aggrs = {}
            for key, func in agg_funcs.items():
                aggrs[key] = func(data.get(key, [0, ]))
            aggregations[grouper] = aggrs

    return {'model': model,
            'set': set,
            'groups': groups,
            'groupname': groupname,
            'aggregations': aggregations}
예제 #4
0
def _get_report_content(report, request=None):
    model = loads(report.model)

    object = model.name
    object = object.split('.')

    module_name = object[0] + '.' + object[1] + '.' + object[2]
    import_name = object[3]

    module = __import__(module_name, globals(), locals(), [import_name], -1)
    classobj = getattr(module, import_name)

    if request:
        unfiltered_set = Object.filter_by_request(request, classobj.objects)
    else:
        unfiltered_set = classobj.objects.exclude(trash=True)

    # construct filter
    filters = {}
    excludes = {}
    for field in model.fields:
        for filter in field.filters:
            if filter['operand'] == 'is':
                filters.setdefault(field.name + '__in',
                                   []).append(filter['choice'])
            elif filter['operand'] == 'not':
                excludes.setdefault(field.name + '__in',
                                    []).append(filter['choice'])
            elif filter['operand'] == 'beforedate':
                filters[field.name + '__gte'] = datetime.date(
                    datetime.strptime(filter['choice'], '%m/%d/%Y'))
            elif filter['operand'] == 'afterdate':
                filters[field.name + '__lte'] = datetime.date(
                    datetime.strptime(filter['choice'], '%m/%d/%Y'))
            elif filter['operand'] == 'beforedatetime':
                filters[field.name + '__gte'] = datetime.strptime(
                    filter['choice'], '%m/%d/%Y %H:%M')
            elif filter['operand'] == 'afterdatetime':
                filters[field.name + '__lte'] = datetime.strptime(
                    filter['choice'], '%m/%d/%Y %H:%M')
            elif filter['operand'] == 'on':
                filters.setdefault(field.name + '__in', []).append(
                    datetime.strptime(filter['choice'], '%m/%d/%Y'))

    set = unfiltered_set.filter(**filters).exclude(**excludes)

    # Check for group
    groupname = None
    groups = None
    for field in model.fields:
        if field.groupby == 1:
            groupname = field.name

    if groupname:
        xfield = classobj._meta.get_field_by_name(groupname)[0]
        xtype = xfield.get_internal_type()
        if xtype == 'ManyToManyField':
            set = sorted(
                set,
                key=lambda item: (", ".join(
                    [unicode(i) for i in getattr(item, groupname).all()])),
                reverse=True)
            groups, groupnames = [], []
            for obj in set:
                for n in getattr(obj, groupname).all():
                    if n not in groupnames:
                        groupnames.append(n)
            for n in groupnames:
                l = []
                for obj in set:
                    if n in getattr(obj, groupname).all():
                        l.append(obj)
                groups.append((unicode(n), l))

        elif xtype == ('DateTimeField' or 'DateField'):
            set = set.order_by(groupname)
            #set = sorted(set, key = lambda item: getattr(item,groupname))
            # TODO: Fix this sort
            groups, groupnames, l, ng = [], [], [], []
            n = None

            if xtype == 'DateTimeField':

                def dt(ob):
                    return getattr(ob, groupname).date()
            else:

                def dt(ob):
                    return getattr(ob, groupname)

            for x in set:
                n = dt(x)
                if n:
                    break
            if n:
                for obj in set:
                    if getattr(obj, groupname):
                        if dt(obj) == n:
                            l.append(obj)
                        else:
                            groups.append((unicode(n), l))
                            l = []
                            n = dt(obj)
                            l.append(obj)
                    else:
                        ng.append(obj)
                if ng:
                    groups.append(('None', ng))

        else:
            set = sorted(
                set,
                key=lambda item: unicode(item.get_field_value(groupname)),
                reverse=True)
            groups = []
            for g, ks in groupby(
                    set,
                    key=lambda item: unicode(item.get_field_value(groupname))):
                groups.append((g, list(ks)))
            xfield = set[0]._meta.get_field_by_name(groupname)[0]

    # Count aggregate functions
    agg_funcs = {}
    for field in model.fields:  # get fields and aggregate functions for them
        if field.display and getattr(field, 'aggregation', None):
            xfield = classobj._meta.get_field_by_name(field.name)[0]
            if number_field_regex.match(xfield.get_internal_type()) \
                    and aggregate_functions.has_key(field.aggregation):
                agg_funcs[field.name] = aggregate_functions[
                    field.aggregation]['function']

    aggregations = {}
    if agg_funcs:
        for grouper, ls in groups if groups else (('set', set), ):
            data = {}
            for s in ls:
                for key in agg_funcs:
                    data.setdefault(key, []).append(getattr(s, key, 0))
            aggrs = {}
            for key, func in agg_funcs.items():
                aggrs[key] = func(data.get(key, [
                    0,
                ]))
            aggregations[grouper] = aggrs

    return {
        'model': model,
        'set': set,
        'groups': groups,
        'groupname': groupname,
        'aggregations': aggregations
    }