@list_route()
    def stats(self, request):
        queryset = filter_queryset_for_user(models.InstanceLicense.objects.all(), request.user)
        queryset = self._filter_queryset(queryset)

        aggregate_parameters = self.request.query_params.getlist('aggregate', [])
        aggregate_parameter_to_field_map = {
            'project': [
                'instance__cloud_project_membership__project__uuid',
                'instance__cloud_project_membership__project__name',
            ],
            'project_group': [
                'instance__cloud_project_membership__project__project_groups__uuid',
                'instance__cloud_project_membership__project__project_groups__name',
            ],
            'customer': [
                'instance__cloud_project_membership__project__customer__uuid',
                'instance__cloud_project_membership__project__customer__name',
                'instance__cloud_project_membership__project__customer__abbreviation',
            ],
            'type': ['template_license__license_type'],
            'name': ['template_license__name'],
        }

        aggregate_fields = []
        for aggregate_parameter in aggregate_parameters:
            if aggregate_parameter not in aggregate_parameter_to_field_map:
                return Response('Licenses statistics can not be aggregated by %s' % aggregate_parameter,
                                status=status.HTTP_400_BAD_REQUEST)
            aggregate_fields += aggregate_parameter_to_field_map[aggregate_parameter]

        queryset = queryset.values(*aggregate_fields).annotate(count=django_models.Count('id', distinct=True))
        # This hack can be removed when https://code.djangoproject.com/ticket/16735 will be closed
        # Replace databases paths by normal names. Ex: instance__project__uuid is replaced by project_uuid
        name_replace_map = {
            'instance__cloud_project_membership__project__uuid': 'project_uuid',
            'instance__cloud_project_membership__project__name': 'project_name',
            'instance__cloud_project_membership__project__project_groups__uuid': 'project_group_uuid',
            'instance__cloud_project_membership__project__project_groups__name': 'project_group_name',
            'instance__cloud_project_membership__project__customer__uuid': 'customer_uuid',
            'instance__cloud_project_membership__project__customer__name': 'customer_name',
            'instance__cloud_project_membership__project__customer__abbreviation': 'customer_abbreviation',
            'template_license__license_type': 'type',
            'template_license__name': 'name',
        }
        for d in queryset:
            for db_name, output_name in name_replace_map.iteritems():
                if db_name in d:
                    d[output_name] = d[db_name]
                    del d[db_name]

            for a in d:
                if a.endswith('uuid'):
                    d[a] = d[a].hex

        # XXX: hack for portal only. (Provide project group data if aggregation was done by project)
        if 'project' in aggregate_parameters and 'project_group' not in aggregate_parameters:
            for item in queryset:
                project = Project.objects.get(uuid=item['project_uuid'])
                if project.project_group is not None:
                    item['project_group_uuid'] = project.project_group.uuid.hex
                    item['project_group_name'] = project.project_group.name
        return Response(queryset)
dataset/ETHPy150Open opennode/nodeconductor/nodeconductor/iaas/views.py/TemplateLicenseViewSet.stats/original