Fakultas Ilmu Komputer UI

Commit 4caec5a5 authored by Erithiana Sisijoan Koesnadi's avatar Erithiana Sisijoan Koesnadi
Browse files

[56] Fitur Sort by Jumlah Unduh (Download)

parent 32b762dd
...@@ -15,6 +15,7 @@ media ...@@ -15,6 +15,7 @@ media
/static/ /static/
media/ media/
.coverage .coverage
virtualenv
# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ # If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
# in your Git repository. Update and uncomment the following line accordingly. # in your Git repository. Update and uncomment the following line accordingly.
......
...@@ -133,6 +133,9 @@ ...@@ -133,6 +133,9 @@
<li> <li>
<a href="?sort=pengunggah">pengunggah</a> <a href="?sort=pengunggah">pengunggah</a>
</li> </li>
<li>
<a href="?sort=jumlah_unduh">jumlah unduh</a>
</li>
</div> </div>
</div> </div>
</div> </div>
......
import json, tempfile, os, mock import json, tempfile, os, mock
import pandas as pd import pandas as pd
from io import StringIO from io import StringIO
import time
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from datetime import datetime from datetime import datetime
...@@ -136,6 +137,45 @@ class DaftarKatalogTest(TestCase): ...@@ -136,6 +137,45 @@ class DaftarKatalogTest(TestCase):
self.assertSequenceEqual(search_result, expected_search_result) self.assertSequenceEqual(search_result, expected_search_result)
class DaftarKatalogSortingByJumlahUnduhTest(TestCase):
def setUp(self):
self.client = Client()
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": "passwordtest"
}
self.contributor_credential_2 = {
"email": "kontributor2@gov.id",
"password": "passwordtest"
}
self.contributor = get_user_model().objects.create_user(
**self.contributor_credential, name="Kontributor 1", is_contributor=True)
self.contributor2 = get_user_model().objects.create_user(
**self.contributor_credential_2, name="Kontributor 2", is_contributor=True)
self.cover = SimpleUploadedFile(
"Cherprang_Areekul40_nJM9dGt.jpg", b"Test file")
self.content = SimpleUploadedFile("Bahan_PA_RKK.pdf", b"Test file")
Materi(title="Materi 1", author="Agas", uploader=self.contributor,
publisher="Kelas SC", descriptions="Deskripsi Materi 1",
status="APPROVE", cover=self.cover, content=self.content).save()
time.sleep(1)
Materi(title="Materi 2", author="Agas", uploader=self.contributor,
publisher="Kelas SC", descriptions="Deskripsi Materi 2",
status="APPROVE", cover=self.cover, content=self.content).save()
self.last_uploaded_material = Materi.objects.last()
material_unduh_url = f"/materi/{self.last_uploaded_material.id}/unduh"
self.client.get(material_unduh_url)
def test_sorting_by_jumlah_unduh(self):
response = self.client.get("/?sort=jumlah_unduh")
self.assertRegex(str(response.content), rf'.*Materi 2.*Materi 1.*')
class DaftarKatalogPerKontributorTest(TestCase): class DaftarKatalogPerKontributorTest(TestCase):
def setUp(self): def setUp(self):
......
...@@ -78,18 +78,20 @@ class DaftarKatalog(TemplateView): ...@@ -78,18 +78,20 @@ class DaftarKatalog(TemplateView):
getSort = request.GET.get("sort") getSort = request.GET.get("sort")
if getSort: if getSort:
url = url + "&sort={0}".format(getSort) url = url + "&sort={0}".format(getSort)
if getSort == "judul": if(getSort == "judul"):
lstMateri = lstMateri.order_by("title") lstMateri = lstMateri.order_by('title')
elif getSort == "penulis": elif(getSort == "penulis"):
lstMateri = lstMateri.order_by("author") lstMateri = lstMateri.order_by('author')
elif getSort == "pengunggah": elif(getSort == "pengunggah"):
lstMateri = lstMateri.order_by("uploader") lstMateri = lstMateri.order_by('uploader')
elif getSort == "terbaru": elif(getSort == "terbaru"):
lstMateri = lstMateri.order_by("-date_created") lstMateri = lstMateri.order_by('-date_created')
elif getSort == "terlama": elif(getSort == "terlama"):
lstMateri = lstMateri.order_by("date_created") lstMateri = lstMateri.order_by('date_created')
elif getSort == "terpopuler": elif(getSort == "terpopuler"):
lstMateri = lstMateri.annotate(count=Count("like__id")).order_by("-count") lstMateri = lstMateri.annotate(count=Count('like__id')).order_by('-count')
elif(getSort == "jumlah_unduh"):
lstMateri = lstMateri.annotate(count=Count('unduh__id')).order_by('-count')
context["materi_list"] = lstMateri context["materi_list"] = lstMateri
paginator = Paginator(context["materi_list"], 15) paginator = Paginator(context["materi_list"], 15)
......
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