Fakultas Ilmu Komputer UI

views.py 23.2 KB
Newer Older
1
import json
2
from datetime import datetime, date
3

4
from django.core.exceptions import PermissionDenied
5
from django.contrib.auth.hashers import make_password
6
from django.http import HttpResponseRedirect, JsonResponse
7
from django.shortcuts import get_object_or_404, render
8
from django.views.generic import TemplateView, View
9
from django.contrib import messages
10
11
from django.utils import timezone, lorem_ipsum
from dateutil.relativedelta import relativedelta
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
12
from administration.models import VerificationReport, VerificationSetting, DeletionHistory
13
from administration.forms import CategoryForm, VerificationSettingForm, RegistrasiAdminForm, PeriodForm
14
from app.models import Category, Materi, ViewStatistics, DownloadStatistics, Comment, Like, getRandomColor
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
15
from authentication.models import User
16
from datetime import datetime
17

18
19
from administration.utils import generate_time_step

20
21
from django.core import management

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

def get_start_end_date(period):
    if period == 'ALL_TIME':
        return (None, None)
    elif period.split("_")[0] == "LAST":
        end = timezone.now()
        delta = 7
        if period.split("_")[1] == "WEEK":
            delta = 7
        if period.split("_")[1] == "MONTH":
            delta = 30
        if period.split("_")[1] == "QUARTER":
            delta = 90
        if period.split("_")[1] == "YEAR":
            delta = 365
        start = end - relativedelta(days=delta)
        return (start, end)
    elif period.split("_")[0] == "CURRENT":
        start = timezone.now()
        if period.split("_")[1] == "MONTH":
            start = start.replace(day=1, hour=0, minute=0,
                                  second=0, microsecond=0)
            end = start + relativedelta(months=1)
            return (start, end)
        elif period.split("_")[1] == "YEAR":
            start = start.replace(month=1, day=1, hour=0,
                                  minute=0, second=0, microsecond=0)
            end = start + relativedelta(years=1)
            return (start, end)
    elif period.split("_")[0] == "MINUS":
        start = timezone.now()
        delta = int(period.split("_")[1])
        if period.split("_")[2] == "MONTH":
            start = start.replace(day=1, hour=0, minute=0,
                                  second=0, microsecond=0)
            start = start - relativedelta(months=delta)
            end = start + relativedelta(months=1)
            return (start, end)
        elif period.split("_")[2] == "YEAR":
            start = start.replace(month=1, day=1, hour=0,
                                  minute=0, second=0, microsecond=0)
            start = start - relativedelta(years=delta)
            end = start + relativedelta(years=1)
            return (start, end)
    else:
        return (None, None)

69

70
71
72
class VerificationView(TemplateView):
    template_name = "verif.html"

73
74
75
76
77
78
79
    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)
80
81
82
83
        context["materi_list_pending"] = Materi.objects.filter(
            status='PENDING')
        context["materi_list_history"] = Materi.objects.exclude(
            status='PENDING')
84
85
        return context

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

90

91
92
93
94
95
96
97
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)
98

99
100
101
    def get_context_data(self, **kwargs):
        context = super(DetailVerificationView,
                        self).get_context_data(**kwargs)
102
103
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
        context["materi_data"] = materi
104
105
        context["kriteria_list"] = VerificationSetting.objects.filter(
            archived=False)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
106
107
108
109
        if materi.status == "PENDING" or materi.status == "REVISION":
            riwayat = False
        else:
            riwayat = True
110
111
        context["riwayat"] = riwayat
        context["verification_report"] = materi.verificationreport_set.first()
112
113
114
115
        return context

    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        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 != "":
136
137
            materi.status = "APPROVE"
            materi.save()
138
        elif action == "disapprove" and feedback != "":
139
140
141
142
143
144
            materi.status = "DISAPPROVE"
            materi.save()
        else:
            context = self.get_context_data(**kwargs)
            context["error"] = True
            return self.render_to_response(context=context)
145
146
147
        verif_report = VerificationReport(
            report=report, materi=materi, user=self.request.user, status=materi.get_status_display())
        verif_report.save()
148
        return HttpResponseRedirect("/administration/")
149

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

154
155

class VerificationSettingView(TemplateView):
156
157
158
159
160
161
    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)
162
163

    def get_context_data(self, **kwargs):
164
165
        context = super(VerificationSettingView,
                        self).get_context_data(**kwargs)
