Exemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        center_id = str_to_int_or_default(request.GET.get('center', 0), 0)
        group_id = str_to_int_or_default(request.GET.get('group', 0), 0)
        host_id = str_to_int_or_default(request.GET.get('host', 0), 0)
        user_id = str_to_int_or_default(request.GET.get('user', 0), 0)
        search = request.GET.get('search', '')

        # 超级用户可以有用户下拉框选项
        auth = request.user
        if auth.is_superuser:
            users = User.objects.all()
        else:   # 普通用户只能查看自己的虚拟机,无用户下拉框选项
            users = None
            user_id = auth.id

        v_manager = VmManager()
        try:
            queryset = v_manager.filter_vms_queryset(center_id=center_id, group_id=group_id, host_id=host_id,
                                                    search=search, user_id=user_id, all_no_filters=auth.is_superuser)
        except VmError as e:
            return render(request, 'error.html', {'errors': ['查询虚拟机时错误',str(e)]})

        queryset = queryset.prefetch_related('vdisk_set')  # 反向预查询硬盘(避免多次访问数据库)
        try:
            c_manager = CenterManager()
            g_manager = GroupManager()
            centers = c_manager.get_center_queryset()
            if center_id > 0:
                groups = c_manager.get_group_queryset_by_center(center_id)
            else:
                groups = g_manager.get_group_queryset()

            if group_id > 0:
                hosts = g_manager.get_host_queryset_by_group(group_id)
            else:
                hosts = None
        except ComputeError as e:
            return render(request, 'error.html', {'errors': ['查询虚拟机时错误', str(e)]})

        context = {'center_id': center_id if center_id > 0 else None, 'centers': centers, 'groups': groups,
                   'group_id': group_id if group_id > 0 else None, 'hosts': hosts,
                   'host_id': host_id if host_id > 0 else None, 'search': search, 'users': users, 'user_id': user_id}
        context = self.get_vms_list_context(request, queryset, context)
        return render(request, 'vms_list.html', context=context)
Exemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        center_id = str_to_int_or_default(request.GET.get('center', 0), 0)
        group_id = str_to_int_or_default(request.GET.get('group', 0), 0)
        host_id = str_to_int_or_default(request.GET.get('host', 0), 0)
        type_id = str_to_int_or_default(request.GET.get('type', 0), 0)
        search = request.GET.get('search', '')
        user = request.user

        c_manager = CenterManager()
        g_manager = GroupManager()
        p_manager = PCIDeviceManager()
        p_manager._group_manager = g_manager
        try:
            queryset = p_manager.filter_pci_queryset(center_id=center_id, group_id=group_id, host_id=host_id,
                                                     search=search, type_id=type_id, user=user, all_no_filters=True)
        except DeviceError as e:
            return render(request, 'error.html', {'errors': ['查询PCI设备时错误', str(e)]})

        try:
            centers = c_manager.get_center_queryset()
            if center_id > 0:
                groups = c_manager.get_user_group_queryset_by_center(center_or_id=center_id, user=user)
            else:
                groups = c_manager.get_user_group_queryset(user=user)

            if group_id > 0:
                hosts = g_manager.get_host_queryset_by_group(group_id)
            else:
                hosts = None
        except ComputeError as e:
            return render(request, 'error.html', {'errors': ['查询PCI设备时错误', str(e)]})

        context = {'center_id': center_id if center_id > 0 else None, 'centers': centers, 'groups': groups,
                   'group_id': group_id if group_id > 0 else None, 'hosts': hosts,
                   'host_id': host_id if host_id > 0 else None, 'search': search, 'types': PCIDevice.CHOICES_TYPE,
                   'type_id': type_id}
        context = self.get_vms_list_context(request, queryset, context)
        return render(request, 'pci_list.html', context=context)