diff --git a/.gitignore b/.gitignore index c0739ce4ef0b8293ec8d34f428377bf9fcf5af8d..7e6811adf6066c047eb2fca79f1f49238d300bc2 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ media /static/ media/ .coverage +virtualenv # 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. diff --git a/app/templates/app/katalog_materi.html b/app/templates/app/katalog_materi.html index b788a696bc8c1747f2a2bb1c0921a222bb8e1536..7881668911c22bb8ecf74a86b4ec1cde05790204 100644 --- a/app/templates/app/katalog_materi.html +++ b/app/templates/app/katalog_materi.html @@ -133,6 +133,9 @@ <li> <a href="?sort=pengunggah">pengunggah</a> </li> + <li> + <a href="?sort=jumlah_unduh">jumlah unduh</a> + </li> </div> </div> </div> diff --git a/app/tests.py b/app/tests.py index 1dcd63c3cecdfa0c581fce89553d39d7713dca1c..abbc2e4171efae4217485787ec0304c9b2d5689e 100644 --- a/app/tests.py +++ b/app/tests.py @@ -1,6 +1,7 @@ import json, tempfile, os, mock import pandas as pd from io import StringIO +import time from bs4 import BeautifulSoup from datetime import datetime @@ -136,6 +137,45 @@ class DaftarKatalogTest(TestCase): 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): def setUp(self): diff --git a/app/views.py b/app/views.py index 4f582eab8d7781c15239b589e6d2c350d9fc72e5..bc0b9f0f74d23bdfb3e4cfd994a52138a020bac5 100644 --- a/app/views.py +++ b/app/views.py @@ -78,18 +78,20 @@ class DaftarKatalog(TemplateView): getSort = request.GET.get("sort") if getSort: url = url + "&sort={0}".format(getSort) - if getSort == "judul": - lstMateri = lstMateri.order_by("title") - elif getSort == "penulis": - lstMateri = lstMateri.order_by("author") - elif getSort == "pengunggah": - lstMateri = lstMateri.order_by("uploader") - elif getSort == "terbaru": - lstMateri = lstMateri.order_by("-date_created") - elif getSort == "terlama": - lstMateri = lstMateri.order_by("date_created") - elif getSort == "terpopuler": - lstMateri = lstMateri.annotate(count=Count("like__id")).order_by("-count") + if(getSort == "judul"): + lstMateri = lstMateri.order_by('title') + elif(getSort == "penulis"): + lstMateri = lstMateri.order_by('author') + elif(getSort == "pengunggah"): + lstMateri = lstMateri.order_by('uploader') + elif(getSort == "terbaru"): + lstMateri = lstMateri.order_by('-date_created') + elif(getSort == "terlama"): + lstMateri = lstMateri.order_by('date_created') + elif(getSort == "terpopuler"): + 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 paginator = Paginator(context["materi_list"], 15)