Fakultas Ilmu Komputer UI

views.py 33.3 KB
Newer Older
1
2
import mimetypes
import os
3
4
5
6
import re
import time
import logging
import traceback
7
from io import BytesIO
8

9
import django
10
from decouple import config
11
import pandas as pd
12
from django.conf import settings
13
from django.contrib import messages
14
15
16
from django.contrib.auth.views import PasswordChangeForm
from django.contrib.auth.views import PasswordChangeView
from django.core.exceptions import PermissionDenied, FieldError
17
from django.core.mail import send_mail
18
from django.core.paginator import Paginator
19
from django.db.models import Q, Avg
20
21
from django.http import (Http404, HttpResponse, HttpResponseRedirect,
                         JsonResponse)
22
from django.shortcuts import get_object_or_404, redirect, render
23
from django.template import loader
24
from django.urls import reverse
25
from django.urls import reverse_lazy
26
from django.views import defaults
27
from django.views.generic import TemplateView
28

29
from app.forms import SuntingProfilForm, UploadMateriForm, RatingContributorForm
30
31
32
from app.models import (
    Category,
    Comment,
33
    Review,
34
35
    Materi,
    ReqMaterial,
36
    Rating, RatingContributor,
insan ramadhan's avatar
insan ramadhan committed
37
    SubmitVisitor
38
)
39
from authentication.models import User
40
41
42
43
from app.utils.encryption_util import encrypt, decrypt
from app.utils.email_utility import send_subscription_email, send_gmail
from app.models import SubscribeModel
from datetime import datetime
44
45
46
47
from .services import DafterKatalogService, DetailMateriService, LikeDislikeService, MateriFieldValidationHelperService, \
    DownloadViewMateriHelperService, UploadMateriService, EditProfileService, RevisiMateriService, \
    DownloadHistoryService, GoogleDriveUploadService

48

49
def permission_denied(request, exception, template_name="error_403.html"):
50
51
    return defaults.permission_denied(request, exception, template_name)

52

53
class DaftarKatalog(TemplateView):
igor lestin sianipar's avatar
igor lestin sianipar committed
54
    paginate_by = 2
55
56
57
58
59
60
61
    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)
62
        context["kategori_list"] = Category.objects.all()
63

Mika dabelza abi's avatar
Mika dabelza abi committed
64
        lstMateri = Materi.objects.filter(status="APPROVE").order_by("date_modified")
Mika dabelza abi's avatar
Mika dabelza abi committed
65
66
        url = ""

67
        lstMateri, url = DafterKatalogService.apply_options(lstMateri, request, url)
Saul Andre's avatar
Saul Andre committed
68

69

Mika dabelza abi's avatar
Mika dabelza abi committed
70
        context["materi_list"] = lstMateri
Mika dabelza abi's avatar
Mika dabelza abi committed
71
        paginator = Paginator(context["materi_list"], 15)
72
        page_number = request.GET.get("page")
Mika dabelza abi's avatar
Mika dabelza abi committed
73
74
75
76
        page_obj = paginator.get_page(page_number)
        context["materi_list"] = page_obj

        context["url"] = url
77
78
        return self.render_to_response(context=context)

79

80
81
class KatalogPerKontributorView(TemplateView):
    template_name = "app/katalog_kontri.html"
82

83
84
85
86
87
88
89
90
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        contributor = get_object_or_404(User, email=kwargs["email"])
        context["contributor"] = contributor
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
91
92
93
94
95

        materi_list = Materi.objects.filter(status="APPROVE", uploader=context["contributor"]).order_by(
            "date_modified"
        )

96
        paginator = Paginator(materi_list, 15)
97
        page_number = request.GET.get("page")
98
99
        materi_list_by_page = paginator.get_page(page_number)
        context["materi_list"] = materi_list_by_page
100
101
102
103
104
        contributor = context["contributor"]
        context["form_rating"] = RatingContributorForm(initial={
            "contributor": contributor,
            "user":request.user
        })