166
        context["items"] = VerificationSetting.objects.filter(
167
            archived=False)
168
169
        context["items_archived"] = VerificationSetting.objects.filter(
            archived=True)
170
        context["page_title"] = "Point Verifikasi Materi"
171
172
173
        return context

    def get(self, request, *args, **kwargs):
174
175
176
        context = self.get_context_data(**kwargs)
        context["form"] = VerificationSettingForm()
        return self.render_to_response(context=context)
177
178

    def post(self, request, *args, **kwargs):
179
180
181
182
        form = VerificationSettingForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/administration/setting/verification/")
183
        else:
184
185
186
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)
187

188

189
class CategorySettingView(TemplateView):
190
191
192
193
194
195
    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)
196
197

    def get_context_data(self, **kwargs):
198
        context = super(CategorySettingView, self).get_context_data(**kwargs)
199
200
        context["items"] = Category.objects.filter(archived=False)
        context["items_archived"] = Category.objects.filter(archived=True)
201
        context["page_title"] = "Kategori Materi"
202
203
204
        return context

    def get(self, request, *args, **kwargs):
205
206
207
        context = self.get_context_data(**kwargs)
        context["form"] = CategoryForm()
        return self.render_to_response(context=context)
208
209

    def post(self, request, *args, **kwargs):
210
211
212
213
        form = CategoryForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/administration/setting/category/")
214
        else:
215
216
217
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
218

219

220
221
class KelolaKontributorView(TemplateView):
    template_name = "kelola_kontributor.html"
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
222
223
224
225
226

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

227
        return super(KelolaKontributorView, self).dispatch(request, *args, **kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
228
229

    def get_context_data(self, **kwargs):
230
        context = super(KelolaKontributorView, self).get_context_data(**kwargs)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
231
        context["users"] = User.objects.filter(is_contributor=True)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
232
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Kontributor")
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
233
234
235
236
237
238
        return context

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

239

240
241
242
243
244
245
246
247
248
249
250
251
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
252
        context["deletion_history"] = DeletionHistory.objects.filter(deleted_user_role="Admin")
253
254
255
256
257
        return context

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

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
259
260
261
262
class ProfileContributorAdminView(TemplateView):
    template_name = "detail_kontri_admin.html"

    def dispatch(self, request, *args, **kwargs):
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
263
        if not request.user.is_authenticated:
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
264
265
266
267
268
269
270
271
272
273
            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"])
274
        return self.render_to_response(context=context)
275

276
277
278
class StatisticsView(TemplateView):
    template_name = "administration/data_statistik.html"

279
280
281
282
283
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            raise PermissionDenied(request)
        return super(StatisticsView, self).dispatch(request, *args, **kwargs)

284
285
286
287
288
289
    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

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    def get_filter_set(self, **kwargs):
        filter_set = {}
        form = PeriodForm(self.request.GET)
        DEFAULT_PERIOD = "LAST_MONTH"
        if form.is_valid():
            form_data = form.cleaned_data
            if len(form_data["categories"]) == 0:
                form_data["categories"] = None

            if form_data["period"] == "":
                form_data["period"] = DEFAULT_PERIOD

            # check if using custom date range
            if form_data["start_date"] is not None and form_data["end_date"] is not None:
                start = timezone.make_aware(datetime.combine(
                    form_data["start_date"], datetime.min.time()))
                end = timezone.make_aware(datetime.combine(
                    form_data["end_date"], datetime.min.time()))
            elif form_data["period"] is not None:
                start, end = get_start_end_date(form_data["period"])
            else:
                start, end = get_start_end_date(DEFAULT_PERIOD)
            filter_set["start"] = start
            filter_set["end"] = end
            filter_set["categories"] = form_data["categories"]
        return filter_set

    def get_query_set(self, object_statistic, **kwargs):
        if self.request.user.is_admin:
            return object_statistic.objects.filter(materi__status="APPROVE")
        else:
            return object_statistic.objects.filter(materi__status="APPROVE", materi__uploader=self.request.user.id)

    def filter_qs(self, qs, filter_set):
        if filter_set.get("categories", None) is not None:
            qs = qs.filter(materi__categories__in=filter_set.get("categories"))
        if filter_set.get("start") is not None and filter_set.get("end") is not None:
            s = filter_set.get("start")
            e = filter_set.get("end")
            qs = qs.filter(timestamp__gte=s, timestamp__lt=e)
        return qs.distinct()

    # def generate_table_data(self, data_sets):
    #     table_data = {}

    #     counter = 1
    #     row_materi_table = []
    #     for item in data_sets:

    #         # return res

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

        # generate view stat
        for label, color, dataset in zip(label, colors, data_sets):
            result = {
                "label": label,
                "backgroundColor": color,
                "borderColor": color,
                "borderWidth": 1
            }
            data = []
            total = 0
            s = 0
            e = 1
            while e < len(time_step):
                todays = dataset.filter(
                    timestamp__gte=time_step[s], timestamp__lt=time_step[e]).count()
                data.append(todays)
                total += todays
                s += 1
                e += 1
            result["data"] = data
            chart_data["charts"].append(result)
            chart_data["total"].append(total)
        return chart_data

377
378
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
379
380
381
382
383
384
385
386
387
388
389
390
391
392
        filter_set = self.get_filter_set(**kwargs)
        view_set = self.get_query_set(ViewStatistics, **kwargs)
        view_set = self.filter_qs(view_set, filter_set)
        download_set = self.get_query_set(DownloadStatistics, **kwargs)
        download_set = self.filter_qs(download_set, filter_set)
        like_set = self.get_query_set(Like, **kwargs)
        like_set = self.filter_qs(like_set, filter_set)
        comment_set = self.get_query_set(Comment, **kwargs)
        comment_set = self.filter_qs(comment_set, filter_set)

        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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

        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)


