Fakultas Ilmu Komputer UI

Commit 8806fcdf authored by Nandhika Prayoga's avatar Nandhika Prayoga
Browse files

[GREEN] Implement recommendation functionality and the view

parent 816c56d7
Pipeline #59990 passed with stages
in 27 minutes and 59 seconds
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">
......
Markdown is supported
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