105
        context["avg_rating"] = User.objects.filter(email=kwargs["email"]) \
106
107
108
109
110
111
112
            .annotate(avg_rating=Avg("contributor__score"))[0]
        context["count_rating"] = RatingContributor.objects.filter(contributor=contributor).count()

        if request.user.is_authenticated:
            has_rated = RatingContributor.objects.filter(user=request.user, contributor=contributor).exists()
            context["has_rated"] = has_rated

113
        return self.render_to_response(context=context)
114

115
    def post(self, request, *args, **kwargs):
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
        context = self.get_context_data(**kwargs)
        if not request.user.is_authenticated:
            raise PermissionDenied(request)

        is_delete = request.POST.get('delete', None)
        is_update = request.POST.get('update', None)
        if is_delete:
            rating_contributor = get_object_or_404(
                RatingContributor, 
                user=request.user,
                contributor=context["contributor"]
            )
            rating_contributor.delete()
        elif is_update:
            score = request.POST.get('score', None)
            rating = RatingContributor.objects.filter(
                user=request.user,
                contributor=context["contributor"]
            ).first()
            
            if rating and score:
                rating.score = int(score)
                rating.save()
        else:
            data = RatingContributorForm(request.POST)
            if data.is_valid():
                data.save()
                
144
145
        return redirect("katalog-per-kontributor", email=kwargs["email"])

146
147
148
149
class DetailMateri(TemplateView):
    template_name = "app/detail_materi.html"

    def get_context_data(self, **kwargs):
150
151
152
153
        context = super(DetailMateri, self).get_context_data(**kwargs)
        if not self.request.session or not self.request.session.session_key:
            self.request.session.save()
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
154
155
156
        DetailMateriService.init_context_data(context, materi, self.request.session)
        published_date = DetailMateriService.set_published_date(materi)
        DetailMateriService.init_citation_and_materi_rating(context, materi, published_date, self.request)
157
        DetailMateriService.init_materi_download_count(context, materi)
158
159
160
161

        if self.request.user.is_authenticated:
            materi_rating = Rating.objects.filter(materi=materi, user=self.request.user).first()
            if materi_rating is not None:
162
                context['materi_rating_score'] = materi_rating.score
163

164
        context['is_authenticated'] = self.request.user.is_authenticated
165

166
        return context
167

168

169
170
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
171
        query_set_for_comment = Comment.objects.filter(materi=context["materi_data"])
172
        query_set_for_review = Review.objects.filter(materi=context["materi_data"])
173
        has_disliked, has_liked = DetailMateriService.find_comment_like_dislike(query_set_for_comment, self.request.session)
174
        context["comment_data"] = query_set_for_comment
175
        context["review_data"] = query_set_for_review
176
177
        context["has_liked_comment"] = has_liked
        context["has_disliked_comment"] = has_disliked
178
179
        return self.render_to_response(context=context)

180

181

182
    def post(self, request, *args, **kwargs):
183
184
185
        comment_text = request.POST.get("comment", None)
        review_text  = request.POST.get("review", None)
        if ((comment_text == None or comment_text == "" )and (review_text == None or review_text == "")):
186
187
            context = self.get_context_data(*args, **kwargs)
            context["error_message"] = "Anda belum menuliskan komentar"
188
            context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
189
            query_set_for_comment = Comment.objects.filter(materi=context["materi_data"])
190
            context["comment_data"] = query_set_for_comment
191
192
            query_set_for_review = Review.objects.filter(materi=context["materi_data"])
            context["review_data"] = query_set_for_review
193
            return self.render_to_response(context=context)
194

195
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
196
        user_obj = request.user if request.user.is_authenticated else None
197
        if user_obj:
198
            if (comment_text != None ):
199
                comment = Comment.objects.create(
200
                    comment=comment_text, username=DetailMateriService.get_user_name(request), materi=materi, user=user_obj
201
202
                )
                comment.save()
