Esempio n. 1
0
def used_resources(request, rest=False):
    """ view for returning used resources for a given cluster user """
    try:
        cluster_user_id = request.GET['id']
    except KeyError:
        return render_404(request, 'requested user was not found')
    cu = get_object_or_404(ClusterUser, pk=cluster_user_id)

    # must be a super user, the user in question, or a member of the group
    user = request.user
    if not user.is_superuser:
        user_type = ContentType.objects.get_for_model(Profile)
        if cu.real_type_id == user_type.pk:
            if not Profile.objects.filter(clusteruser_ptr=cu.pk, user=user)\
                .exists():
                raise Http403(_('You are not authorized to view this page'))
        else:
            if not Organization.objects.filter(clusteruser_ptr=cu.pk, \
                                               group__user=user).exists():
                raise Http403(_('You are not authorized to view this page'))

    resources = get_used_resources(cu.cast())
    if rest:
        return resources
    else:
        return render_to_response("ganeti/overview/used_resources.html", {
            'resources':resources
        }, context_instance=RequestContext(request))
Esempio n. 2
0
def used_resources(request, rest=False):
    """ view for returning used resources for a given cluster user """
    try:
        cluster_user_id = request.GET["id"]
    except KeyError:
        return render_404(request, "requested user was not found")
    cu = get_object_or_404(ClusterUser, pk=cluster_user_id)

    # must be a super user, the user in question, or a member of the group
    user = request.user
    if not user.is_superuser:
        user_type = ContentType.objects.get_for_model(Profile)
        if cu.real_type_id == user_type.pk:
            if not Profile.objects.filter(clusteruser_ptr=cu.pk, user=user).exists():
                raise Http403(_("You are not authorized to view this page"))
        else:
            if not Organization.objects.filter(clusteruser_ptr=cu.pk, group__user=user).exists():
                raise Http403(_("You are not authorized to view this page"))

    resources = get_used_resources(cu.cast())
    if rest:
        return resources
    else:
        return render_to_response(
            "ganeti/overview/used_resources.html", {"resources": resources}, context_instance=RequestContext(request)
        )
Esempio n. 3
0
def quota(request, cluster_slug, user_id):
    """
    Updates quota for a user
    """
    cluster = get_object_or_404(Cluster, slug=cluster_slug)
    user = request.user
    if not (user.is_superuser or user.has_perm('admin', cluster)):
        raise Http403(NO_PRIVS)

    if request.method == 'POST':
        form = QuotaForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            cluster_user = data['user']
            if data['delete']:
                cluster.set_quota(cluster_user, None)
            else:
                cluster.set_quota(cluster_user, data)

            # return updated html
            cluster_user = cluster_user.cast()
            url = reverse('cluster-permissions', args=[cluster.slug])
            if isinstance(cluster_user, (Profile, )):
                return render_to_response(
                    "ganeti/cluster/user_row.html", {
                        'object': cluster,
                        'user_detail': cluster_user.user,
                        'url': url
                    },
                    context_instance=RequestContext(request))
            else:
                return render_to_response(
                    "ganeti/cluster/group_row.html", {
                        'object': cluster,
                        'group': cluster_user.group,
                        'url': url
                    },
                    context_instance=RequestContext(request))

        # error in form return ajax response
        content = json.dumps(form.errors)
        return HttpResponse(content, mimetype='application/json')

    if user_id:
        cluster_user = get_object_or_404(ClusterUser, id=user_id)
        quota = cluster.get_quota(cluster_user)
        data = {'user': user_id}
        if quota:
            data.update(quota)
    else:
        return render_404(request, _('User was not found'))

    form = QuotaForm(data)
    return render_to_response("ganeti/cluster/quota.html", {
        'form': form,
        'cluster': cluster,
        'user_id': user_id
    },
                              context_instance=RequestContext(request))
Esempio n. 4
0
File: cluster.py Progetto: bsu/GWM2
def quota(request, cluster_slug, user_id):
    """
    Updates quota for a user
    """
    cluster = get_object_or_404(Cluster, slug=cluster_slug)
    user = request.user
    if not (user.is_superuser or user.has_perm('admin', cluster)):
        return render_403(request, _("You do not have sufficient privileges"))
    
    if request.method == 'POST':
        form = QuotaForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            cluster_user = data['user']
            if data['delete']:
                cluster.set_quota(cluster_user)
            else:
                quota = cluster.get_quota()
                same = data['virtual_cpus'] == quota['virtual_cpus'] \
                    and data['disk']==quota['disk'] \
                    and data['ram']==quota['ram']
                if same:
                    # same as default, set quota to default.
                    cluster.set_quota(cluster_user)
                else:
                    cluster.set_quota(cluster_user, data)
            
            # return updated html
            cluster_user = cluster_user.cast()
            url = reverse('cluster-permissions', args=[cluster.slug])
            if isinstance(cluster_user, (Profile,)):
                return render_to_response(
                    "ganeti/cluster/user_row.html",
                    {'object':cluster, 'user_detail':cluster_user.user, 'url':url},
                    context_instance=RequestContext(request))
            else:
                return render_to_response(
                    "ganeti/cluster/group_row.html",
                    {'object':cluster, 'group':cluster_user.group, 'url':url},
                    context_instance=RequestContext(request))
        
        # error in form return ajax response
        content = json.dumps(form.errors)
        return HttpResponse(content, mimetype='application/json')
    
    if user_id:
        cluster_user = get_object_or_404(ClusterUser, id=user_id)
        quota = cluster.get_quota(cluster_user)
        data = {'user':user_id}
        if quota:
            data.update(quota)
    else:
        return render_404(request, _('User was not found'))
    
    form = QuotaForm(data)
    return render_to_response("ganeti/cluster/quota.html",
                        {'form':form, 'cluster':cluster, 'user_id':user_id},
                        context_instance=RequestContext(request))
Esempio n. 5
0
def quota(request, cluster_slug, user_id):
    """
    Updates quota for a user
    """
    cluster = get_object_or_404(Cluster, slug=cluster_slug)
    user = request.user
    if not (user.is_superuser or user.has_perm("admin", cluster)):
        raise Http403(NO_PRIVS)

    if request.method == "POST":
        form = QuotaForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            cluster_user = data["user"]
            if data["delete"]:
                cluster.set_quota(cluster_user, None)
            else:
                cluster.set_quota(cluster_user, data)

            # return updated html
            cluster_user = cluster_user.cast()
            url = reverse("cluster-permissions", args=[cluster.slug])
            if isinstance(cluster_user, (Profile,)):
                return render_to_response(
                    "ganeti/cluster/user_row.html",
                    {"object": cluster, "user_detail": cluster_user.user, "url": url},
                    context_instance=RequestContext(request),
                )
            else:
                return render_to_response(
                    "ganeti/cluster/group_row.html",
                    {"object": cluster, "group": cluster_user.group, "url": url},
                    context_instance=RequestContext(request),
                )

        # error in form return ajax response
        content = json.dumps(form.errors)
        return HttpResponse(content, mimetype="application/json")

    if user_id:
        cluster_user = get_object_or_404(ClusterUser, id=user_id)
        quota = cluster.get_quota(cluster_user)
        data = {"user": user_id}
        if quota:
            data.update(quota)
    else:
        return render_404(request, _("User was not found"))

    form = QuotaForm(data)
    return render_to_response(
        "ganeti/cluster/quota.html",
        {"form": form, "cluster": cluster, "user_id": user_id},
        context_instance=RequestContext(request),
    )