Fakultas Ilmu Komputer UI

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

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

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


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

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

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

35

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

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

    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        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 != "":
78
79
            materi.status = "APPROVE"
            materi.save()
80
        elif action == "disapprove" and feedback != "":
81
82
83
84
85
86
            materi.status = "DISAPPROVE"
            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)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
157

158

159
160
class KelolaKontributorView(TemplateView):
    template_name = "kelola_kontributor.html"
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
161
162
163
164
165

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

166
        return super(KelolaKontributorView, self).dispatch(request, *args, **kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
167
168

    def get_context_data(self, **kwargs):
169
        context = super(KelolaKontributorView, self).get_context_data(**kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
170
171
172
173
174
175
176
        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)

177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
class KelolaAdminView(TemplateView):
    template_name = "kelola_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(KelolaAdminView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(KelolaAdminView, self).get_context_data(**kwargs)
        context['users_list'] = User.objects.filter(is_admin=True)
        return context

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

196

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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"])
212
        return self.render_to_response(context=context)
213

214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
class ProfileAdminAdministrationView(TemplateView):
    template_name = "detail_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(ProfileAdminAdministrationView, 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"])
        return self.render_to_response(context=context)

232

233
234
235
236
237
238
239
240
241
242
243
244
class RegistrasiAdminView(TemplateView):
    template_name = "registrasi_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(RegistrasiAdminView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(RegistrasiAdminView, self).get_context_data(**kwargs)
        context["form"] = RegistrasiAdminForm
        return context
245

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

250
251
252
253
254
255
256
257
258
259
260
261
262
263
    def post(self, request, *args, **kwargs):
        data = request.POST.copy()
        form = RegistrasiAdminForm(request.POST)
        if form.is_valid():
            new_user = form.save(commit=False)
            new_user.password = make_password(data["password"])
            new_user.is_admin = True
            new_user.save()
            return HttpResponseRedirect("/administration/kelola-admin/")
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

class EditVerificationView(TemplateView):
    template_name = "edit_verifikasi.html"

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

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

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


282
283
284
285
def delete_admin(request, *args, **kwargs):
    User.objects.filter(pk=kwargs["pk"]).delete()
    return HttpResponseRedirect("/administration/kelola-admin/")

286

287
288
def delete_contributor(request, *args, **kwargs):
    User.objects.filter(pk=kwargs["pk"]).delete()
289
    return HttpResponseRedirect("/administration/kelola-kontributor/")