203
204
205
206
207
208
209
210
211
212
213
214
                materi_uploader = materi.uploader
                if materi_uploader.is_subscribing_to_material_comments and user_obj.email != materi_uploader.email:
                    email_content =  f'User dengan email {user_obj.email} ' + \
                        f'menambahkan komentar pada materi Anda dengan judul "{materi.title}".' + \
                        f'\nKomentar: "{comment.comment}".\n' + \
                        f'Silahkan akses halaman detail materi untuk berinteraksi lebih lanjut.'
                    send_mail(
                        subject = 'DIGIPUS: Komentar Baru pada Materi Anda',
                        message = email_content,
                        from_email = getattr(settings, 'EMAIL_HOST_USER'),
                        recipient_list = [materi_uploader.email,],
                        fail_silently = False,
215
                    )
216
            elif (review_text != None):
217
                review = Review.objects.create(
218
                    review=review_text, username=DetailMateriService.get_user_name(request), materi=materi, user=user_obj
219
220
                )
                review.save()
221
222
        return HttpResponseRedirect(request.path)

223

224
def toggle_like(request):
225
226
227
    if request.method == "POST":
        materi_id = request.POST.get("materi_id", None)
        session_id = request.POST.get("session_id", None)
228
229
        if materi_id is None or session_id is None:
            return JsonResponse({"success": False, "msg": "Missing parameter"})
230
        return JsonResponse(LikeDislikeService.apply_like_materi(materi_id, session_id))
231
232
233
234
    else:
        return JsonResponse({"success": False, "msg": "Unsuported method"})


235
def delete_comment(request, pk_materi, pk_comment):
236
237
    if not request.user.is_authenticated or not request.user.is_admin:
        raise PermissionDenied(request)
238
    comment = get_object_or_404(Comment, pk=pk_comment)
239
    url = "/materi/" + str(pk_materi) + "/"
240
241
242
    comment.delete()
    return HttpResponseRedirect(url)

243
def toggle_like_comment(request):
244
    comment_id = 0
245
246
247
248
249
    if request.method == "POST":
        comment_id = request.POST.get("comment_id", None)
        session_id = request.POST.get("session_id", None)
        if comment_id is None or session_id is None:
            return JsonResponse({"success": False, "msg": "Missing parameter", "comment_id": comment_id})
250
        return JsonResponse(LikeDislikeService.apply_comment_like(comment_id, session_id))
251
252
253
    else:
        return JsonResponse({"success": False, "msg": "Unsuported method", "comment_id": comment_id})

254

255
def toggle_dislike_comment(request):
256
    comment_id = 0
257
258
259
260
261
    if request.method == "POST":
        comment_id = request.POST.get("comment_id", None)
        session_id = request.POST.get("session_id", None)
        if comment_id is None or session_id is None:
            return JsonResponse({"success": False, "msg": "Missing parameter", "comment_id": comment_id})
262
        return JsonResponse(LikeDislikeService.apply_comment_dislike(comment_id, session_id))
263
264
    else:
        return JsonResponse({"success": False, "msg": "Unsuported method", "comment_id": comment_id})
265
266


267
def add_rating_materi(request):
268
    if request.method == "POST" and request.user.is_authenticated:
269

270
271
        materi_id = request.POST.get("materi_id", None)
        rating_score = request.POST.get("rating_score", None)
272
273


274
275
276
277
        is_valid_params, materi_id, \
        rating_score, response, \
        status_code = MateriFieldValidationHelperService.\
            validate_materi_rating_params(materi_id,rating_score)
278

279
280
        if not is_valid_params:
            return JsonResponse(response, status=status_code)
281

282
283
284
        is_valid_rating, materi, \
        response, status_code = MateriFieldValidationHelperService.\
            validate_materi_rating(materi_id, request.user)
285

286
287
        if not is_valid_rating:
            return JsonResponse(response, status=status_code)
