aboutsummaryrefslogtreecommitdiffstats
path: root/warningmgr/views.py
blob: e4c47ed5dfd0fdd025bf050aefd99866c80478e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# buildhistory-web - view definitions
#
# Copyright (C) 2013-2015 Intel Corporation
#
# Licensed under the MIT license, see COPYING.MIT for details

from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.template import RequestContext
from warningmgr.models import WarningItem, Comment
from datetime import datetime
from django.views.generic import DetailView, ListView

def postcomment(request, pk):
    if not request.user.is_authenticated():
        raise PermissionDenied
    w = get_object_or_404(WarningItem, pk=pk)
    comment_text = request.POST['comment_text'].strip()
    if comment_text != '':
        c = w.comment_set.create(comment=request.POST['comment_text'], date=datetime.now(), author=request.user.username)
        c.save()
        return HttpResponseRedirect(reverse('warning_item', args=(w.id,)))
    else:
        return render_to_response('warningmgr/detail.html', {
            'warningitem': w,
            'error_message': "You didn't specify any comment text.",
        }, context_instance=RequestContext(request))

def ignore(request, pk):
    return _statuschange(request, pk, 'I')

def actionreq(request, pk):
    return _statuschange(request, pk, 'A')

def resolve(request, pk):
    return _statuschange(request, pk, 'R')

def unreview(request, pk):
    return _statuschange(request, pk, 'N')

def _statuschange(request, pk, newstatus):
    if not (request.user.is_authenticated() and request.user.has_perm('warningmgr.change_warningitem')):
        raise PermissionDenied
    w = get_object_or_404(WarningItem, pk=pk)
    w.change_status(newstatus, request.user.username)
    w.save()
    return HttpResponseRedirect(reverse('warning_item', args=(w.id,)))


def multi_action(request):
    if not (request.user.is_authenticated() and request.user.has_perm('warningmgr.change_warningitem')):
        raise PermissionDenied
    action = request.POST['action']
    action_statuses = { 'actionreq': 'A', 'resolve': 'R', 'ignore': 'I' }
    if action and action in action_statuses:
        id_list = request.POST.getlist('selecteditems')
        id_list = [int(i) for i in id_list if i.isdigit()]
        warningitems = WarningItem.objects.filter(id__in=id_list)
        for w in warningitems:
            w.change_status(action_statuses[action], request.user.username)
            w.save()

    return HttpResponseRedirect(reverse('warning_list'))


class WarningListView(ListView):
    context_object_name = 'warning_list'
    paginate_by = 20

    def get_queryset(self):
        return WarningItem.objects.filter(status__in=self.request.session.get('status_filter', 'NA')).order_by('-build__created_date')

class ReviewedWarningListView(WarningListView):

    def get_context_data(self, **kwargs):
        context = super(WarningListView, self).get_context_data(**kwargs)
        context['reviewed'] = True
        return context

    def get_queryset(self):
        return WarningItem.objects.filter(status__in=self.request.session.get('status_filter', 'AIR')).order_by('-build__created_date')