Fakultas Ilmu Komputer UI

views.py 4.94 KB
Newer Older
1
from django.core.exceptions import PermissionDenied
2
from django.http import HttpResponseRedirect
3
from django.shortcuts import get_object_or_404, render
4
from django.views.generic import TemplateView
5
6
7

from app.models import Category, Materi

8
from .forms import CategoryForm, VerificationSettingForm
9
from .models import VerificationSetting
10
11
12


# Create your views here.
13
14
15
class VerificationView(TemplateView):
    template_name = "verif.html"

16
17
18
19
20
21
22
23
24
25
    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)
        context["materi_list"] = Materi.objects.filter(status='PENDING')
        return context

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

30

31
32
33
34
35
36
37
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)
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
    def get_context_data(self, **kwargs):
        context = super(DetailVerificationView,
                        self).get_context_data(**kwargs)
        context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
        context["kriteria_list"] = VerificationSetting.objects.filter(
            archived=False)
        return context

    def post(self, request, *args, **kwargs):
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
        feedback = request.POST.get("feedback")
        if request.POST.get("action") == "approve":
            materi.status = "APPROVE"
            materi.feedback = feedback
            materi.save()
        elif request.POST.get("action") == "disapprove":
            materi.status = "DISAPPROVE"
            materi.feedback = feedback
            materi.save()
        else:
            context = self.get_context_data(**kwargs)
            context["error"] = True
            return self.render_to_response(context=context)
        return HttpResponseRedirect("/administration/")
63

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

68
69
70
71
72

class VerificationSettingView(TemplateView):
    template_name = "setting_verifikasi.html"

    def get_context_data(self, **kwargs):
73
74
75
76
        context = super(VerificationSettingView,
                        self).get_context_data(**kwargs)
        context["verification_settings"] = VerificationSetting.objects.filter(
            archived=False)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        return context

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated == False or request.user.is_admin != True:
            raise PermissionDenied(request)
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = VerificationSettingForm()
            return self.render_to_response(context=context)

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False or request.user.is_admin != True:
            raise PermissionDenied(request)
        else:
            form = VerificationSettingForm(request.POST)
            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)
99
100


101
102
103
104
class CategorySettingView(TemplateView):
    template_name = "setting_category.html"

    def get_context_data(self, **kwargs):
105
        context = super(CategorySettingView, self).get_context_data(**kwargs)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        context["category_settings"] = Category.objects.all()
        return context

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated == False or request.user.is_admin != True:
            raise PermissionDenied(request)
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = CategoryForm()
            return self.render_to_response(context=context)

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False or request.user.is_admin != True:
            raise PermissionDenied(request)
        else:
            form = CategoryForm(request.POST)
            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)