Move default template for tables out of template into table base class
[ganeti_webmgr.git] / ganeti_web / views / jobs.py
blob3197aad04fb2f2742d64f85abdc428d05463a604
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 from django.contrib.auth.decorators import login_required
19 from django.http import HttpResponse
20 from django.shortcuts import get_object_or_404
21 from django.utils import simplejson as json
22 from django.views.generic.detail import DetailView
24 from ganeti_web.middleware import Http403
25 from ganeti_web.models import Job, Cluster, VirtualMachine, Node
26 from ganeti_web.views.generic import NO_PRIVS, LoginRequiredMixin
29 class JobDetailView(LoginRequiredMixin, DetailView):
31 template_name = "ganeti/job/detail.html"
33 def get_object(self, queryset=None):
34 return get_object_or_404(Job, job_id=self.kwargs["job_id"],
35 cluster__slug=self.kwargs["cluster_slug"])
37 def get_context_data(self, **kwargs):
38 job = kwargs["object"]
39 user = self.request.user
40 admin = user.is_superuser or user.has_perm("admin", job.cluster)
42 return {
43 "job": job,
44 "cluster_admin": admin,
48 @login_required
49 def status(request, cluster_slug, job_id, rest=False):
50 """
51 returns the raw info of a job
52 """
53 job = get_object_or_404(Job, cluster__slug=cluster_slug, job_id=job_id)
54 if rest:
55 return job
56 else:
57 return HttpResponse(json.dumps(job.info), mimetype='application/json')
60 @login_required
61 def clear(request, cluster_slug, job_id):
62 """
63 Remove a job.
64 """
66 user = request.user
67 cluster = get_object_or_404(Cluster, slug=cluster_slug)
68 job = get_object_or_404(Job, cluster__slug=cluster_slug, job_id=job_id)
69 obj = job.obj
71 # if not a superuser, check permissions on the object itself
72 cluster_admin = user.is_superuser or user.has_perm('admin', cluster)
74 if not cluster_admin:
75 if isinstance(obj, (Cluster, Node)):
76 raise Http403(NO_PRIVS)
77 elif isinstance(obj, (VirtualMachine,)):
78 # object is a virtual machine, check perms on VM and on Cluster
79 if not (obj.owner_id == user.get_profile().pk
80 or user.has_perm('admin', obj)
81 or user.has_perm('admin', obj.cluster)):
82 raise Http403(NO_PRIVS)
84 # If the job points to an object, and the job is the most recent job on
85 # the object, then clear it from the object.
86 if obj is not None:
87 if obj.last_job == job:
88 obj.last_job = None
89 obj.ignore_cache = False
90 obj.save()
92 # "Clear" the job. With extreme prejudice.
93 job.delete()
95 return HttpResponse('1', mimetype='application/json')