288
289

        Rating(materi=materi, user=request.user, score=rating_score).save()
290
291
292
        return JsonResponse(
            {"success": True, "msg": "Rating successfully created", "rating_score": rating_score}, status=201
        )
293
294
    return JsonResponse({"success": False, "msg": "Forbidden"}, status=403)

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
def subscribeform(request):
    return render(request, 'subscribe.html', )

def subscribe(request):
    post_data = request.POST.copy()
    email = post_data.get("email", None)

    error_msg = validate_email(email)
    if error_msg:
        messages.error(request, error_msg)
        return HttpResponseRedirect(reverse('subscribe'))

    token = encrypt(email + "_" + str(time.time()))
    subscription_confirmation_url = request.build_absolute_uri( reverse('subscription_confirmation')) + "?token=" + token
    status = send_gmail(email, subscription_confirmation_url)
    save_status = save_email(email)

    if save_status:
        token = encrypt(email + "_" + str(time.time()))
        subscription_confirmation_url = request.build_absolute_uri(reverse('subscription_confirmation')) + "?token=" + token
        status = send_gmail(email, subscription_confirmation_url)
        if not status:
            SubscribeModel.objects.get(email=email).delete()
            logging.getLogger("info").info(
                "Deleted the record from Subscribe table for " + email + " as email sending failed. status: " + str(
                    status))
        else:
            msg = "Mail sent to email Id '" + email + "'. Please confirm your subscription by clicking on " \
                                                      "confirmation link provided in email. " \
                                                      "Please check your spam folder as well."
            messages.success(request, msg)
            return HttpResponse(msg)
    else:
        msg = "Some error occurred. Please try in some time. Meanwhile we are looking into it."
        messages.error(request, msg)
        return HttpResponse(msg)
    return  HttpResponseRedirect(reverse('subscribeform'))

def subscription_confirmation(request):
    if "POST" == request.method:
        raise Http404

    token = request.GET.get("token", None)

    if not token:
        logging.getLogger("warning").warning("Invalid Link ")
        messages.error(request, "Invalid Link")
        return HttpResponseRedirect(reverse('subscribeform'))

    token = decrypt(token)
    if token:
        token = token.split("_")
        email = token[0]
        print(email)
        initiate_time = token[1]  # time when email was sent , in epoch format. can be used for later calculations
        try:
            subscribe_model_instance = SubscribeModel.objects.get(email=email)
            subscribe_model_instance.status = "CONFIRMED"
            subscribe_model_instance.updated_date = datetime.today()
            subscribe_model_instance.save()
            messages.success(request, "Subscription Confirmed. Thank you.")
        except Exception as e:
            logging.getLogger("warning").warning(traceback.format_exc())
            messages.error(request, "Invalid Link")
    else:
        logging.getLogger("warning").warning("Invalid token ")
        messages.error(request, "Invalid Link")

    return HttpResponse('<h1>Thank you for subscribing us</h1>')

def validate_email(email):
    if email is None:
        return "Email is required."
    elif not re.match(r"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", email):
        return "Invalid Email Address."
    else:
        return None

def save_email(email):
    try:
        subscribe_model_instance = SubscribeModel.objects.get(email=email)
    except Exception as e:
        subscribe_model_instance = SubscribeModel()


    # does not matter if already subscribed or not...resend the email
    subscribe_model_instance.email = email
    subscribe_model_instance.status = "SUBSCRIBED"
    subscribe_model_instance.created_date = datetime.today()
    subscribe_model_instance.updated_date = datetime.today()
    subscribe_model_instance.save()
    return True
387

388

389
390
391
392
393
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):
394
395
396
        try:
            mimetype = mimetypes.guess_type(file_path)
            with open(file_path, "rb") as fh:
397
398
399
                return DownloadViewMateriHelperService.build_materi_response(fh, file_path,
                                                                             materi, mimetype, request,
                                                                             HttpResponse(fh.read(), content_type=mimetype[0]))
