Fakultas Ilmu Komputer UI

views.py 6.61 KB
Newer Older
1
2
3
4
import mimetypes
import os

from django.conf import settings
5
from django.core import serializers
6
from django.core.exceptions import PermissionDenied
7
from django.db.models import Q
8
9
10
from django.http import (Http404, HttpResponse, HttpResponseRedirect,
                         JsonResponse)
from django.shortcuts import get_object_or_404, redirect, render
Samuel Dimas's avatar
Samuel Dimas committed
11
from django.template import loader
igor lestin sianipar's avatar
igor lestin sianipar committed
12
from django.views.generic import TemplateView
13
from django.core import serializers
14
15
from .models import Materi,Category
from django.db.models import Q
igor lestin sianipar's avatar
igor lestin sianipar committed
16
from .forms import UploadMateriForm
17
from .models import Category, Materi
18

19
20
21
22
23
24
25
26
27

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)
28
        context["kategori_list"] = Category.objects.all()
29
30
31
32
33
        try:
            context["materi_list"] = Materi.objects.all().filter(status = "(\"APPROVE\", \"Diterima\")")
        except:
            context["materi_list"] = None

34
35
        getSearch = request.GET.get('search')
        if getSearch:
36
37
            list = Materi.objects.filter(
                Q(title__icontains=getSearch) | 
38
                Q(author__icontains=getSearch) |
39
40
                Q(uploader__name__icontains=getSearch) |
                Q(descriptions__icontains=getSearch) |
41
                Q(publisher__icontains=getSearch)
42
43
44
45
46
47
48
49
50
                ).distinct()
            # list = Materi.objects.filter(
            #     Q(title__icontains=getSearch) |
            #     Q(descriptions__icontains=getSearch) |
            #     Q(author__icontains=getSearch) |
            #     Q(uploader__icontains=getSearch) | 
            #     Q(publisher__icontains=getSearch)
            #     )
            context["materi_list"] = list.filter(status = "(\"APPROVE\", \"Diterima\")")
51
52
53
54
55
56

        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)
57
            context["materi_list"] = list.filter(status = "(\"APPROVE\", \"Diterima\")")
58
59
60
            
        getSort = request.GET.get("sort")
        if getSort: 
61
            list = Materi.objects.all().filter(status = "(\"APPROVE\", \"Diterima\")")
62
63
64
65
66
67
68
            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')
        
69
70
71
72
73
74
75
76
77
78
        return self.render_to_response(context=context)

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)
79
        context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
80
        return self.render_to_response(context=context)
81

82

83
84
85
86
87
88
89
90
def download_materi(request, pk):
    materi = get_object_or_404(Materi, pk=pk)
    path = materi.content.path
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
        mimetype = mimetypes.guess_type(file_path)
        with open(file_path, "rb") as fh:
            response = HttpResponse(fh.read(), content_type=mimetype[0])
91
92
            response["Content-Disposition"] = "attachment; filename=" + \
                os.path.basename(file_path)
93
94
95
96
            return response
    else:
        raise Http404("File tidak dapat ditemukan.")

97

98
99
100
101
102
103
104
105
def view_materi(request, pk):
    materi = get_object_or_404(Materi, pk=pk)
    path = materi.content.path
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
        mimetype = mimetypes.guess_type(file_path)
        with open(file_path, "rb") as fh:
            response = HttpResponse(fh.read(), content_type=mimetype[0])
106
107
            response["Content-Disposition"] = "inline; filename=" + \
                os.path.basename(file_path)
108
109
110
            return response
    else:
        raise Http404("File tidak dapat ditemukan.")
igor lestin sianipar's avatar
igor lestin sianipar committed
111

112

igor lestin sianipar's avatar
igor lestin sianipar committed
113
class UploadMateriView(TemplateView):
114
    template_name = "unggah.html"
igor lestin sianipar's avatar
igor lestin sianipar committed
115
116
117
118
119
120
121
122
123
    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)
124
        form = UploadMateriForm(request.POST, request.FILES)
igor lestin sianipar's avatar
igor lestin sianipar committed
125
126
127
128
        if form.is_valid():
            materi = form.save(commit=False)
            materi.uploader = request.user
            materi.save()
129
            return HttpResponseRedirect("/unggah/")
igor lestin sianipar's avatar
igor lestin sianipar committed
130
131
        else:
            context = self.get_context_data(**kwargs)
132
            context["form"] = form
igor lestin sianipar's avatar
igor lestin sianipar committed
133
134
135
136
137
138
            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)
139
        context["form"] = UploadMateriForm
igor lestin sianipar's avatar
igor lestin sianipar committed
140
        return self.render_to_response(context)
141
142


143
144
145
class UploadMateriHTML(TemplateView):
    template_name = "unggah.html"
    context = {}
146

147
148
149
150
151
152
    def get_template_names(self):
        if self.request.path == "/unggah/":
            template_name = "unggah.html"
        return template_name


153
class DashboardKontributorView(TemplateView):
154
    template_name = "dashboard.html"
155
156
157
158
159
160
161
162
163
164
165
166
167

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_contributor:
            raise PermissionDenied(request)
        return super(DashboardKontributorView, self).dispatch(request, *args, **kwargs)

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

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


170
171
172
173
174
175
176
177
178
179
180
181
182
183
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))