Fakultas Ilmu Komputer UI

views.py 23.9 KB
Newer Older
1
from django.core.exceptions import PermissionDenied
2
from django.contrib.auth.hashers import make_password
3
from django.db.models import Count
4
from django.http import HttpResponseRedirect, JsonResponse
5
from django.shortcuts import get_object_or_404
6
from django.views.generic import TemplateView, View
7
from django.contrib import messages
8
from django.utils import timezone
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
9
from administration.models import VerificationReport, VerificationSetting, DeletionHistory
Arief Pratama's avatar
Arief Pratama committed
10
from administration.forms import CategoryForm, VerificationSettingForm, RegistrasiAdminForm, PeriodForm, EditAdminStatusForm, EditKontributorStatusForm
11
from administration.services import StatisticService, DetailVerificationService, LaporanMateriService
12
from app.models import Category, Materi, ViewStatistics, DownloadStatistics, Comment, Like, LaporanMateri, AdminNotification
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
13
from authentication.models import User
14
from datetime import datetime
15

16
17
from administration.utils import generate_time_step

18
19
from django.core import management

Rizkhi PH's avatar
Rizkhi PH committed
20
ADMINISTRATION_MANAGEMENT = "/administration/kelola-admin/"
21
ADMINISTRATION_REPORT = "/administration/laporan-materi/"
22
23
ADMINISTRATION_VERIFICATION_SETTING = "/administration/setting/verification/"
ADMINISTRATION_CATEGORY_SETTING = "/administration/setting/category/"
24

25

26
27
28
class VerificationView(TemplateView):
    template_name = "verif.html"

29
30
31
32
33
34
35
    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)
36
37
38
39
        context["materi_list_pending"] = Materi.objects.filter(
            status='PENDING')
        context["materi_list_history"] = Materi.objects.exclude(
            status='PENDING')
40
41
        return context

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

46

47
48
49
50
51
52
53
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)
54

55
56
57
    def get_context_data(self, **kwargs):
        context = super(DetailVerificationView,
                        self).get_context_data(**kwargs)
58
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
59
        DetailVerificationService.init_data(context, materi)
60
61
        return context

62

63
64
    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
65
66
67
68
        feedback = request.POST.get("feedback", "")
        action = request.POST.get("action", None)

        report = {}
69
70
71
        items = request.POST.items()

        report["kriteria"] = DetailVerificationService.initilize_kriteria(items)
72
73
74
        report["feedback"] = feedback

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

87

88
89
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
90
91
        if(request.user.is_admin):
            AdminNotification.objects.filter(materi=context["materi_data"]).delete()
92
        return self.render_to_response(context=context)
93

94
95

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

    def get_context_data(self, **kwargs):
104
105
        context = super(VerificationSettingView,
                        self).get_context_data(**kwargs)
106
        context["items"] = VerificationSetting.objects.filter(
107
            archived=False)
108
109
        context["items_archived"] = VerificationSetting.objects.filter(
            archived=True)
110
        context["page_title"] = "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
        form = VerificationSettingForm(request.POST)
        if form.is_valid():
            form.save()
122
            return HttpResponseRedirect(ADMINISTRATION_VERIFICATION_SETTING)
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.filter(archived=False)
        context["items_archived"] = Category.objects.filter(archived=True)
141
        context["page_title"] = "Kategori Materi"
142
143
144
        return context

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

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

159

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

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

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

    def get_context_data(self, **kwargs):
170
        context = super(KelolaKontributorView, self).get_context_data(**kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
171
        context["users"] = User.objects.filter(is_contributor=True)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
172
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Kontributor")
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
173
174
175
176
177
178
        return context

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

179

180
181
182
183
184
185
186
187
188
189
190
191
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)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
192
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Admin")
193
194
195
196
197
        return context

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

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
199
200
201
202
class ProfileContributorAdminView(TemplateView):
    template_name = "detail_kontri_admin.html"

    def dispatch(self, request, *args, **kwargs):
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
203
        if not request.user.is_authenticated:
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
204
205
206
207
208
209
210
211
212
213
            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"])
214
        return self.render_to_response(context=context)
215

216
217
218
class StatisticsView(TemplateView):
    template_name = "administration/data_statistik.html"

219
220
221
222
223
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            raise PermissionDenied(request)
        return super(StatisticsView, self).dispatch(request, *args, **kwargs)

224
225
226
227
228
229
    def get_context_data(self, **kwargs):
        context = super(StatisticsView, self).get_context_data(**kwargs)
        context["page_title"] = "Statistik"
        context["periodForm"] = PeriodForm(self.request.GET)
        return context

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245




    def generate_chart_data(self, start, end, data_sets):
        chart_data = {
            "charts": [],
            "total": [],
            "label": []
        }
        label = ["Dilihat", "Diunduh", "Komentar", "Disukai"]
        colors = ["#4e73df", "#e74a3b", "#1cc88a", "#f6c23e"]
        time_step = generate_time_step(start, end)
        for i in time_step:
            chart_data["label"].append(i.strftime("%d/%m/%Y"))

246
        StatisticService.generate_view_stat(chart_data, colors, data_sets, label, time_step)
