Ticket #3957 (partial) - ganeti_webmgr uses ganeti python namespace:
[ganeti_webmgr.git] / ganeti_web / views / jobs.py
blob8b3b2bcccb1bf25cebda6e5386af05c2a40873a0
1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
18 import json
19 from django.template.context import RequestContext
21 from django.contrib.auth.decorators import login_required
22 from django.http import HttpResponse
23 from django.shortcuts import get_object_or_404, render_to_response
25 from ganeti_web.models import Job, Cluster, VirtualMachine, Node
26 from ganeti_web.views import render_403
27 from django.utils.translation import ugettext as _
29 @login_required
30 def status(request, cluster_slug, job_id):
31 """
32 returns the raw info of a job
33 """
34 job = get_object_or_404(Job, cluster__slug=cluster_slug, job_id=job_id)
35 return HttpResponse(json.dumps(job.info), mimetype='application/json')
38 @login_required
39 def detail(request, cluster_slug, job_id):
40 job = get_object_or_404(Job, cluster__slug=cluster_slug, job_id=job_id)
42 user = request.user
43 cluster_admin = True if user.is_superuser else user.has_perm('admin', job.cluster)
45 return render_to_response("job/detail.html",{
46 'job':job,
47 'cluster_admin':cluster_admin
48 } ,context_instance=RequestContext(request))
51 @login_required
52 def clear(request, cluster_slug, job_id):
53 """
54 Clear a single failed job error message
55 """
57 user = request.user
58 cluster = get_object_or_404(Cluster, slug=cluster_slug)
59 job = get_object_or_404(Job, cluster__slug=cluster_slug, job_id=job_id)
60 obj = job.obj
62 # if not a superuser, check permissions on the object itself
63 cluster_admin = user.is_superuser or user.has_perm('admin', cluster)
65 if not cluster_admin:
66 if isinstance(obj, (Cluster, Node)):
67 return render_403(request, _("You do not have sufficient privileges"))
68 elif isinstance(obj, (VirtualMachine,)):
69 # object is a virtual machine, check perms on VM and on Cluster
70 if not (obj.owner_id == user.get_profile().pk \
71 or user.has_perm('admin', obj) \
72 or user.has_perm('admin', obj.cluster)):
73 return render_403(request, _("You do not have sufficient privileges"))
76 # clear the error.
77 Job.objects.filter(pk=job.pk).update(cleared=True)
79 # clear the job from the object, but only if it is the last job. It's
80 # possible another job was started after this job, and the error message
81 # just wasn't cleared.
83 # XXX object could be none, in which case we dont need to clear its last_job
84 if obj is not None:
85 ObjectModel = obj.__class__
86 ObjectModel.objects.filter(pk=job.object_id, last_job=job) \
87 .update(last_job=None, ignore_cache=False)
89 return HttpResponse('1', mimetype='application/json')