Fakultas Ilmu Komputer UI

Commit 65fa64d8 authored by Saul Andre's avatar Saul Andre
Browse files

Merge branch 'PBI-Daftar_Komentar' into 'staging'

Creating comments page for contributor

See merge request ppl-fasilkom-ui/2020/ppl-c/diskominfo-depok-digipus/marjinal-digipus!54
parents 2e389049 0874eafa
.container-fluid {
}
.user .profile-pic {
display: inline-block;
height: 50px;
width: 50px;
border-radius: 50%;
margin-right: 10px;
}
.comment {
border-bottom: 1px solid #d4d4d4;
}
.comment .user .profile {
display: inline-block;
height: 25px;
width: 25px;
background-color: #bbb;
border-radius: 50%;
margin-right: 10px;
}
.comment .text {
padding-left: 20px;
}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="description" content="" />
<meta name="author" content="" />
<title>Comments</title>
<!-- Custom fonts for this template -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
rel="stylesheet"
/>
<!-- Custom styles for this template -->
<link href="{% static 'css/sb-admin-2.min.css' %}" rel="stylesheet" />
<link rel="stylesheet" href="{% static 'css/button.css' %}" />
<link
rel="icon"
type="image/png"
href="{% static 'images/icons/logo.ico' %}"
/>
<!-- Custom styles for this page -->
<link
rel="stylesheet"
type="text/css"
href="{% static 'app/css/comments.css' %}"
/>
</head>
<body id="page-top" style="font-family: 'Poppins', sans-serif;">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul
class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion"
id="accordionSidebar"
>
<!-- Sidebar - Brand -->
<a
class="sidebar-brand d-flex align-items-center justify-content-center"
href="{% url 'daftar_katalog' %}"
>
<div class="sidebar-brand-icon rotate-n-15"></div>
<div class="sidebar-brand-text mx-3">Digipus</div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0" />
<!-- Nav Item - Dashboard -->
<li class="nav-item">
<a class="nav-link" href="{% url 'unggah' %}">
<span>Unggah Materi</span></a
>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'dashboard' %}">
<span>Riwayat Unggah</span></a
>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span>Statisik Materi</span></a
>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'comments' user.id %}">
<span>Komentar</span></a
>
</li>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav
class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow"
>
<!-- Sidebar Toggle (Topbar) -->
<button
id="sidebarToggleTop"
class="btn btn-link d-md-none rounded-circle mr-3"
>
<i class="fa fa-bars"></i>
</button>
<div class="sidebar-brand-text mx-3">
Diskominfo Kota Depok
</div>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="/dashboard/">
<span
class="mr-2 d-none d-lg-inline text-gray-600 small"
>Dasbor</span
>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/profil/">
<span
class="mr-2 d-none d-lg-inline text-gray-600 small"
>Profil</span
>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/dashboard/">
<span
class="mr-2 d-none d-lg-inline text-gray-600 small"
>Logout</span
>
</a>
</li>
<div class="topbar-divider d-none d-sm-block"></div>
<!-- Nav Item - User Information -->
<li class="nav-item dropdown no-arrow">
<a
class="nav-link dropdown-toggle"
href="#"
id="userDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
<span
class="mr-2 d-none d-lg-inline text-gray-600 small"
>{{ user.name }}</span
>
<img
class="img-profile rounded-circle"
src="{{ user.profile_picture.url}}"
/>
</a>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<!-- Begin Page Content -->
<div class="container-fluid">
<div
class="row user shadow p-3 mb-3 bg-white rounded bd-highlight"
>
<div
class="user-profile d-flex flex-row align-items-center flex-grow-1 bd-highligh"
>
<img
class="profile-pic"
src="{{ user.profile_picture.url }}"
alt="profile-picture"
/>
<p class="user-name fs-10 h5 pl-2 m-0">
{{user.name}}
</p>
</div>
<div
class="numb-of-materi pl-2 pr-2 flex-column align-items-center bd-highligh"
>
<p class="m-0 text-center">
{{numb_of_materi}}
</p>
<p class="m-0">Materi</p>
</div>
<div
class="numb-of-comments pl-2 pr-2 flex-column align-items-center bd-highligh"
>
<p class="m-0 text-center">
{{numb_of_comments}}
</p>
<p class="m-0">Komentar</p>
</div>
</div>
<div
class="row comments shadow p-3 mb-3 bg-white rounded d-flex flex-column bd-highlight"
>
<p class="h4 mb-3 bd-highlight">Komentar</p>
<div class="comments">
{% for comment in comments %}
<div
class="comment shadow-sm p-3 mb-1 bg-white rounded bd-highlight"
>
<div
class="d-flex bd-highlight mb-3 align-items-center user"
>
{% if comment.user != Null %}
<img
class="profile"
src="{{ comment.user.profile_picture.url }}"
alt="profile-picture"
/>
{% else %}
<span
style="background-color: #{{comment.profile}}"
class="profile p-1 bd-highligh"
></span>
{% endif %}
<p class="p-1 bd-highligh m-0">
<b>{{comment.user.name}}</b>
</p>
{% if user.is_admin %}
<a
class="ml-auto p-1 bd-highlight close"
href="{% url 'delete-comment' materi_data.id comment.id %}"
>
<span aria-hidden="true"
>&times;</span
>
</a>
{% endif %}
</div>
<p class="text">{{comment.comment}}</p>
</div>
{% endfor %}
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<!-- Footer -->
<footer class="sticky-footer bg-white">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span
>Copyright &copy; Diskominfo Kota Depok
2020</span
>
</div>
</div>
</footer>
<!-- End of Footer -->
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
<!-- Bootstrap core JavaScript-->
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"
></script>
<!-- Core plugin JavaScript-->
<script
src="https://code.jquery.com/jquery-3.5.0.min.js"
integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ="
crossorigin="anonymous"
></script>
<!-- Custom scripts for all pages-->
<script src="{% static 'js/sb-admin-2.min.js' %}"></script>
<!-- Page level plugins -->
<script src="{% static 'vendor/datatables/jquery.dataTables.min.js' %}"></script>
<script src="{% static 'vendor/datatables/dataTables.bootstrap4.min.js' %}"></script>
<!-- Page level custom scripts -->
<script src="{% static 'js/demo/datatables-demo.js' %}"></script>
</body>
</html>
......@@ -58,6 +58,11 @@
<a class="nav-link" href="#">
<span>Statisik Materi</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'comments' user.id %}">
<span>Komentar</span></a>
</li>
</ul>
<!-- End of Sidebar -->
......@@ -223,4 +228,4 @@
</body>
</html>
\ No newline at end of file
</html>
......@@ -62,6 +62,10 @@
<span>Statisik Materi</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'comments' user.id %}">
<span>Komentar</span></a>
</li>
</ul>
<!-- End of Sidebar -->
......
......@@ -15,7 +15,7 @@ from .models import Category, Comment, Materi, Like, ViewStatistics, DownloadSta
from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri,
ProfilKontributorView, SuksesLoginAdminView,
SuksesLoginKontributorView, SuntingProfilView,
ProfilAdminView, SuntingProfilAdminView)
ProfilAdminView, CommentsView, SuntingProfilAdminView)
from app.views import UploadMateriHTML, UploadMateriView
from authentication.models import User
......@@ -159,8 +159,71 @@ class DetailMateriTest(TestCase):
self.client.get(deleteURL)
self.assertEqual(Comment.objects.all().filter(
comment="This is new comment by Anonymous").count(), 0)
class ViewCommentsTest(TestCase):
def setUp(self):
self.client = Client()
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": "passwordtest"
}
self.matt_damon_credential = {
"email": "mattdamon@gov.id",
"password": "passwordtest"
}
self.contributor = get_user_model().objects.create_user(
**self.contributor_credential, name="Kontributor", is_contributor=True)
self.mattDamon = get_user_model().objects.create_user(
**self.matt_damon_credential, name="Matt Damon", 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()
Materi(title="Materi 2", author="Matt", uploader=self.mattDamon,
publisher="Kelas SC", descriptions="Deskripsi Materi 2",
status="APPROVE", cover=self.cover, content=self.content).save()
self.materi1 = Materi.objects.first()
self.materi2 = Materi.objects.get(uploader=self.mattDamon)
self.commentByKontributor = Comment.objects.create(username='saul', comment="this is contributor comment", materi=self.materi1, user=self.contributor)
self.commentByMatt = Comment.objects.create(username='saul', comment="this is Matt Damon", materi=self.materi2, user=self.mattDamon)
self.url = '/kontributor/' + str(self.contributor.id) + '/comments/'
def test_comments_url_exist(self):
self.client.login(**self.contributor_credential)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.status_code, 404)
def test_comments_using_comments_template(self):
self.client.login(**self.contributor_credential)
response = self.client.get(self.url)
self.assertTemplateUsed(response, 'comments.html')
def test_comments_using_comments_func(self):
self.client.login(**self.contributor_credential)
found = resolve(self.url)
self.assertEqual(found.func.__name__, CommentsView.as_view().__name__)
def test_comments_page_render_comments(self):
self.client.login(**self.contributor_credential)
response = self.client.get(self.url)
self.assertContains(response, "this is contributor comment")
self.assertNotContains(response, 'bukan comment')
def test_comments_page_only_render_specific_user_comments(self):
self.client.login(**self.contributor_credential)
response = self.client.get(self.url)
self.assertContains(response, "this is contributor comment")
self.assertNotContains(response, "this is Matt Damon")
def test_comments_page_only_for_specific_contributor(self):
self.client.login(**self.matt_damon_credential)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
self.assertNotEqual(response.status_code, 200)
class TemplateLoaderTest(TestCase):
def test_template_loader_url_exist(self):
url = "/test-page.html"
......
......@@ -4,7 +4,7 @@ from app import views
from app.views import (DashboardKontributorView, ProfilKontributorView,
SuksesLoginAdminView, SuksesLoginKontributorView,
SuntingProfilView, UploadMateriHTML, UploadMateriView,
ProfilAdminView, SuntingProfilAdminView)
ProfilAdminView, CommentsView, SuntingProfilAdminView)
urlpatterns = [
path("", views.DaftarKatalog.as_view(), name="daftar_katalog"),
......@@ -23,5 +23,6 @@ urlpatterns = [
path("sukses-admin/", SuksesLoginAdminView.as_view(), name="sukses-admin"),
re_path(r"^.*\.html", views.pages, name="pages"),
path("profil-admin/", ProfilAdminView.as_view(), name="profil-admin"),
path("sunting-admin/", SuntingProfilAdminView.as_view(), name="sunting-admin")
path('kontributor/<int:pk_user>/comments/', CommentsView.as_view(), name='comments'),
path("sunting-admin/", SuntingProfilAdminView.as_view(), name="sunting-admin"),
]
......@@ -14,7 +14,8 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.template import loader
from django.urls import reverse
from django.views.generic import TemplateView, ListView
from .models import Category, Comment, Materi
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from app.forms import SuntingProfilForm, UploadMateriForm
from app.models import Category, Comment, Materi, Like, ViewStatistics, DownloadStatistics
from authentication.models import User
......@@ -111,7 +112,6 @@ class DetailMateri(TemplateView):
materi = get_object_or_404(Materi, pk=kwargs["pk"])
user_obj = request.user if request.user.is_authenticated else None
print(user_obj)
comment = Comment.objects.create(comment=commentText,
username=self.get_user_name(request), materi=materi, user=user_obj)
comment.save()
......@@ -164,7 +164,6 @@ def download_materi(request, pk):
else:
raise Http404("File tidak dapat ditemukan.")
def view_materi(request, pk):
materi = get_object_or_404(Materi, pk=pk)
path = materi.content.path
......@@ -183,7 +182,6 @@ def view_materi(request, pk):
else:
raise Http404("File tidak dapat ditemukan.")
class UploadMateriView(TemplateView):
template_name = "unggah.html"
context = {}
......@@ -203,9 +201,7 @@ class UploadMateriView(TemplateView):
kateg = form.cleaned_data['categories']
for i in kateg:
materi.categories.add(i)
messages.success(
request, "Materi berhasil diunggah, periksa riwayat unggah anda")
messages.success(request, "Materi berhasil diunggah, periksa riwayat unggah anda")
return HttpResponseRedirect("/unggah/")
else:
context = self.get_context_data(**kwargs)
......@@ -224,7 +220,6 @@ class UploadMateriView(TemplateView):
class UploadMateriHTML(TemplateView):
template_name = "unggah.html"
context = {}
def get_template_names(self):
if self.request.path == "/unggah/":
template_name = "unggah.html"
......@@ -407,6 +402,32 @@ class SuksesLoginAdminView(TemplateView):
return self.render_to_response(context)
class CommentsView(TemplateView):
template_name = "comments.html"
def dispatch(self, request, *args, **kwargs):
if not request.user.pk == kwargs["pk_user"] :
raise PermissionDenied(request)
return super(CommentsView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(CommentsView, self).get_context_data(**kwargs)
user = get_object_or_404(User, pk=kwargs["pk_user"])
users_materi = Materi.objects.filter(uploader=user)
numb_of_comments = 0
qset_comments = Comment.objects.none()
for materi in users_materi:
materi_comments = Comment.objects.filter(materi=materi).count()
numb_of_comments += materi_comments
qset_comments |= Comment.objects.filter(materi=materi)
context['comments'] = qset_comments.order_by('-timestamp')
context["numb_of_comments"] = numb_of_comments
context["numb_of_materi"] = users_materi.count()
return context
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context=context)
def pages(request):
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