400
401
        except Exception as e:
            raise Http404("File tidak dapat ditemukan.")
402
403
404
    else:
        raise Http404("File tidak dapat ditemukan.")

igor lestin sianipar's avatar
igor lestin sianipar committed
405

406
407
408
409
410
411
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)
412
413
414
        try:
            with open(file_path, "rb") as fh:
                response = HttpResponse(fh.read(), content_type=mimetype[0])
415
                DownloadViewMateriHelperService.build_view_materi_response(file_path, materi, response)
416
417
418
                return response
        except Exception as e:
            raise Http404("File tidak dapat ditemukan.")
419
420
    else:
        raise Http404("File tidak dapat ditemukan.")
igor lestin sianipar's avatar
igor lestin sianipar committed
421

igor lestin sianipar's avatar
igor lestin sianipar committed
422

Selvy Fitriani's avatar
Selvy Fitriani committed
423
424
def delete_materi(request, pk):
    materi = get_object_or_404(Materi, pk=pk)
425
426
427
    if request.user.is_superuser or request.user.is_admin:
        materi.soft_delete()
        return HttpResponseRedirect("/administration/")
Selvy Fitriani's avatar
Selvy Fitriani committed
428
429
430
    materi.delete()
    return HttpResponseRedirect("/dashboard/")

igor lestin sianipar's avatar
igor lestin sianipar committed
431
class UploadMateriView(TemplateView):
432
    template_name = "unggah.html"
igor lestin sianipar's avatar
igor lestin sianipar committed
433
434
435
436
437
438
    context = {}

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

Saul Andre's avatar
Saul Andre committed
439
    def post(self, request, *args, **kwargs):
Steven Kusuman's avatar
Steven Kusuman committed
440
        if request.user.is_authenticated == False or not request.user.is_contributor:
igor lestin sianipar's avatar
igor lestin sianipar committed
441
            raise PermissionDenied(request)
442
        form = UploadMateriForm(request.POST, request.FILES)
igor lestin sianipar's avatar
igor lestin sianipar committed
443
444
445
        if form.is_valid():
            materi = form.save(commit=False)
            materi.uploader = request.user
446
            konten = form.cleaned_data["content"]
447
448
            yt_url_id = form.cleaned_data['yt_video_id']
            if not UploadMateriService.validate_file_extension(konten, request, yt_url_id):
449
                return HttpResponseRedirect("/unggah/")
450
            UploadMateriService.upload_materi(form, materi)
451
            messages.success(request, "Materi berhasil diunggah, periksa riwayat unggah anda")
452
            return HttpResponseRedirect("/unggah/")
Saul Andre's avatar
Saul Andre committed
453
        else:
igor lestin sianipar's avatar
igor lestin sianipar committed
454
            context = self.get_context_data(**kwargs)
455
            context["form"] = form
456
            messages.error(request, "Terjadi kesalahan pada pengisian data")
igor lestin sianipar's avatar
igor lestin sianipar committed
457
458
            return self.render_to_response(context)

459
460


igor lestin sianipar's avatar
igor lestin sianipar committed
461
    def get(self, request, *args, **kwargs):
Steven Kusuman's avatar
Steven Kusuman committed
462
        if request.user.is_authenticated == False or not request.user.is_contributor:
igor lestin sianipar's avatar
igor lestin sianipar committed
463
            raise PermissionDenied(request)
464

igor lestin sianipar's avatar
igor lestin sianipar committed
465
        context = self.get_context_data(**kwargs)
466
        context["form"] = UploadMateriForm
igor lestin sianipar's avatar
igor lestin sianipar committed
467
        return self.render_to_response(context)
468

469

470
471
472
class UploadMateriHTML(TemplateView):
    template_name = "unggah.html"
    context = {}
igor lestin sianipar's avatar
igor lestin sianipar committed
473

474
475
476
477
478
479
    def get_template_names(self):
        if self.request.path == "/unggah/":
            template_name = "unggah.html"
        return template_name


