示例#1
0
def my_appliances(request, show_user="******"):
    if not request.user.is_superuser:
        if not (show_user == "my" or show_user == request.user.username):
            messages.info(request, "You can't view others' appliances!")
            return redirect("my_appliances")
        if show_user == request.user.username:
            return redirect("my_appliances")
    else:
        other_users = User.objects.exclude(pk=request.user.pk).order_by(
            "last_name", "first_name")
    if show_user == "my":
        pools = AppliancePool.objects.filter(owner=request.user).order_by("id")
    elif show_user == "all":
        pools = AppliancePool.objects.order_by("id")
    else:
        pools = AppliancePool.objects.filter(
            owner__username=show_user).order_by("id")
    pools = pools.select_related('group', 'provider', 'owner')
    page = request.GET.get("page")
    try:
        per_page = int(request.GET.get("per_page", 25))
    except (ValueError, TypeError):
        per_page = 5

    pools_paginator = Paginator(pools, per_page)
    try:
        pools_paged = pools_paginator.page(page)
        page = int(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        pools_paged = pools_paginator.page(1)
        page = 1
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        pools_paged = pools_paginator.page(pools_paginator.num_pages)
        page = pools_paginator.num_pages

    pages = list(pools_paginator.page_range)
    if pools_paginator.num_pages <= 5:
        start_index = 0
        end_index = 5
    else:
        if page - 2 < 0:
            start_index = 0
            end_index = 5
        elif page + 2 > pools_paginator.num_pages:
            end_index = pools_paginator.num_pages
            start_index = end_index - 5
        else:
            start_index = page - 3
            end_index = page + 2
    if start_index < 0:
        end_index -= start_index
        start_index = 0
    pages = pages[start_index:end_index]
    available_groups = Group.objects.filter(
        id__in=Template.objects.values_list('template_group',
                                            flat=True).distinct())
    group_tuples = []
    for grp in available_groups:
        group_tuples.append((grp.templates.order_by('-date')[0].date, grp))
    group_tuples.sort(key=lambda gt: gt[0], reverse=True)
    template_types = [t for t in Template.TEMPLATE_TYPES]
    can_order_pool = show_user == "my"
    new_pool_possible = True
    display_legend = False
    for pool in pools:
        if not pool.finished:
            display_legend = True
    per_pool_quota = None
    pools_remaining = None
    num_user_vms = Appliance.objects.filter(
        appliance_pool__owner=request.user).count()
    if request.user.has_quotas:
        if request.user.quotas.total_pool_quota is not None:
            if request.user.quotas.total_pool_quota <= len(pools):
                new_pool_possible = False
            pools_remaining = request.user.quotas.total_pool_quota - len(pools)
        if request.user.quotas.total_vm_quota is not None:
            if request.user.quotas.total_vm_quota <= num_user_vms:
                new_pool_possible = False
        if request.user.quotas.per_pool_quota is not None:
            per_pool_quota = request.user.quotas.per_pool_quota
            remaining_vms = request.user.quotas.total_vm_quota - num_user_vms
            if remaining_vms < per_pool_quota:
                per_pool_quota = remaining_vms
    per_pool_quota_enabled = per_pool_quota is not None
    can_change_hw = request.user.has_perm('appliances.can_modify_hw')
    provider_types = Provider.get_available_provider_types(request.user)
    return render(request, 'appliances/my_appliances.html', locals())
示例#2
0
def my_appliances(request, show_user="******"):
    if not request.user.is_superuser:
        if not (show_user == "my" or show_user == request.user.username):
            messages.info(request, "You can't view others' appliances!")
            return redirect("my_appliances")
        if show_user == request.user.username:
            return redirect("my_appliances")
    else:
        other_users = User.objects.exclude(pk=request.user.pk).order_by("last_name", "first_name")
    if show_user == "my":
        pools = AppliancePool.objects.filter(owner=request.user).order_by("id")
    elif show_user == "all":
        pools = AppliancePool.objects.order_by("id")
    else:
        pools = AppliancePool.objects.filter(owner__username=show_user).order_by("id")
    pools = pools.select_related('group', 'provider', 'owner')
    page = request.GET.get("page")
    try:
        per_page = int(request.GET.get("per_page", 5))
    except (ValueError, TypeError):
        per_page = 5

    pools_paginator = Paginator(pools, per_page)
    try:
        pools_paged = pools_paginator.page(page)
        page = int(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        pools_paged = pools_paginator.page(1)
        page = 1
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        pools_paged = pools_paginator.page(pools_paginator.num_pages)
        page = pools_paginator.num_pages

    pages = list(pools_paginator.page_range)
    if pools_paginator.num_pages <= 5:
        start_index = 0
        end_index = 5
    else:
        if page - 2 < 0:
            start_index = 0
            end_index = 5
        elif page + 2 > pools_paginator.num_pages:
            end_index = pools_paginator.num_pages
            start_index = end_index - 5
        else:
            start_index = page - 3
            end_index = page + 2
    if start_index < 0:
        end_index -= start_index
        start_index = 0
    pages = pages[start_index:end_index]
    available_groups = Group.objects.filter(
        id__in=Template.objects.values_list('template_group', flat=True).distinct())
    group_tuples = []
    for grp in available_groups:
        group_tuples.append((grp.templates.order_by('-date')[0].date, grp))
    group_tuples.sort(key=lambda gt: gt[0], reverse=True)
    template_types = [t for t in Template.TEMPLATE_TYPES]
    can_order_pool = show_user == "my"
    new_pool_possible = True
    display_legend = False
    for pool in pools:
        if not pool.finished:
            display_legend = True
    per_pool_quota = None
    pools_remaining = None
    num_user_vms = Appliance.objects.filter(appliance_pool__owner=request.user).count()
    if request.user.has_quotas:
        if request.user.quotas.total_pool_quota is not None:
            if request.user.quotas.total_pool_quota <= len(pools):
                new_pool_possible = False
            pools_remaining = request.user.quotas.total_pool_quota - len(pools)
        if request.user.quotas.total_vm_quota is not None:
            if request.user.quotas.total_vm_quota <= num_user_vms:
                new_pool_possible = False
        if request.user.quotas.per_pool_quota is not None:
            per_pool_quota = request.user.quotas.per_pool_quota
            remaining_vms = request.user.quotas.total_vm_quota - num_user_vms
            if remaining_vms < per_pool_quota:
                per_pool_quota = remaining_vms
    per_pool_quota_enabled = per_pool_quota is not None
    can_change_hw = request.user.has_perm('appliances.can_modify_hw')
    provider_types = Provider.get_available_provider_types(request.user)
    return render(request, 'appliances/my_appliances.html', locals())
示例#3
0
def available_provider_types(user):
    """Return a list of provider types usable for provisioning."""
    return Provider.get_available_provider_types(user)
示例#4
0
文件: api.py 项目: akarol/cfme_tests
def available_provider_types(user):
    """Return a list of provider types usable for provisioning."""
    return Provider.get_available_provider_types(user)