Fakultas Ilmu Komputer UI

Commit c29c2c11 authored by MUHAMMAD EZRA RIZKIATAMA PUTRA's avatar MUHAMMAD EZRA RIZKIATAMA PUTRA
Browse files

[#86] Material Statistic: Like Count (User View)

parent 59a5a847
......@@ -33,7 +33,6 @@ div.review {
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
{% if request.user.is_contributor %}
{% if materi_data.status == "DISAPPROVE" %}
<li class="nav-item black-text">
......@@ -164,6 +163,14 @@ div.review {
<p class="info-content">{{materi_data.content.size|filesizeformat}}</p>
</dd>
</div>
<div class="info" id="1">
<dl class="col col-4">
<dt class="info-name">Jumlah Like</dt>
</dl>
<dd>
<p class="info-content">{{materi_data.like_count}}</p>
</dd>
</div>
<div class="info" id="1">
<dl class="col col-4">
<dt class="info-name">Jumlah Download</dt>
......@@ -225,11 +232,11 @@ div.review {
<input type="hidden" name="action" value="like">
</form>
{% if has_liked %}
<button id="thumb" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded"><i id="thumbIcon"
aria-hidden="true" class="fas fa-thumbs-up"></i> Disukai</button>
<button id="thumb" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded" onClick="postLike()"><i id="thumbIcon"
aria-hidden="true" class="fas fa-thumbs-up"></i><span> Disukai</span></button>
{% else %}
<button id="thumb" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded"><i id="thumbIcon"
aria-hidden="true" class="far fa-thumbs-up"></i> Sukai</button>
<button id="thumb" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded" onClick="postLike()"><i id="thumbIcon"
aria-hidden="true" class="far fa-thumbs-up"></i><span> Sukai</span></button>
{% endif %}
{% if user.is_authenticated %}
......@@ -331,7 +338,7 @@ div.review {
<div>
<button id="thumb-like-comment-{{ comment.id }}" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded" onClick="postLikeComment({{ comment.id }})">
<div class="d-flex flex-row">
{% if has_liked.comment.id %}
{% if has_liked_comment.comment.id %}
<i id="thumb-like-comment-icon-{{ comment.id }}" aria-hidden="true" class="fas fa-thumbs-up"></i>
{% else %}
<i id="thumb-like-comment-icon-{{ comment.id }}" aria-hidden="true" class="far fa-thumbs-up"></i>
......@@ -341,7 +348,7 @@ div.review {
</button>
<button id="thumb-dislike-comment-{{ comment.id }}" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded" onClick="postDislikeComment({{ comment.id }})">
<div class="d-flex flex-row">
{% if has_disliked.comment.id %}
{% if has_disliked_comment.comment.id %}
<i id="thumb-dislike-comment-icon-{{ comment.id }}" aria-hidden="true" class="fas fa-thumbs-down"></i>
{% else %}
<i id="thumb-dislike-comment-icon-{{ comment.id }}" aria-hidden="true" class="far fa-thumbs-down"></i>
......@@ -509,8 +516,7 @@ div.review {
}
</script>
<script>
$('#thumb').click(function () {
function postLike() {
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
......@@ -528,7 +534,7 @@ div.review {
success: LikePost,
dataType: 'html'
});
});
}
function postLikeComment(comment_id) {
$.ajaxSetup({
......@@ -599,12 +605,15 @@ div.review {
function LikePost(data, jqXHR) {
var data = $.parseJSON(data)
var likeCount = parseInt($('.info-content')[6].textContent)
if (data['liked']) {
$('#thumbIcon').removeClass("fas fa-thumbs-up").addClass('far fa-thumbs-up')
document.getElementById("thumb").firstChild.data = " Sukai"
$('#thumb>span').text(" Sukai")
$('.info-content')[6].textContent = likeCount - 1
} else {
$('#thumbIcon').removeClass("far fa-thumbs-up").addClass('fas fa-thumbs-up')
document.getElementById("thumb").firstChild.data = " Disukai"
$('#thumb>span').text(" Disukai")
$('.info-content')[6].textContent = likeCount + 1
}
}
......
......@@ -334,7 +334,9 @@ class DetailMateriTest(TestCase):
self.download_url1 = self.url + "unduh"
self.url2 = "/materi/" + str(self.materi2.id) + "/"
self.download_url2 = self.url2 + "unduh"
self.like_url = '/materi/like/'
self.dcount_info_name = "Jumlah Download"
self.lcount_info_name = "Jumlah Like"
self.materi_with_published_date = Materi.objects.create(title="Materi 1", author="Agas", uploader=self.contributor,
publisher="Kelas SC", descriptions="Deskripsi Materi 1",
......@@ -899,6 +901,56 @@ class DetailMateriTest(TestCase):
self.check_materi_info_not_in_html(self.dcount_info_name, dcount_materi2, html)
self.check_materi_info_in_html(self.dcount_info_name, dcount_materi2, html2)
self.check_materi_info_not_in_html(self.dcount_info_name, dcount_materi1, html2)
def test_like_count_displayed_on_template_when_no_like(self):
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.lcount_info_name, 0, html)
def test_like_count_displayed_on_template_when_single_like(self):
payload = {
'materi_id': self.materi1.id,
'session_id': "dummysession"
}
self.client.post(self.like_url, payload)
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.lcount_info_name, 1, html)
def test_like_count_displayed_on_template_when_multiple_like(self):
payload1 = {
'materi_id': self.materi1.id,
'session_id': "dummysession1"
}
payload2 = {
'materi_id': self.materi1.id,
'session_id': "dummysession2"
}
self.client.post(self.like_url, payload1)
self.client.post(self.like_url, payload2)
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.lcount_info_name, 2, html)
def test_like_count_displayed_on_template_when_unlike(self):
payload = {
'materi_id': self.materi1.id,
'session_id': "dummysession"
}
# Like materi once
self.client.post(self.like_url, payload)
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.lcount_info_name, 1, html)
# Like it again, resulting in unlike
self.client.post(self.like_url, payload)
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.lcount_info_name, 0, html)
class PostsViewTest(TestCase):
......
......@@ -134,8 +134,8 @@ class DetailMateri(TemplateView):
has_disliked, has_liked = DetailMateriService.find_comment_like_dislike(query_set_for_comment, self.request.session)
context["comment_data"] = query_set_for_comment
context["review_data"] = query_set_for_review
context["has_liked"] = has_liked
context["has_disliked"] = has_disliked
context["has_liked_comment"] = has_liked
context["has_disliked_comment"] = has_disliked
return self.render_to_response(context=context)
......
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