diff --git a/app/services.py b/app/services.py index 05be1da1f347bb687288ff36a6b9f4f78dfd474e..538d8f9bf41b7d648a35e7a48b00b2ff202adca3 100644 --- a/app/services.py +++ b/app/services.py @@ -60,6 +60,8 @@ class DafterKatalogService: lst_materi = lst_materi.annotate(count=Count('like__id')).order_by('-count') elif (get_sort == "jumlah_unduh"): 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') return lst_materi, url @staticmethod diff --git a/app/templates/app/katalog_materi.html b/app/templates/app/katalog_materi.html index bd49b2d93fc8ba0d0416e2088640618e17fc414e..fa2665103a32ea28fe8fda9be4d708815a4455b1 100644 --- a/app/templates/app/katalog_materi.html +++ b/app/templates/app/katalog_materi.html @@ -141,6 +141,9 @@ <li> <a href="?sort=jumlah_unduh">jumlah unduh</a> </li> + <li> + <a href="?sort=jumlah_tampilan">jumlah tampilan</a> + </li> </ul> </div> </div> diff --git a/app/tests.py b/app/tests.py index f4e366e8e32663d2b7dc363d46de851d70d81333..5c645df2dd7c5a3044be1e669a0d88de97da53b9 100644 --- a/app/tests.py +++ b/app/tests.py @@ -186,6 +186,40 @@ class DaftarKatalogSortingByJumlahUnduhTest(TestCase): response = self.client.get("/?sort=jumlah_unduh") self.assertRegex(str(response.content), rf'.*Materi 2.*Materi 1.*') +class DaftarKatalogSortingByJumlahTampilanTest(TestCase): + def setUp(self): + self.contributor_credential = { + "email": "kontributor@gov.id", + "password": id_generator() + } + self.contributor = get_user_model().objects.create_user( + **self.contributor_credential, name="Kontributor", is_contributor=True + ) + self.client = Client() + content = b"Test file" + self.cover = SimpleUploadedFile( + "cover.jpg", + content + ) + self.content = SimpleUploadedFile( + "content.txt", + content + ) + Materi(title="Materi 1", author="Agas", uploader=self.contributor, + publisher="Kelas SC", descriptions="Deskripsi Materi 1", + status="PENDING", cover=self.cover, content=self.content).save() + + Materi(title="Materi 2", author="Agas", uploader=self.contributor, + publisher="Kelas SC", descriptions="Deskripsi Materi 1", + status="PENDING", cover=self.cover, content=self.content).save() + self.materi2 = Materi.objects.last() + self.url = f"/materi/{self.materi2.id}/view" + + def sorting_by_jumlah_tampilan(self): + self.client.get(self.url) + response = self.client.get("/?sort=jumlah_tampilan") + self.assertRegex(str(response.content), rf'.*Materi 2.*Materi 1.*') + class DaftarKatalogPerKontributorTest(TestCase): def setUp(self): self.client = Client()