Fakultas Ilmu Komputer UI

views.py 7.33 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

from app.models import Category, Materi
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
7
from authentication.models import User
8

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


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

17
18
19
20
21
22
23
    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)
24
25
26
27
        context["materi_list_pending"] = Materi.objects.filter(
            status='PENDING')
        context["materi_list_history"] = Materi.objects.exclude(
            status='PENDING')
28
29
        return context

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

34

35
36
37
38
39
40
41
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)
42

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

    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        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 != "":
77
78
            materi.status = "APPROVE"
            materi.save()
79
        elif action == "disapprove" and feedback != "":
80
81
82
83
84
85
            materi.status = "DISAPPROVE"
            materi.save()
        else:
            context = self.get_context_data(**kwargs)
            context["error"] = True
            return self.render_to_response(context=context)
86
87
88
        verif_report = VerificationReport(
            report=report, materi=materi, user=self.request.user, status=materi.get_status_display())
        verif_report.save()
89
        return HttpResponseRedirect("/administration/")
90

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

95
96

class VerificationSettingView(TemplateView):
97
98
99
100
101
102
    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)
103
104

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

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

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


128
class CategorySettingView(TemplateView):
129
130
131
132
133
134
    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)
135
136

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

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

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

157

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class ContributorListView(TemplateView):
    template_name = "daftar_kontributor.html"

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

        return super(ContributorListView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(ContributorListView, self).get_context_data(**kwargs)
        context["users"] = User.objects.filter(is_contributor=True)
        return context

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

176

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class ProfileContributorAdminView(TemplateView):
    template_name = "detail_kontri_admin.html"

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

        return super(ProfileContributorAdminView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        return super().get_context_data(**kwargs)

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        context["user"] = get_object_or_404(User, pk=kwargs["pk"])
192
        return self.render_to_response(context=context)