480
481
482
483
484
485
486
487
488
489
490
class UploadMateriExcelView(TemplateView):
    template_name = "unggah_excel.html"
    context = {}

    def get_template_names(self):
        if self.request.path == "/unggah_excel/":
            template_name = "unggah_excel.html"
        return template_name

    def get(self, request, *args, **kwargs):

491
        if "template" in self.request.GET:
492

493
494
495
            data_frame = pd.DataFrame(
                {"Title": [], "Author": [], "Publisher": [], "Categories": [], "Description": [],}
            )
496
497

            with BytesIO() as b:
498
                writer = pd.ExcelWriter(b, engine="xlsxwriter")  # pylint: disable=abstract-class-instantiated
499
500
501
                data_frame.to_excel(writer, index=0)
                writer.save()
                response = HttpResponse(
502
503
                    b.getvalue(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                )
504
505
506
507
508
509
510
511
512
513

                response["Content-Disposition"] = "attachment; filename=template.xlsx"

                return response

        else:
            context = self.get_context_data(**kwargs)
            return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
514
515
        excel_file = request.FILES["excel"]
        excel = pd.read_excel(excel_file)
516

517
        row, lines = excel.shape
518
        categories = Category.objects.all()
519

520
        field_length = {
521
522
523
            "title": 50,
            "author": 30,
            "publisher": 30,
524
525
526
527
528
529
        }

        message = None

        # First pass, validate input
        for i in range(row):
530

531
            # Validate Categories
532
            message = UploadMateriService.validate_excel_categories(categories, excel, i, message)
533

534
            message = UploadMateriService.validate_excel_field_length(excel, field_length, i, message)
535
536
537

            if message != None:
                break
538

539
540
        if message != None:
            messages.error(request, message)
541
            return HttpResponseRedirect("/unggah_excel/")
542
543
544

        # Second pass, save data
        with django.db.transaction.atomic():
545
            UploadMateriService.upload_materi_excel(categories, excel, request, row)
546

547
        messages.success(request, "Materi berhasil diunggah")
548

549
        return HttpResponseRedirect("/unggah_excel/")
550
551


552
553


554
class DashboardKontributorView(TemplateView):
555
    template_name = "dashboard.html"
556
557
558
559
560
561
562

    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):
563
        context = super(DashboardKontributorView, self).get_context_data(**kwargs)
564
565
566
567
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
568
569
570
        current_user = self.request.user
        materi_list = current_user.materi_set.all()
        context["materi_list"] = materi_list
571
        return self.render_to_response(context)
572

573

574
class ProfilView(TemplateView):
575
576
577
    template_name = "profil.html"

    def dispatch(self, request, *args, **kwargs):
578
        if not request.user.is_authenticated:
579
            raise PermissionDenied(request)
580
        return super(ProfilView, self).dispatch(request, *args, **kwargs)
581
582
583

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
584
        current_user = request.user
585
586
587
        context["user"] = current_user
        return self.render_to_response(context)

588

589
590
591
592
class SuntingProfilView(TemplateView):
    template_name = "sunting.html"

    def dispatch(self, request, *args, **kwargs):
593
        if not request.user.is_authenticated:
594
595
596
597
            raise PermissionDenied(request)
        return super(SuntingProfilView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
598
        current_user = self.request.user
599
600
601
602
603
604
605
606
607
608
        context = self.get_context_data(**kwargs)
        context["user"] = current_user
        context["form"] = SuntingProfilForm(instance=current_user)
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)

        current_user = self.request.user
609

610
        form = SuntingProfilForm(request.POST, request.FILES, instance=current_user)
611
612
        if form.is_valid():
            current_user.default_profile_picture = True
613
614
615

            # Removing exifdata from profile picture on upload
            if request.FILES:
616
                EditProfileService.update_profile_picture(current_user, request)
617
            else:
618
                form.save()
