Fakultas Ilmu Komputer UI

views.py 24.3 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
from app.models import NotifikasiKontributor
16

17
18
from administration.utils import generate_time_step

19
20
from django.core import management

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

26

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

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

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

47

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

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

63

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

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

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

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

        new_kontributor_notif = NotifikasiKontributor(materi=materi, user=materi.uploader, feedback=materi.get_status_display())
        new_kontributor_notif.save()

90
        return HttpResponseRedirect("/administration/")
91

92

93
94
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
95
96
        if(request.user.is_admin):
            AdminNotification.objects.filter(materi=context["materi_data"]).delete()
97
        return self.render_to_response(context=context)
98

99
100

class VerificationSettingView(TemplateView):
101
102
103
104
105
106
    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)
107
108

    def get_context_data(self, **kwargs):
109
110
        context = super(VerificationSettingView,
                        self).get_context_data(**kwargs)
111
        context["items"] = VerificationSetting.objects.filter(
112
            archived=False)
113
114
        context["items_archived"] = VerificationSetting.objects.filter(
            archived=True)
115
        context["page_title"] = "Point Verifikasi Materi"
116
117
118
        return context

    def get(self, request, *args, **kwargs):
119
120
121
        context = self.get_context_data(**kwargs)
        context["form"] = VerificationSettingForm()
        return self.render_to_response(context=context)
122
123

    def post(self, request, *args, **kwargs):
124
125
126
        form = VerificationSettingForm(request.POST)
        if form.is_valid():
            form.save()
127
            return HttpResponseRedirect(ADMINISTRATION_VERIFICATION_SETTING)
128
        else:
129
130
131
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)
132

133

134
class CategorySettingView(TemplateView):
135
136
137
138
139
140
    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)
141
142

    def get_context_data(self, **kwargs):
143
        context = super(CategorySettingView, self).get_context_data(**kwargs)
144
145
        context["items"] = Category.objects.filter(archived=False)
        context["items_archived"] = Category.objects.filter(archived=True)
146
        context["page_title"] = "Kategori Materi"
147
148
149
        return context

    def get(self, request, *args, **kwargs):
150
151
152
        context = self.get_context_data(**kwargs)
        context["form"] = CategoryForm()
        return self.render_to_response(context=context)
153
154

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

164

165
166
class KelolaKontributorView(TemplateView):
    template_name = "kelola_kontributor.html"
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
167
168
169
170
171

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

172
        return super(KelolaKontributorView, self).dispatch(request, *args, **kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
173
174

    def get_context_data(self, **kwargs):
175
        context = super(KelolaKontributorView, self).get_context_data(**kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
176
        context["users"] = User.objects.filter(is_contributor=True)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
177
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Kontributor")
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
178
179
180
181
182
183
        return context

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

184

185
186
187
188
189
190
191
192
193
194
195
196
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
197
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Admin")
198
199
200
201
202
        return context

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

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
204
205
206
207
class ProfileContributorAdminView(TemplateView):
    template_name = "detail_kontri_admin.html"

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

221
222
223
class StatisticsView(TemplateView):
    template_name = "administration/data_statistik.html"

224
225
226
227
228
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            raise PermissionDenied(request)
        return super(StatisticsView, self).dispatch(request, *args, **kwargs)

229
230
231
232
233
234
    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

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250




    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"))

251
        StatisticService.generate_view_stat(chart_data, colors, data_sets, label, time_step)
252
253
        return chart_data

254

255
256
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
257
258
259
260
261
262
263
264
265
        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)
266
267
268
269
270

        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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

        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)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
302
        context["deletion_history"] = DeletionHistory.objects.filter(deletor_admin=get_object_or_404(User, pk=kwargs["pk"]))
303
304
        return self.render_to_response(context=context)

305

306
307
308
309
310
311
312
313
314
315
316
317
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
318

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

323
324
325
326
327
328
329
330
    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
331
            return HttpResponseRedirect(ADMINISTRATION_MANAGEMENT)
332
333
334
335
336
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

337
338

class EditVerificationView(TemplateView):
339
    template_name = "edit_settings.html"
340
341
342
343
344
345
346
347

    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)
348
        context["page_title"] = "Verifikasi Materi"
349
350
        context["item"] = VerificationSetting.objects.get(id=kwargs["pk"])
        context["form"] = VerificationSettingForm(instance=context["item"])
351
352
353
354
355
356
        return context

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

357
358
359
360
361
362
    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()
363
            return HttpResponseRedirect(ADMINISTRATION_VERIFICATION_SETTING)
364
365
366
367
368
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

369

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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()
395
            return HttpResponseRedirect(ADMINISTRATION_CATEGORY_SETTING)
396
397
398
399
400
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

Rizkhi PH's avatar
Rizkhi PH committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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)
428

Arief Pratama's avatar
Arief Pratama committed
429
430

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

    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/')


459
def delete_admin(request, *args, **kwargs):
460
461
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
462
463

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

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
467
468
469
470
471
472
    DeletionHistory.objects.create(
        deleted_user_name=user_to_be_deleted.name, 
        deleted_user_role="Admin",
        deletor_admin=request.user    
    )

473
    User.objects.filter(pk=kwargs["pk"]).delete()
Rizkhi PH's avatar
Rizkhi PH committed
474
    return HttpResponseRedirect(ADMINISTRATION_MANAGEMENT)
475

476

477
def delete_contributor(request, *args, **kwargs):
478
479
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
480
481
482
483
484
485
486
487
    
    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    
    )

488
    User.objects.filter(pk=kwargs["pk"]).delete()
489
    return HttpResponseRedirect("/administration/kelola-kontributor/")
490
491
492


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


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
511
    query_object = get_object_or_404(Category,
512
                                    pk=kwargs["pk_category"])
513
514
    query_object.archived = True
    query_object.description = "Telah dihapus pada " + \
Arief Pratama's avatar
Arief Pratama committed
515
        str(timezone.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
516
517
    query_object.archived_by = request.user
    query_object.save()
518
    messages.success(request, "Kategori " +
519
                     category_name + " berhasil dihapus")
520
    return HttpResponseRedirect(ADMINISTRATION_CATEGORY_SETTING)
521
522
523
524
525
526
527
528
529
530
531
532
533

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)
534
535
536
537
538
539
540
541
542
543
544
545
546
    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)
547
        context['materi_list'] = Materi.all_objects.all()
548
549
550
551
552
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)
553
554
555
556
557
558
559
560
561
562
563

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)
564
565
566
567
568
        laporan_materi, materi_dilaporkan, \
        materi_diblokir = LaporanMateriService.initilize_report()
        context["laporan_materi"] = laporan_materi
        context["materi_dilaporkan"] = materi_dilaporkan
        context["materi_diblokir"] = materi_diblokir
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
        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()
593
594
        new_kontributor_notif = NotifikasiKontributor(materi=materi, user=materi.uploader, feedback=materi.get_status_display())
        new_kontributor_notif.save()
595
596
597
598
599
600
601
602
603
604
605
606

    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))