247
248
        return chart_data

249

250
251
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
252
253
254
255
256
257
258
259
260
        filter_set = StatisticService.get_filter_set(self.request.GET,**kwargs)
        view_set = StatisticService.get_query_set(self.request.user,ViewStatistics, **kwargs)
        view_set = StatisticService.filter_qs(view_set, filter_set)
        download_set = StatisticService.get_query_set(self.request.user, DownloadStatistics, **kwargs)
        download_set = StatisticService.filter_qs(download_set, filter_set)
        like_set = StatisticService.get_query_set(self.request.user, Like, **kwargs)
        like_set = StatisticService.filter_qs(like_set, filter_set)
        comment_set = StatisticService.get_query_set(self.request.user, Comment, **kwargs)
        comment_set = StatisticService.filter_qs(comment_set, filter_set)
261
262
263
264
265

        data_sets = [view_set, download_set, like_set, comment_set]
        chart_data = self.generate_chart_data(
            filter_set["start"], filter_set["end"], data_sets)
        context["chart_data"] = chart_data
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

        return self.render_to_response(context=context)


class StatisticApiView(View):
    def get_context_data(self, **kwargs):
        context = {}
        context["success"] = True
        context["period"] = self.request.GET.get("period", "default")
        return context

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


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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"])
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
297
        context["deletion_history"] = DeletionHistory.objects.filter(deletor_admin=get_object_or_404(User, pk=kwargs["pk"]))
298
299
        return self.render_to_response(context=context)

300

301
302
303
304
305
306
307
308
309
310
311
312
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
313

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

318
319
320
321
322
323
324
325
    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()
Rizkhi PH's avatar
Rizkhi PH committed
326
            return HttpResponseRedirect(ADMINISTRATION_MANAGEMENT)
327
328
329
330
331
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

332
333

class EditVerificationView(TemplateView):
334
    template_name = "edit_settings.html"
335
336
337
338
339
340
341
342

    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)
343
        context["page_title"] = "Verifikasi Materi"
344
345
        context["item"] = VerificationSetting.objects.get(id=kwargs["pk"])
        context["form"] = VerificationSettingForm(instance=context["item"])
346
347
348
349
350
351
        return context

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

352
353
354
355
356
357
    def post(self, request, *args, **kwargs):
        verification_object = self.get_context_data(**kwargs)["item"]
        form = VerificationSettingForm(
            request.POST, instance=verification_object)
        if form.is_valid():
            form.save()
358
            return HttpResponseRedirect(ADMINISTRATION_VERIFICATION_SETTING)
359
360
361
362
363
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
class EditCategoryView(TemplateView):
    template_name = "edit_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(EditCategoryView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(EditCategoryView, self).get_context_data(**kwargs)
        context["page_title"] = "Kategori Materi"
        context["item"] = Category.objects.get(id=kwargs["pk"])
        context["form"] = CategoryForm(instance=context["item"])
        return context

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

    def post(self, request, *args, **kwargs):
        category_object = self.get_context_data(**kwargs)["item"]
        form = CategoryForm(
            request.POST, instance=category_object)
        if form.is_valid():
            form.save()
390
            return HttpResponseRedirect(ADMINISTRATION_CATEGORY_SETTING)
391
392
393
394
395
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

Rizkhi PH's avatar
Rizkhi PH committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
class EditAdminStatusView(TemplateView):
    template_name = "edit_admin.html"

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

    def get_context_data(self, **kwargs):
        context = super(EditAdminStatusView, self).get_context_data(**kwargs)
        context["page_title"] = "Admin Status"
        context["item"] = User.objects.get(id=kwargs["pk"])
        context["form"] = EditAdminStatusForm(instance=context["item"])
        return context

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

    def post(self, request, *args, **kwargs):
        user_object = self.get_context_data(**kwargs)["item"]
        if user_object.is_superuser:
            raise PermissionDenied(request)
        form = EditAdminStatusForm(
            request.POST, instance=user_object)
        form.save()
        return HttpResponseRedirect(ADMINISTRATION_MANAGEMENT)
423

Arief Pratama's avatar
Arief Pratama committed
424
425

class EditKontributorStatusView(TemplateView):
426
    template_name = "edit_admin.html"
Arief Pratama's avatar
Arief Pratama committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

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

    def get_context_data(self, **kwargs):
        context = super(EditKontributorStatusView, self).get_context_data(**kwargs)
        context["page_title"] = "Admin Status"
        context["item"] = User.objects.get(id=kwargs["pk"])
        context["form"] = EditKontributorStatusForm(instance=context["item"])
        return context

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

    def post(self, request, *args, **kwargs):
        user_object = self.get_context_data(**kwargs)["item"]
        if not user_object.is_contributor:
            raise PermissionDenied(request)
        form = EditKontributorStatusForm(
            request.POST, instance=user_object)
        form.save()
        return HttpResponseRedirect('/administration/kelola-kontributor/')


454
def delete_admin(request, *args, **kwargs):
455
456
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
457
458

    user_to_be_deleted = User.objects.get(pk=kwargs["pk"])
Rizkhi PH's avatar
Rizkhi PH committed
459
460
461
    if (user_to_be_deleted.is_superuser):
        raise PermissionDenied(request)

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
462
463
464
465
466
467
    DeletionHistory.objects.create(
        deleted_user_name=user_to_be_deleted.name, 
        deleted_user_role="Admin",
        deletor_admin=request.user    
    )

468
    User.objects.filter(pk=kwargs["pk"]).delete()
Rizkhi PH's avatar
Rizkhi PH committed
469
    return HttpResponseRedirect(ADMINISTRATION_MANAGEMENT)
470

471

472
def delete_contributor(request, *args, **kwargs):
473
474
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
475
476
477
478
479
480
481
482
    
    user_to_be_deleted = User.objects.get(pk=kwargs["pk"])
    DeletionHistory.objects.create(
        deleted_user_name=user_to_be_deleted.name, 
        deleted_user_role="Kontributor",
        deletor_admin=request.user    
    )

483
    User.objects.filter(pk=kwargs["pk"]).delete()
484
    return HttpResponseRedirect("/administration/kelola-kontributor/")
485
486
487


def delete_verification(request, *args, **kwargs):
488
489
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
490
    query_object = get_object_or_404(VerificationSetting,
491
                                    pk=kwargs["pk_verification"])
492
493
    query_object.archived = True
    query_object.description = "Telah dihapus pada " + \
Arief Pratama's avatar
Arief Pratama committed
494
        str(timezone.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
495
496
    query_object.archived_by = request.user
    query_object.save()
497
    messages.success(request, "Point verifikasi berhasil dihapus")
498
    return HttpResponseRedirect(ADMINISTRATION_VERIFICATION_SETTING)
499
500
501
502
503
504
505


def delete_category(request, *args, **kwargs):
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
    category_name = get_object_or_404(Category,
                                      pk=kwargs["pk_category"]).name
506
    query_object = get_object_or_404(Category,
507
                                    pk=kwargs["pk_category"])
508
509
    query_object.archived = True
    query_object.description = "Telah dihapus pada " + \
Arief Pratama's avatar
Arief Pratama committed
510
        str(timezone.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
511
512
    query_object.archived_by = request.user
    query_object.save()
513
    messages.success(request, "Kategori " +
514
                     category_name + " berhasil dihapus")
515
    return HttpResponseRedirect(ADMINISTRATION_CATEGORY_SETTING)
516
517
518
519
520
521
522
523
524
525
526
527
528

def generatedummy(request):
    if request.user.is_authenticated is False or request.user.is_admin is False:
        return PermissionDenied(request)
    materi = request.GET.get("materi", None)
    visitor = request.GET.get("visitor", None)
    if materi is None or visitor is None:
        return JsonResponse({"success":False})
    materi = int(materi)
    visitor = int(visitor)
    # RUn management command
    management.call_command('generatedummy', materi)
    management.call_command('generatetraffic', visitor)
529
530
531
532
533
534
535
536
537
538
539
540
541
    return JsonResponse({"success":True,"materi":materi,"visitor":visitor})

class KelolaMateriView(TemplateView):
    template_name = "kelola_materi.html"

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

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

    def get_context_data(self, **kwargs):
        context = super(KelolaMateriView, self).get_context_data(**kwargs)
542
        context['materi_list'] = Materi.all_objects.all()
543
544
545
546
547
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)
548
549
550
551
552
553
554
555
556
557
558

class LaporanMateriView(TemplateView):
    template_name = "laporan_materi.html"
    
    def dispatch(self, request, *args, **kwargs):   
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(LaporanMateriView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        context = super(LaporanMateriView, self).get_context_data(**kwargs)
559
560
561
562
563
        laporan_materi, materi_dilaporkan, \
        materi_diblokir = LaporanMateriService.initilize_report()
        context["laporan_materi"] = laporan_materi
        context["materi_dilaporkan"] = materi_dilaporkan
        context["materi_diblokir"] = materi_diblokir
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
        return self.render_to_response(context=context)

class LaporanMateriDetailView(TemplateView):
    template_name = "laporan_materi_detail.html"
    
    def dispatch(self, request, *args, **kwargs):   
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(LaporanMateriDetailView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        context = super(LaporanMateriDetailView, self).get_context_data(**kwargs)
        context["laporan_materi"] = LaporanMateri.objects.filter(materi_id=kwargs["pk"], is_rejected=False)
        context["materi"] = get_object_or_404(Materi, id=kwargs["pk"])
        return self.render_to_response(context=context)

def blok_materi(request, *args, **kwargs):
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
    
    materi = get_object_or_404(Materi, pk=kwargs["pk"])
    if materi.status == "APPROVE" and LaporanMateri.objects.filter(is_rejected=False, materi_id=materi.id):
        materi.status = "BLOCKED"
        materi.save()

    return HttpResponseRedirect(ADMINISTRATION_REPORT)

def tolak_laporan(request, *args, **kwargs):
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
    
    laporan = get_object_or_404(LaporanMateri, pk=kwargs["pk"])
    laporan.is_rejected = True
    laporan.save()

    return HttpResponseRedirect(ADMINISTRATION_REPORT + str(laporan.materi_id))