Fakultas Ilmu Komputer UI

Commit 5ce74ac2 authored by Nandhika Prayoga's avatar Nandhika Prayoga
Browse files

[#47] Material: Recommendation

parent 1993e13e
import datetime
import os
import random
from functools import cmp_to_key
from django.contrib import messages
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ValidationError
from django.db.models import Count, Q
from django.db.models import Case, When, Count, Q
from django.shortcuts import get_object_or_404
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
......@@ -67,6 +68,8 @@ class DafterKatalogService:
@staticmethod
def apply_options(lst_materi, request, url):
should_random = bool(request.GET.get("random"))
should_get_recommendation = bool(request.GET.get("recommendation"))
get_search = request.GET.get("search")
if get_search:
lst_materi, url = DafterKatalogService.search_materi(get_search, lst_materi, url)
......@@ -76,10 +79,24 @@ class DafterKatalogService:
get_sort = request.GET.get("sort")
if get_sort:
lst_materi, url = DafterKatalogService.apply_sort(get_sort, lst_materi, url)
if should_random:
if should_get_recommendation:
lst_materi = DafterKatalogService.get_recommendation(lst_materi)
elif should_random:
lst_materi = DafterKatalogService.apply_random(lst_materi)
return lst_materi, url
@staticmethod
def get_recommendation(lst_materi):
sorted_list = sorted(
lst_materi,
key=cmp_to_key(lambda a, b: 1 if (a.like_count, a.date_modified) < (b.like_count, b.date_modified) else -1),
)
ids = [materi.id for materi in sorted_list]
preserved = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(ids)])
return Materi.objects.filter(id__in=ids).order_by(preserved)
@staticmethod
def apply_random(lst_materi):
return random.sample(list(lst_materi), len(lst_materi))
......
......@@ -150,4 +150,57 @@ body{
color: #ffffff;
background-color: #615CFD;
}
\ No newline at end of file
#container-materi-ordering {
display: flex;
flex-flow: row wrap;
flex-basis: auto;
}
#container-materi-ordering > * {
margin: 0 8px ;
}
#container-materi-ordering > *:first-child {
margin-left: 0px;
}
#container-materi-ordering > *:last-child {
margin-right: 0px;
}
.btn-materi-ordering {
border-radius: 2px;
font-size: 14px;
padding: 8px 16px;
height: 38px;
border-radius: 2px;
color: #ffffff;
background-color: #615CFD;
border: none;
}
.btn-materi-ordering:hover {
color: #615CFD;
background-color: #ffffff;
}
@media only screen and (max-width: 768px) {
#container-materi-ordering {
flex-direction: column;
}
#container-materi-ordering > * {
margin-left: 0 !important;
margin-right: 0 !important;
margin: 8px 0;
}
#container-materi-ordering > *:first-child {
margin-top: 0px;
}
#container-materi-ordering > *:last-child {
margin-bottom: 0px;
}
}
\ No newline at end of file
......@@ -76,10 +76,13 @@
</div>
</div>
</header>
<a href="/download-history/" class="btn-history">Riwayat Unduh</a><br><br>
<a href="?random=1" class="btn-history">Acak Materi</a><br><br>
<a href="/download-history/" class="btn-history">Riwayat Unduh</a><br><br>
<div id="container-materi-ordering">
<a href="?random=1" class="btn-materi-ordering">Acak Materi</a><br><br>
<a href="?recommendation=1" class="btn-materi-ordering">Rekomendasi</a><br><br>
</div>
<div class="container">
<div class="row content">
......
......@@ -3114,5 +3114,102 @@ class PasswordValidatorPolicyTest(TestCase):
def test_using_password_using_correct_policy(self):
self.assertEquals(self.validator.validate(self.password_enforcing_policy), None)
class MateriRecommendationTest(TestCase):
def setUp(self):
self.contributor = User.objects.create(
email="kontributor@gov.id", password="passwordtest", name="kontributor", is_contributor=True
)
self.cover = SimpleUploadedFile("ExampleCover221.jpg", b"Test file")
self.content = SimpleUploadedFile("ExampleFile221.pdf", b"Test file")
def test_able_to_get_recommendation_by_like_counts(self):
materi1 = Materi.objects.create(
title="Materi 1",
author="Nandhika",
uploader=self.contributor,
publisher="Publisher",
descriptions="Deskripsi Materi 1",
status="APPROVE",
cover=self.cover,
content=self.content,
date_modified=datetime.now(),
date_created=datetime.now(),
)
Like.objects.create(materi=materi1)
Like.objects.create(materi=materi1)
materi2 = Materi.objects.create(
title="Materi 2",
author="Prayoga",
uploader=self.contributor,
publisher="Publisher",
descriptions="Deskripsi Materi 2",
status="APPROVE",
cover=self.cover,
content=self.content,
date_modified=datetime.now(),
date_created=datetime.now(),
)
Like.objects.create(materi=materi2)
Like.objects.create(materi=materi2)
Like.objects.create(materi=materi2)
materi3 = Materi.objects.create(
title="Materi 3",
author="Nandhika Prayoga",
uploader=self.contributor,
publisher="Publisher",
descriptions="Deskripsi Materi 3",
status="APPROVE",
cover=self.cover,
content=self.content,
date_modified=datetime.now(),
date_created=datetime.now(),
)
response = Client().get("/?recommendation=1")
list = [int(id) for id in re.findall(r"Materi\s(\d+)[^\d]", response.content.decode())]
self.assertEqual(list, [2, 1, 3])
def test_set_date_as_tiebreak_if_like_counts_is_same(self):
materi2 = Materi.objects.create(
title="Materi 2",
author="Prayoga",
uploader=self.contributor,
publisher="Publisher",
descriptions="Deskripsi Materi 2",
status="APPROVE",
cover=self.cover,
content=self.content,
date_modified=datetime.now(),
date_created=datetime.now(),
)
Like.objects.create(materi=materi2)
Like.objects.create(materi=materi2)
materi1 = Materi.objects.create(
title="Materi 1",
author="Nandhika",
uploader=self.contributor,
publisher="Publisher",
descriptions="Deskripsi Materi 1",
status="APPROVE",
cover=self.cover,
content=self.content,
date_modified=datetime.now(),
date_created=datetime.now(),
)
Like.objects.create(materi=materi1)
Like.objects.create(materi=materi1)
response = Client().get("/?recommendation=1")
list = [int(id) for id in re.findall(r"Materi\s(\d+)[^\d]", response.content.decode())]
self.assertEqual(list, [1, 2])
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment