From db19e628d783b10e19491aa1775fdcf1c5f3fcfa Mon Sep 17 00:00:00 2001
From: Erithiana Sisijoan Koesnadi <erithiana.sisijoan@ui.ac.id>
Date: Thu, 29 Oct 2020 13:16:50 +0700
Subject: [PATCH] [53] Fitur Sorting Materi By Jumlah Tampilan

---
 app/services.py                       |  2 ++
 app/templates/app/katalog_materi.html |  3 +++
 app/tests.py                          | 34 +++++++++++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/app/services.py b/app/services.py
index 05be1da..538d8f9 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 bd49b2d..fa26651 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 f4e366e..5c645df 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()
-- 
GitLab