409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
424
        context["deletion_history"] = DeletionHistory.objects.filter(deletor_admin=get_object_or_404(User, pk=kwargs["pk"]))
425
426
        return self.render_to_response(context=context)

427

428
429
430
431
432
433
434
435
436
437
438
439
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
440

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

445
446
447
448
449
450
451
452
453
454
455
456
457
458
    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)

459
460

class EditVerificationView(TemplateView):
461
    template_name = "edit_settings.html"
462
463
464
465
466
467
468
469

    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)
470
        context["page_title"] = "Verifikasi Materi"
471
472
        context["item"] = VerificationSetting.objects.get(id=kwargs["pk"])
        context["form"] = VerificationSettingForm(instance=context["item"])
473
474
475
476
477
478
        return context

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

479
480
481
482
483
484
485
486
487
488
489
490
    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()
            return HttpResponseRedirect("/administration/setting/verification/")
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

491

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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()
            return HttpResponseRedirect("/administration/setting/category/")
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)


524
def delete_admin(request, *args, **kwargs):
525
526
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
527
528
529
530
531
532
533
534

    user_to_be_deleted = User.objects.get(pk=kwargs["pk"])
    DeletionHistory.objects.create(
        deleted_user_name=user_to_be_deleted.name, 
        deleted_user_role="Admin",
        deletor_admin=request.user    
    )

535
536
537
    User.objects.filter(pk=kwargs["pk"]).delete()
    return HttpResponseRedirect("/administration/kelola-admin/")

538

539
def delete_contributor(request, *args, **kwargs):
540
541
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
542
543
544
545
546
547
548
549
    
    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    
    )

550
    User.objects.filter(pk=kwargs["pk"]).delete()
551
    return HttpResponseRedirect("/administration/kelola-kontributor/")
552
553
554


def delete_verification(request, *args, **kwargs):
555
556
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
557
558
559
    queryObject = get_object_or_404(VerificationSetting,
                                    pk=kwargs["pk_verification"])
    queryObject.archived = True
560
561
562
    queryObject.description = "Telah dihapus pada " + \
        str(datetime.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
    queryObject.archived_by = request.user
563
    queryObject.save()
564
    messages.success(request, "Point verifikasi berhasil dihapus")
565
    return HttpResponseRedirect("/administration/setting/verification/")
566
567
568
569
570
571
572


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
573
574
575
    queryObject = get_object_or_404(Category,
                                    pk=kwargs["pk_category"])
    queryObject.archived = True
576
577
578
    queryObject.description = "Telah dihapus pada " + \
        str(datetime.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
    queryObject.archived_by = request.user
579
580
    queryObject.save()
    messages.success(request, "Kategori " +
581
582
                     category_name + " berhasil dihapus")
    return HttpResponseRedirect("/administration/setting/category/")
583
584
585
586
587
588
589
590
591
592
593
594
595
596

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)
    return JsonResponse({"success":True,"materi":materi,"visitor":visitor})