Fakultas Ilmu Komputer UI

views.py 16.4 KB
Newer Older
1
2
import json

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

15
16

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

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

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

37

38
39
40
41
42
43
44
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)
45

46
47
48
    def get_context_data(self, **kwargs):
        context = super(DetailVerificationView,
                        self).get_context_data(**kwargs)
49
50
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
        context["materi_data"] = materi
51
52
        context["kriteria_list"] = VerificationSetting.objects.filter(
            archived=False)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
53
54
55
56
        if materi.status == "PENDING" or materi.status == "REVISION":
            riwayat = False
        else:
            riwayat = True
57
58
        context["riwayat"] = riwayat
        context["verification_report"] = materi.verificationreport_set.first()
59
60
61
62
        return context

    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        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 != "":
83
84
            materi.status = "APPROVE"
            materi.save()
85
        elif action == "disapprove" and feedback != "":
86
87
88
89
90
91
            materi.status = "DISAPPROVE"
            materi.save()
        else:
            context = self.get_context_data(**kwargs)
            context["error"] = True
            return self.render_to_response(context=context)
92
93
94
        verif_report = VerificationReport(
            report=report, materi=materi, user=self.request.user, status=materi.get_status_display())
        verif_report.save()
95
        return HttpResponseRedirect("/administration/")
96

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

101
102

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

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

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

    def post(self, request, *args, **kwargs):
126
127
128
129
        form = VerificationSettingForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect("/administration/setting/verification/")
130
        else:
131
132
133
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)
134

135

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

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

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

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

166

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

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

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

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

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

186

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

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

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

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

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
class StatisticsView(TemplateView):
    template_name = "administration/data_statistik.html"

    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

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        form = context["periodForm"]

        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)


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
266
        context["deletion_history"] = DeletionHistory.objects.filter(deletor_admin=get_object_or_404(User, pk=kwargs["pk"]))
267
268
        return self.render_to_response(context=context)

269

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

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

287
288
289
290
291
292
293
294
295
296
297
298
299
300
    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)

301
302

class EditVerificationView(TemplateView):
303
    template_name = "edit_settings.html"
304
305
306
307
308
309
310
311

    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)
312
        context["page_title"] = "Verifikasi Materi"
313
314
        context["item"] = VerificationSetting.objects.get(id=kwargs["pk"])
        context["form"] = VerificationSettingForm(instance=context["item"])
315
316
317
318
319
320
        return context

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

321
322
323
324
325
326
327
328
329
330
331
332
    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)

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


366
def delete_admin(request, *args, **kwargs):
367
368
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
369
370
371
372
373
374
375
376

    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    
    )

377
378
379
    User.objects.filter(pk=kwargs["pk"]).delete()
    return HttpResponseRedirect("/administration/kelola-admin/")

380

381
def delete_contributor(request, *args, **kwargs):
382
383
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
384
385
386
387
388
389
390
391
    
    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    
    )

392
    User.objects.filter(pk=kwargs["pk"]).delete()
393
    return HttpResponseRedirect("/administration/kelola-kontributor/")
394
395
396


def delete_verification(request, *args, **kwargs):
397
398
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
399
400
401
    queryObject = get_object_or_404(VerificationSetting,
                                    pk=kwargs["pk_verification"])
    queryObject.archived = True
402
403
404
    queryObject.description = "Telah dihapus pada " + \
        str(datetime.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
    queryObject.archived_by = request.user
405
    queryObject.save()
406
    messages.success(request, "Point verifikasi berhasil dihapus")
407
    return HttpResponseRedirect("/administration/setting/verification/")
408
409
410
411
412
413
414


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
415
416
417
    queryObject = get_object_or_404(Category,
                                    pk=kwargs["pk_category"])
    queryObject.archived = True
418
419
420
    queryObject.description = "Telah dihapus pada " + \
        str(datetime.now().strftime("%m/%d/%Y, %H:%M:%S")) + " WIB"
    queryObject.archived_by = request.user
421
422
    queryObject.save()
    messages.success(request, "Kategori " +
423
424
                     category_name + " berhasil dihapus")
    return HttpResponseRedirect("/administration/setting/category/")