Fakultas Ilmu Komputer UI

Commit 4d5c3ba4 authored by Michael Wiryadinata Halim's avatar Michael Wiryadinata Halim
Browse files

Profile: Rating for Contributor (Rating Count/Average)

parent 4baf6312
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% load humanize %}
{% block title %}Digipus - {% endblock %} {% block title %}Digipus - {% endblock %}
{% block header %} {% block header %}
...@@ -86,6 +87,8 @@ ...@@ -86,6 +87,8 @@
</table> </table>
</div> </div>
</div> </div>
<h4 id="rating">Rating: {% if avg_rating.avg_rating %}{{ avg_rating.avg_rating|floatformat:"2"|intcomma }}{% else %}0{% endif %}</h4>
<h6>oleh: {{ count_rating }} orang</h6>
<div class="row"><form method="post">{{ form_rating }} {% csrf_token %} <div class="row"><form method="post">{{ form_rating }} {% csrf_token %}
<button type="submit" class="form-control" style="margin-top: 1rem">Submit</button></form> </div> <button type="submit" class="form-control" style="margin-top: 1rem">Submit</button></form> </div>
</div> </div>
......
...@@ -69,6 +69,7 @@ from selenium.webdriver.chrome.options import Options ...@@ -69,6 +69,7 @@ from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoSuchElementException
from statistics import mean
...@@ -1970,6 +1971,69 @@ class RatingContributorTest(TransactionTestCase): ...@@ -1970,6 +1971,69 @@ class RatingContributorTest(TransactionTestCase):
self.client.post(url, data={"user": self.contributor.id, "score": 0}) self.client.post(url, data={"user": self.contributor.id, "score": 0})
self.assertEqual(0, RatingContributor.objects.filter(user=self.contributor.id).count()) self.assertEqual(0, RatingContributor.objects.filter(user=self.contributor.id).count())
def test_average_rating_score_empty(self):
url = f"/profil/{self.contributor.email}/"
response = self.client.get(url)
self.assertTemplateUsed(response=response, template_name="app/katalog_kontri.html")
self.assertContains(response=response, text="Rating: 0", count=1)
self.assertContains(response=response, text="oleh: 0 orang", count=1)
def test_average_rating_correct(self):
url = f"/profil/{self.contributor.email}/"
self.client.post(url, data={"user": self.contributor.id, "score": 5})
response = self.client.get(url)
self.assertContains(response=response, text="Rating: 5", count=1)
self.assertContains(response=response, text="oleh: 1 orang", count=1)
def test_average_rating_form_incorrect_correct(self):
url = f"/profil/{self.contributor.email}/"
self.client.post(url, data={"user": self.contributor.id, "score": 6})
response = self.client.get(url)
self.assertContains(response=response, text="Rating: 0", count=1)
self.assertContains(response=response, text="oleh: 0 orang", count=1)
self.client.post(url, data={"user": self.contributor.id, "score": -1})
response = self.client.get(url)
self.assertContains(response=response, text="Rating: 0", count=1)
self.assertContains(response=response, text="oleh: 0 orang", count=1)
def test_average_with_multiple_score_correct(self):
score = 5
avg = [score]
url = f"/profil/{self.contributor.email}/"
self.client.post(url, data={"user": self.contributor.id, "score": score})
response = self.client.get(url)
self.assertContains(response=response, text=f"Rating: {mean(avg)}", count=1)
self.assertContains(response=response, text=f"oleh: {len(avg)} orang", count=1)
score = 4
avg.append(score)
self.client.post(url, data={"user": self.contributor.id, "score": score})
response = self.client.get(url)
self.assertContains(response=response, text=f"Rating: {mean(avg)}", count=1)
self.assertContains(response=response, text=f"oleh: {len(avg)} orang", count=1)
score = 3
avg.append(score)
self.client.post(url, data={"user": self.contributor.id, "score": score})
response = self.client.get(url)
self.assertContains(response=response, text=f"Rating: {mean(avg)}", count=1)
self.assertContains(response=response, text=f"oleh: {len(avg)} orang", count=1)
score = 2
avg.append(score)
self.client.post(url, data={"user": self.contributor.id, "score": score})
response = self.client.get(url)
self.assertContains(response=response, text=f"Rating: {mean(avg)}", count=1)
self.assertContains(response=response, text=f"oleh: {len(avg)} orang", count=1)
score = 1
avg.append(score)
self.client.post(url, data={"user": self.contributor.id, "score": score})
response = self.client.get(url)
self.assertContains(response=response, text=f"Rating: {mean(avg)}", count=1)
self.assertContains(response=response, text=f"oleh: {len(avg)} orang", count=1)
class UserDownloadHistoryTest(TestCase): class UserDownloadHistoryTest(TestCase):
def setUp(self): def setUp(self):
self.user1_credential = { self.user1_credential = {
......
...@@ -8,7 +8,7 @@ from django.conf import settings ...@@ -8,7 +8,7 @@ from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import Q from django.db.models import Q, Avg
from django.http import (Http404, HttpResponse, HttpResponseRedirect, from django.http import (Http404, HttpResponse, HttpResponseRedirect,
JsonResponse) JsonResponse)
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
...@@ -23,7 +23,7 @@ from app.models import ( ...@@ -23,7 +23,7 @@ from app.models import (
Comment, Comment,
Materi, Materi,
ReqMaterial, ReqMaterial,
Rating, Rating, RatingContributor,
) )
from authentication.models import User from authentication.models import User
from .services import DafterKatalogService, DetailMateriService, LikeDislikeService, MateriFieldValidationHelperService, \ from .services import DafterKatalogService, DetailMateriService, LikeDislikeService, MateriFieldValidationHelperService, \
...@@ -88,6 +88,9 @@ class KatalogPerKontributorView(TemplateView): ...@@ -88,6 +88,9 @@ class KatalogPerKontributorView(TemplateView):
context["materi_list"] = materi_list_by_page context["materi_list"] = materi_list_by_page
contributor = get_object_or_404(User, email=kwargs["email"]) contributor = get_object_or_404(User, email=kwargs["email"])
context["form_rating"] = RatingContributorForm(initial={"user": contributor}) context["form_rating"] = RatingContributorForm(initial={"user": contributor})
context["avg_rating"] = User.objects.filter(email=kwargs["email"]) \
.annotate(avg_rating=Avg("ratingcontributor__score"))[0]
context["count_rating"] = RatingContributor.objects.filter(user=contributor).count()
return self.render_to_response(context=context) return self.render_to_response(context=context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
......
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