Fakultas Ilmu Komputer UI

views.py 4.65 KB
Newer Older
Samuel Dimas's avatar
Samuel Dimas committed
1
2
3
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
igor lestin sianipar's avatar
igor lestin sianipar committed
4
from django.views.generic import TemplateView
5
from django.core import serializers
6
7
from .models import Materi,Category
from django.db.models import Q
igor lestin sianipar's avatar
igor lestin sianipar committed
8
9
10
from .forms import UploadMateriForm
from django.core.exceptions import PermissionDenied

11
12
13
14
15
16
17
18
19

class DaftarKatalog(TemplateView):
    template_name = "app/katalog_materi.html"

    def get_context_data(self, **kwargs):
        return super().get_context_data(**kwargs)

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
20
        context["kategori_list"] = Category.objects.all()
21
        context["materi_list"] = Materi.objects.all()
22
23
24
25
26
27
28
29
30
31
32
        
        getSearch = request.GET.get('search')
        if getSearch:
            list = Materi.objects.all().filter(
                Q(title__icontains = getSearch) |
                Q(descriptions__icontains = getSearch) |
                Q(author__icontains=getSearch) |
                Q(uploader__icontains=getSearch) | 
                Q(publisher__icontains=getSearch)
                ).distinct
            context["materi_list"] = list
Saul Andre's avatar
Saul Andre committed
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        getKategori = request.GET.get("kategori")
        if getKategori:
            pkGet = request.GET.get("kategori")
            kategori = Category.objects.get(pk = pkGet)
            list = Materi.objects.filter(categories = kategori.pk)
            context["materi_list"] = list
            
        getSort = request.GET.get("sort")
        if getSort: 
            list = Materi.objects.all()
            if(getSort == "judul"):
                context["materi_list"] = list.order_by('title')
            elif(getSort == "penulis"):
                context["materi_list"] = list.order_by('author')
            elif(getSort == "pengunggah"):
                context["materi_list"] = list.order_by('uploader')
        
        return self.render_to_response(context=context)
igor lestin sianipar's avatar
igor lestin sianipar committed
52

Saul Andre's avatar
Saul Andre committed
53
54
55
56
57
58
59
60
61
62
63
64
65
class DetailMateri(TemplateView):
    template_name = "app/detail_materi.html"

    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 = super().get_context_data(**kwargs)
        print(context['slug'])
        context["materi_data"] = get_object_or_404(Materi, pk=kwargs['slug'])
        print(context["materi_data"])
        return self.render_to_response(context=context)
66
67


68
69
70



71
72
73
74
75
76
77
78
79
# class DetailMateri(TemplateView):
#     template_name = "app/detail_materi.html"

#     def get_context_data(self, **kwargs):
#         return super().get_context_data(**kwargs)

#     def get(self, request, *args, **kwargs):
#         context = self.get_context_data(**kwargs)
#         return self.render_to_response(context=context)
Samuel Dimas's avatar
Samuel Dimas committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:

        load_template = request.path.split('/')[-1]
        template = loader.get_template(load_template)
        return HttpResponse(template.render(context, request))

    except:

        template = loader.get_template('error-404.html')
        return HttpResponse(template.render(context, request))
igor lestin sianipar's avatar
igor lestin sianipar committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144


def dashboard(request):
    return render(request, 'dashboard.html')


def profile(request):
    return render(request, 'profile.html')


def unggah(request):
    return render(request, 'unggah.html')


def file_text(request):
    return render(request, 'file_text.html')


def file_video(request):
    return render(request, 'file_video.html')


class UploadMateriView(TemplateView):
    template_name = "file_text.html"
    context = {}

    def get_context_data(self, **kwargs):
        context = super(UploadMateriView, self).get_context_data(**kwargs)
        return context

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)
        form = UploadMateriForm(request.POST)
        if form.is_valid():
            materi = form.save(commit=False)
            materi.uploader = request.user
            materi.save()
            return HttpResponseRedirect('unggah')
        else:
            context = self.get_context_data(**kwargs)
            context['form'] = form
            return self.render_to_response(context)

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)
        context = self.get_context_data(**kwargs)
        context['form'] = UploadMateriForm
        return self.render_to_response(context)