diff --git a/app/services.py b/app/services.py index 7ea312c8994282b062e98e49280da85d4758354d..87760645a964019755947da189c7ea97f0afe727 100644 --- a/app/services.py +++ b/app/services.py @@ -67,6 +67,8 @@ class DafterKatalogService: lst_materi = lst_materi.annotate(count=Count('unduh__id')).order_by('-count') elif (get_sort == "jumlah_tampilan"): lst_materi = lst_materi.annotate(count=Count('baca__id')).order_by('-count') + elif (get_sort == "jumlah_komentar"): + lst_materi = lst_materi.annotate(count=Count('comment__id')).order_by('-count') return lst_materi, url @staticmethod diff --git a/app/templates/app/katalog_materi.html b/app/templates/app/katalog_materi.html index 4a06d4c5a4233e6766f70a3617cf61c9755260e2..62736dfe989318cc88f0b122fb4ca01e737cc96a 100644 --- a/app/templates/app/katalog_materi.html +++ b/app/templates/app/katalog_materi.html @@ -149,6 +149,9 @@ <li> <a href="?sort=jumlah_tampilan">jumlah tampilan</a> </li> + <li> + <a href="?sort=jumlah_komentar">jumlah komentar</a> + </li> </ul> </div> </div> diff --git a/app/tests.py b/app/tests.py index 02f8e1fb666e89e5a1319b20398e06da1682957e..a487c2b693a76393a635a91271a64ed492855618 100644 --- a/app/tests.py +++ b/app/tests.py @@ -206,7 +206,48 @@ class DaftarKatalogSortingByJumlahTampilanTest(TestCase): self.client.get(self.url) response = self.client.get("/?sort=jumlah_tampilan") self.assertRegex(str(response.content), rf'.*Materi 2.*Materi 1.*') - + +class DaftarKatalogSortingByJumlahKomentarTest(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() + + url = f"/materi/{self.last_uploaded_material.id}/" + self.client.login(**self.contributor_credential_2) + self.client.post(url, {"comment": "This is new comment by Kontributor 2"}) + + def test_sorting_by_jumlah_komentar(self): + response = self.client.get("/?sort=jumlah_komentar") + self.assertRegex(str(response.content), rf'.*Materi 2.*Materi 1.*') + class DaftarKatalogPerKontributorTest(TestCase): def setUp(self): self.client = Client()