Fakultas Ilmu Komputer UI

views.py 6.95 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
11
12
13
from django.http import (Http404, HttpResponse, HttpResponseRedirect,
                         JsonResponse)
from django.shortcuts import get_object_or_404, redirect, render
from django.template import loader
from django.views.generic import TemplateView

igor lestin sianipar's avatar
igor lestin sianipar committed
14
from .forms import UploadMateriForm
15
from .models import Category, Materi, Comment
igor lestin sianipar's avatar
igor lestin sianipar committed
16

17
18
19
20
21
22
23
24
25
26

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)
        context["materi_list"] = Materi.objects.all()
27
28

        getSearch = request.GET.get("search")
29
        if getSearch:
30
31
32
33
34
35
36
37
38
39
40
            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
            )
41
            context["materi_list"] = list
Saul Andre's avatar
Saul Andre committed
42

43
44
45
        getKategori = request.GET.get("kategori")
        if getKategori:
            pkGet = request.GET.get("kategori")
46
47
            kategori = Category.objects.get(pk=pkGet)
            list = Materi.objects.filter(categories=kategori.pk)
48
            context["materi_list"] = list
49

50
        getSort = request.GET.get("sort")
51
        if getSort:
52
            list = Materi.objects.all()
53
54
55
56
57
58
59
            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")

60
61
        return self.render_to_response(context=context)

62

63
64
65
66
67
68
69
70
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)
71
        context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
72
73
        return self.render_to_response(context=context)

74
    def post(self, request, *args, **kwargs):
75
        commentText = request.POST.get("comment", None)
76
77
        url = request.path
        materi = get_object_or_404(Materi, pk=kwargs['pk'])
78
        if commentText == None or commentText== '':
79
80
81
82
            context = self.get_context_data(*args, **kwargs)
            context["error_message"] = "Anda belum menuliskan komentar"
            return self.render_to_response(context=context)
        else:
83
            comment = Comment.objects.create(comment=commentText)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            if request.user.is_authenticated:
                if request.user.is_superuser:
                    username = "Admin"
                else:
                    username = request.user.name
                comment.user = username
                comment.save()
                materi.comments.add(comment)
                materi.save()
                return HttpResponseRedirect(url)
            else:
                comment.user = "Anonim"
                comment.save()
                materi.comments.add(comment)
                materi.save()
                return HttpResponseRedirect(url)
    
def delete_comment(request, pk_materi, pk_comment):
    materi = get_object_or_404(Materi, pk=pk_materi)
    comment = get_object_or_404(Comment, pk=pk_comment)
    url = '/materi/' + str(pk_materi) + "/"
    materi.comments.remove(comment)
    comment.delete()
    return HttpResponseRedirect(url)

109
110
111
112
113
114
115
def method1(param1, param2):
    temp1 = param1*param2
    return temp1

def total_gaji_setahun(gaji, jml_bulan):
    total_gaji = gaji*jml_bulan
    return total_gaji
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
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])
            response["Content-Disposition"] = "attachment; filename=" + os.path.basename(file_path)
            return response
    else:
        raise Http404("File tidak dapat ditemukan.")


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])
            response["Content-Disposition"] = "inline; filename=" + os.path.basename(file_path)
            return response
    else:
        raise Http404("File tidak dapat ditemukan.")
igor lestin sianipar's avatar
igor lestin sianipar committed
143
144
145


def dashboard(request):
146
    return render(request, "dashboard.html")
igor lestin sianipar's avatar
igor lestin sianipar committed
147
148
149


def profile(request):
150
    return render(request, "profile.html")
igor lestin sianipar's avatar
igor lestin sianipar committed
151
152
153


def unggah(request):
154
    return render(request, "unggah.html")
igor lestin sianipar's avatar
igor lestin sianipar committed
155
156
157


def file_text(request):
158
    return render(request, "file_text.html")
igor lestin sianipar's avatar
igor lestin sianipar committed
159
160
161


def file_video(request):
162
    return render(request, "file_video.html")
igor lestin sianipar's avatar
igor lestin sianipar committed
163
164
165
166
167
168
169
170
171
172


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

Saul Andre's avatar
Saul Andre committed
173
    def post(self, request, *args, **kwargs):
igor lestin sianipar's avatar
igor lestin sianipar committed
174
175
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)
176
        form = UploadMateriForm(request.POST, request.FILES)
igor lestin sianipar's avatar
igor lestin sianipar committed
177
178
179
180
        if form.is_valid():
            materi = form.save(commit=False)
            materi.uploader = request.user
            materi.save()
181
            return HttpResponseRedirect("/unggah/")
Saul Andre's avatar
Saul Andre committed
182
        else:
igor lestin sianipar's avatar
igor lestin sianipar committed
183
            context = self.get_context_data(**kwargs)
184
            context["form"] = form
igor lestin sianipar's avatar
igor lestin sianipar committed
185
186
187
188
189
190
            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)
191
        context["form"] = UploadMateriForm
igor lestin sianipar's avatar
igor lestin sianipar committed
192
        return self.render_to_response(context)
193

Samuel Dimas's avatar
Samuel Dimas committed
194
195
196
197
198
199
200

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

201
        load_template = request.path.split("/")[-1]
Samuel Dimas's avatar
Samuel Dimas committed
202
203
204
205
206
        template = loader.get_template(load_template)
        return HttpResponse(template.render(context, request))

    except:

207
        template = loader.get_template("error-404.html")
Samuel Dimas's avatar
Samuel Dimas committed
208
        return HttpResponse(template.render(context, request))