Fakultas Ilmu Komputer UI

views.py 23.8 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
12
from administration.services import StatisticService, DetailVerificationService, LaporanMateriService
from app.models import Category, Materi, ViewStatistics, DownloadStatistics, Comment, Like, LaporanMateri
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
90
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context=context)
91

92
93

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

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

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

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

126

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

    def get_context_data(self, **kwargs):
136
        context = super(CategorySettingView, self).get_context_data(**kwargs)
137
138
        context["items"] = Category.objects.filter(archived=False)
        context["items_archived"] = Category.objects.filter(archived=True)
139
        context["page_title"] = "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
        form = CategoryForm(request.POST)
        if form.is_valid():
            form.save()
151
            return HttpResponseRedirect(ADMINISTRATION_CATEGORY_SETTING)
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

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

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

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

    def get_context_data(self, **kwargs):
168
        context = super(KelolaKontributorView, self).get_context_data(**kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
169
        context["users"] = User.objects.filter(is_contributor=True)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
170
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Kontributor")
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
171
172
173
174
175
176
        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
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
190
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Admin")
191
192
193
194
195
        return context

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

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

    def dispatch(self, request, *args, **kwargs):
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
201
        if not request.user.is_authenticated:
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
202
203
204
205
206
207
208
209
210
211
            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
class StatisticsView(TemplateView):
    template_name = "administration/data_statistik.html"

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

222
223
224
225
226
227
    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

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243




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

244
        StatisticService.generate_view_stat(chart_data, colors, data_sets, label, time_step)
245
246
        return chart_data

247

248
249
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
250
251
252
253
254
255
256
257
258
        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)
259
260
261
262
263

        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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

        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)


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

298

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

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

316
317
318
319
320
321
322
323
    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
324
            return HttpResponseRedirect(ADMINISTRATION_MANAGEMENT)
325
326
327
328
329
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

330
331

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

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

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

350
351
352
353
354
355
    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()
356
            return HttpResponseRedirect(ADMINISTRATION_VERIFICATION_SETTING)
357
358
359
360
361
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

362

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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()
388
            return HttpResponseRedirect(ADMINISTRATION_CATEGORY_SETTING)
389
390
391
392
393
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

Rizkhi PH's avatar
Rizkhi PH committed
394
395
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
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)
421

Arief Pratama's avatar
Arief Pratama committed
422
423

class EditKontributorStatusView(TemplateView):
424
    template_name = "edit_admin.html"
Arief Pratama's avatar
Arief Pratama committed
425
426
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

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


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

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

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

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

469

470
def delete_contributor(request, *args, **kwargs):
471
472
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
473
474
475
476
477
478
479
480
    
    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    
    )

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


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


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

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)
527
528
529
530
531
532
533
534
535
536
537
538
539
    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)
540
        context['materi_list'] = Materi.all_objects.all()
541
542
543
544
545
        return context

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

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)
557
558
559
560
561
        laporan_materi, materi_dilaporkan, \
        materi_diblokir = LaporanMateriService.initilize_report()
        context["laporan_materi"] = laporan_materi
        context["materi_dilaporkan"] = materi_dilaporkan
        context["materi_diblokir"] = materi_diblokir
562
563
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
        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))