619
            return HttpResponseRedirect("/profil/")
620
621
        else:
            context = self.get_context_data(**kwargs)
622
623
            context["form"] = form
            return self.render_to_response(context)
624

625

626
627
628
629
630
631
632
633
634
class ReqMateriView(TemplateView):
    template_name = "req_materi.html"

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            return HttpResponseRedirect("/login/")
        return super(ReqMateriView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
635
        context = super(ReqMateriView, self).get_context_data(**kwargs)
636
637
638
639
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
640
        context['requested_material'] = ReqMaterial.objects.all()
641
642
643
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
644
        title = request.POST.get("title", None)
645
646
647
648
649
        if title is None:
            return JsonResponse({"success": False, "msg": "Missing parameter"})
        ReqMaterial(title=title).save()
        return JsonResponse({"success": True, "msg": "Permintaan materi berhasil dikirimkan"})

650

651
652
653
654
655
656
657
class SuksesLoginKontributorView(TemplateView):
    template_name = "sukses_kontri.html"

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

659
    def get_context_data(self, **kwargs):
660
        context = super(SuksesLoginKontributorView, self).get_context_data(**kwargs)
661
662
663
664
665
666
667
668
669
670
        return context

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

        current_user = self.request.user
        context["user"] = current_user

        return self.render_to_response(context)

671

672
673
674
675
676
677
678
class SuksesLoginAdminView(TemplateView):
    template_name = "sukses_admin.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_admin:
            raise PermissionDenied(request)
        return super(SuksesLoginAdminView, self).dispatch(request, *args, **kwargs)
679

680
681
682
683
684
685
686
687
688
689
690
691
    def get_context_data(self, **kwargs):
        context = super(SuksesLoginAdminView, self).get_context_data(**kwargs)
        return context

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

        current_user = self.request.user
        context["user"] = current_user

        return self.render_to_response(context)

igor lestin sianipar's avatar
igor lestin sianipar committed
692

693
694
695
class PostsView(TemplateView):

    template_name = "user_uploaded_posts.html"
696
697

    def dispatch(self, request, *args, **kwargs):
698
        if not request.user.is_authenticated:
699
            raise PermissionDenied(request)
700
        return super(PostsView, self).dispatch(request, *args, **kwargs)
701
702

    def get(self, request, *args, **kwargs):
703
704
705
706
        context = super().get_context_data(**kwargs)
        user = self.request.user

        posts = Materi.objects.filter(uploader=user).order_by("-date_created")
707
        posts_data = {post.id: {"data": post, "comments": []} for post in posts}
708

709
        comments = Comment.objects.filter(materi__id__in=posts_data.keys()).order_by("-timestamp")
710
711
712
713
714
715
716

        for comment in comments:
            posts_data[comment.materi.id]["comments"].append(comment)

        context["user"] = user
        context["posts"] = posts_data

717
        return self.render_to_response(context=context)
718

719

720
721
722
class RevisiMateriView(TemplateView):
    template_name = "revisi.html"

723
    def dispatch(self, request, *args, **kwargs):
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
        if not request.user.is_contributor:
            raise PermissionDenied(request)
        return super(RevisiMateriView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(RevisiMateriView, self).get_context_data(**kwargs)
        current_materi = get_object_or_404(Materi, pk=kwargs["pk"])
        context["materi"] = current_materi
        context["revisi_form"] = UploadMateriForm(instance=current_materi)
        return context

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

739
    def post(self, request, *args, **kwargs):
740
741
742
743
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)

        current_materi = get_object_or_404(Materi, pk=kwargs["pk"])
744
        form = UploadMateriForm(request.POST, request.FILES, instance=current_materi)
745
        if form.is_valid():
746
            RevisiMateriService.revisi_materi(form, request)
747
748
749
750
751
752
            return HttpResponseRedirect("/dashboard/")
        else:
            context = self.get_context_data(**kwargs)
            context["form_revisi"] = form
            return self.render_to_response(context)

753

754

Samuel Dimas's avatar
Samuel Dimas committed
755
756
757
758
759
760
def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:

761
        load_template = request.path.split("/")[-1]
Samuel Dimas's avatar
Samuel Dimas committed
762
763
764
        template = loader.get_template(load_template)
        return HttpResponse(template.render(context, request))

765
    except Exception as e:
Samuel Dimas's avatar
Samuel Dimas committed
766

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


771
772
773
774
class DownloadHistoryView(TemplateView):
    template_name = "download_history.html"

    def get_context_data(self, **kwargs):
775
        context = super(DownloadHistoryView, self).get_context_data(**kwargs)
776
777
778
779
780
781
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        if request.user.is_authenticated:
            current_user = self.request.user
782
            DownloadHistoryService.init_data_authenticated_user(context, current_user)
783
        else:
784
            DownloadHistoryService.init_data_guest_user(context, request)
785
        return self.render_to_response(context)
786

787

788
789
790
791
792
def save_to_gdrive(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):
793
        GoogleDriveUploadService.upload_to_gdrive(file_path, materi.title)
794
795
796
    else:
        raise Http404("File tidak dapat ditemukan.")

Yaumi's avatar
Yaumi committed
797
798
    return HttpResponseRedirect(reverse('detail-materi', kwargs={'pk': pk}))

799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
class MateriFavorite(TemplateView):

    template_name = "user_favorite_materi.html"

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

    def get(self, request, *args, **kwargs):
        context = super().get_context_data(**kwargs)
        user = self.request.user

        materi = Materi.objects.filter(like=True)
        likes_data = { mat.id: { "data": mat, "comments": [] } for mat in materi }

        comments = Comment.objects \
            .filter(materi__id__in=likes_data.keys()) \
            .order_by("-timestamp")

        for comment in comments:
            likes_data[comment.materi.id]["comments"].append(comment)

        context["user"] = user
        context["likes"] = likes_data

        return self.render_to_response(context=context)

Yaumi's avatar
Yaumi committed
827
class PasswordChangeViews(PasswordChangeView):
828

Yaumi's avatar
Yaumi committed
829
830
    from_class = PasswordChangeForm
    success_url = reverse_lazy('password_success')
Yaumi's avatar
Yaumi committed
831

Yaumi's avatar
Yaumi committed
832
def password_success(request):
833
    return render(request, 'password_success.html', {})
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857


def see_given_rating(request):
    if request.user.is_authenticated:
        order_by_key = request.GET.get('order_by_key', 'timestamp')

        if request.GET.get('order', '').lower() == 'asc':
            order_by = 'asc'
            query_order = ''
        else:
            order_by = 'dsc'
            query_order = '-'

        try:
            if order_by_key[0] == '-':
                order_by_key = order_by_key[1:]
            rating_list = Rating.objects.filter(user=request.user).order_by(query_order + order_by_key)
        except FieldError:
            order_by_key = 'timestamp'
            rating_list = Rating.objects.filter(user=request.user).order_by(query_order + order_by_key)

        return render(request, 'given-rating.html',
                      context={'rating_list': rating_list, 'order_by_key': order_by_key, 'order_by': order_by})
    return permission_denied(request, exception=None)
insan ramadhan's avatar
insan ramadhan committed
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

class SubmitVisitorView(TemplateView):
    template_name = "submit_visitor.html"

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            return HttpResponseRedirect("/login/")
        return super(SubmitVisitorView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(SubmitVisitorView, 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)

    def post(self, request, *args, **kwargs):
        title = request.POST.get("title", None)
        email = request.POST.get("email", None)
        user_id = request.POST.get("user_id", None)
        if title is None:
            return JsonResponse({"success": False, "msg": "Missing parameter"})
        SubmitVisitor(msg=title, user_id=user_id, email=email).save()
        return JsonResponse({"success": True, "msg": "Buku tamu berhasil ditambahkan"})