Fakultas Ilmu Komputer UI

views.py 6 KB
Newer Older
1
from django.core.exceptions import PermissionDenied
2
from django.http import HttpResponseRedirect
3
from django.shortcuts import get_object_or_404, render
4
from django.views.generic import TemplateView
5
6
7

from app.models import Category, Materi

8
from .forms import CategoryForm, VerificationSettingForm
9
from .models import VerificationSetting, VerificationReport
10
11
12


# Create your views here.
13
14
15
class VerificationView(TemplateView):
    template_name = "verif.html"

16
17
18
19
20
21
22
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(VerificationView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(VerificationView, self).get_context_data(**kwargs)
23
24
25
26
        context["materi_list_pending"] = Materi.objects.filter(
            status='PENDING')
        context["materi_list_history"] = Materi.objects.exclude(
            status='PENDING')
27
28
        return context

29
30
31
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)
32

33

34
35
36
37
38
39
40
class DetailVerificationView(TemplateView):
    template_name = "detail_verif.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(DetailVerificationView, self).dispatch(request, *args, **kwargs)
41

42
43
44
    def get_context_data(self, **kwargs):
        context = super(DetailVerificationView,
                        self).get_context_data(**kwargs)
45
46
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
        context["materi_data"] = materi
47
48
        context["kriteria_list"] = VerificationSetting.objects.filter(
            archived=False)
49
50
51
        riwayat = True if materi.status != "PENDING" else False
        context["riwayat"] = riwayat
        context["verification_report"] = materi.verificationreport_set.first()
52
53
54
55
        return context

    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        feedback = request.POST.get("feedback", "")
        action = request.POST.get("action", None)

        report = {}
        kriteria = []
        for item in request.POST.items():
            if "kriteria" in item[0]:
                verification_item_id = item[0].split("-")[1]
                verif_obj = VerificationSetting.objects.get(
                    id=int(verification_item_id))
                kriteria_value = {
                    "title": verif_obj.title,
                    "status": True if item[1] == "1" else False
                }
                kriteria.append(kriteria_value)

        report["kriteria"] = kriteria
        report["feedback"] = feedback

        if action == "approve" and feedback != "":
76
77
78
            materi.status = "APPROVE"
            materi.feedback = feedback
            materi.save()
79
        elif action == "disapprove" and feedback != "":
80
81
82
83
84
85
86
            materi.status = "DISAPPROVE"
            materi.feedback = feedback
            materi.save()
        else:
            context = self.get_context_data(**kwargs)
            context["error"] = True
            return self.render_to_response(context=context)
87
88
89
        verif_report = VerificationReport(
            report=report, materi=materi, user=self.request.user, status=materi.get_status_display())
        verif_report.save()
90
        return HttpResponseRedirect("/administration/")
91

92
93
94
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context=context)
95

96
97

class VerificationSettingView(TemplateView):
98
99
100
101
102
103
    template_name = "settings.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(VerificationSettingView, self).dispatch(request, *args, **kwargs)
104
105

    def get_context_data(self, **kwargs):
106
107
        context = super(VerificationSettingView,
                        self).get_context_data(**kwargs)
108
        context["items"] = VerificationSetting.objects.filter(
109
            archived=False)
110
        context["page_title"] = "Pengaturan Point Verifikasi Materi"
111
112
113
        return context

    def get(self, request, *args, **kwargs):
114
115
116
        context = self.get_context_data(**kwargs)
        context["form"] = VerificationSettingForm()
        return self.render_to_response(context=context)
117
118

    def post(self, request, *args, **kwargs):
119
120
121
122
        form = VerificationSettingForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/administration/setting/verification/")
123
        else:
124
125
126
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)
127
128


129
class CategorySettingView(TemplateView):
130
131
132
133
134
135
    template_name = "settings.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(CategorySettingView, self).dispatch(request, *args, **kwargs)
136
137

    def get_context_data(self, **kwargs):
138
        context = super(CategorySettingView, self).get_context_data(**kwargs)
139
140
        context["items"] = Category.objects.all()
        context["page_title"] = "Pengaturan Kategori Materi"
141
142
143
        return context

    def get(self, request, *args, **kwargs):
144
145
146
        context = self.get_context_data(**kwargs)
        context["form"] = CategoryForm()
        return self.render_to_response(context=context)
147
148

    def post(self, request, *args, **kwargs):
149
150
151
152
        form = CategoryForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/administration/setting/category/")
153
        else:
154
155
156
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)