From f807dc0e7325fbe2a7d2fa3be8fcbb0ef8f5bbd1 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Thu, 1 Oct 2020 23:10:26 +0700 Subject: [PATCH 01/14] [RED] Add test for url mapping to PostsView --- app/tests.py | 14 +++++++++++++- app/views.py | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/tests.py b/app/tests.py index 9a6e595..25a879a 100644 --- a/app/tests.py +++ b/app/tests.py @@ -17,7 +17,7 @@ from .models import Category, Comment, Materi, Like, Rating from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri, ProfilKontributorView, SuksesLoginAdminView, SuksesLoginKontributorView, SuntingProfilView, - ProfilAdminView, CommentsView, SuntingProfilAdminView, RevisiMateriView) + ProfilAdminView, CommentsView, PostsView, SuntingProfilAdminView, RevisiMateriView) class DaftarKatalogTest(TestCase): @@ -159,6 +159,18 @@ class DetailMateriTest(TestCase): self.assertEqual(Comment.objects.all().filter( comment="This is new comment by Anonymous").count(), 0) + +class PostsViewTest(TestCase): + + @classmethod + def setUpTestData(cls): + cls.url = '/posts/' + + def test_url_resolves_to_posts_view(self): + found = resolve(self.url) + self.assertEqual(found.func.__name__, PostsView.as_view().__name__) + + class ViewCommentsTest(TestCase): def setUp(self): self.client = Client() diff --git a/app/views.py b/app/views.py index a505a3e..2a69519 100644 --- a/app/views.py +++ b/app/views.py @@ -433,6 +433,10 @@ class SuksesLoginAdminView(TemplateView): return self.render_to_response(context) +class PostsView(TemplateView): + pass + + class CommentsView(TemplateView): template_name = "comments.html" -- GitLab From f3f2a329804e6aaf45baa9e515927c17a1b57e9c Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Thu, 1 Oct 2020 23:13:12 +0700 Subject: [PATCH 02/14] [GREEN] Set url mapping for /posts/ to the PostsView --- app/urls.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/urls.py b/app/urls.py index 5939f9e..a467666 100644 --- a/app/urls.py +++ b/app/urls.py @@ -4,7 +4,7 @@ from app import views from app.views import (DashboardKontributorView, ProfilKontributorView, SuksesLoginAdminView, SuksesLoginKontributorView, SuntingProfilView, UploadMateriHTML, UploadMateriView, - ProfilAdminView, CommentsView, SuntingProfilAdminView) + ProfilAdminView, CommentsView, PostsView, SuntingProfilAdminView) urlpatterns = [ path("", views.DaftarKatalog.as_view(), name="daftar_katalog"), @@ -25,5 +25,6 @@ urlpatterns = [ re_path(r"^.*\.html", views.pages, name="pages"), path("profil-admin/", ProfilAdminView.as_view(), name="profil-admin"), path('kontributor/<int:pk_user>/comments/', CommentsView.as_view(), name='comments'), + path("posts/", PostsView.as_view(), name='posts'), path("sunting-admin/", SuntingProfilAdminView.as_view(), name="sunting-admin"), ] -- GitLab From c46091c11d0db55b077ce0eda076a1f258c96df8 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Thu, 1 Oct 2020 23:17:05 +0700 Subject: [PATCH 03/14] [RED] Add tests on response status based on authentication status --- app/tests.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/tests.py b/app/tests.py index 25a879a..647a206 100644 --- a/app/tests.py +++ b/app/tests.py @@ -2,7 +2,7 @@ import json from io import StringIO from django.contrib.auth import get_user_model -from django.core.exceptions import ValidationError +from django.core.exceptions import PermissionDenied, ValidationError from django.core.files.uploadedfile import SimpleUploadedFile from django.core.management import call_command from django.test import Client, RequestFactory, TestCase @@ -165,10 +165,25 @@ class PostsViewTest(TestCase): @classmethod def setUpTestData(cls): cls.url = '/posts/' + cls.user_credentials = { + "email": "user@email.com", + "password": "justpass" + } + cls.user = User.objects.create_user(**cls.user_credentials, is_contributor=True) def test_url_resolves_to_posts_view(self): found = resolve(self.url) self.assertEqual(found.func.__name__, PostsView.as_view().__name__) + + def test_returns_200_on_authenticated_access(self): + self.client.login(**self.user_credentials) + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + + def test_returns_403_on_unauthenticated_access(self): + response = self.client.get(self.url) + self.assertRaises(PermissionDenied) + self.assertEqual(response.status_code, 403) class ViewCommentsTest(TestCase): -- GitLab From 58b0d796af2431e80019abd013690b8ab89fb0d0 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Thu, 1 Oct 2020 23:19:07 +0700 Subject: [PATCH 04/14] [GREEN] Implement authentication guard on PostsView --- app/views.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/views.py b/app/views.py index 2a69519..1b50a36 100644 --- a/app/views.py +++ b/app/views.py @@ -434,8 +434,13 @@ class SuksesLoginAdminView(TemplateView): class PostsView(TemplateView): - pass + def dispatch(self, request, *args, **kwargs): + if not request.user.is_authenticated: + raise PermissionDenied(request) + return super(PostsView, self).dispatch(request, *args, **kwargs) + def get(self, request, *args, **kwargs): + return HttpResponse() class CommentsView(TemplateView): template_name = "comments.html" -- GitLab From d3e60095b38f43772fe12b75fdb1d0237ae9c18b Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Thu, 1 Oct 2020 23:22:07 +0700 Subject: [PATCH 05/14] [RED] Add template assertion test --- app/tests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/tests.py b/app/tests.py index 647a206..f16da15 100644 --- a/app/tests.py +++ b/app/tests.py @@ -185,6 +185,11 @@ class PostsViewTest(TestCase): self.assertRaises(PermissionDenied) self.assertEqual(response.status_code, 403) + def test_returns_correct_template(self): + self.client.login(**self.user_credentials) + response = self.client.get(self.url) + self.assertTemplateUsed(response, "user_uploaded_posts.html") + class ViewCommentsTest(TestCase): def setUp(self): -- GitLab From 4dc9e9f044f3e686238f5a8d13a41f453231a5c0 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Thu, 1 Oct 2020 23:24:37 +0700 Subject: [PATCH 06/14] [GREEN] Set template to render on PostsView --- app/templates/user_uploaded_posts.html | 11 +++++++++++ app/views.py | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 app/templates/user_uploaded_posts.html diff --git a/app/templates/user_uploaded_posts.html b/app/templates/user_uploaded_posts.html new file mode 100644 index 0000000..bd2bde6 --- /dev/null +++ b/app/templates/user_uploaded_posts.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Document</title> +</head> +<body> + +</body> +</html> diff --git a/app/views.py b/app/views.py index 1b50a36..c72bc93 100644 --- a/app/views.py +++ b/app/views.py @@ -434,13 +434,17 @@ class SuksesLoginAdminView(TemplateView): class PostsView(TemplateView): + + template_name = "user_uploaded_posts.html" + def dispatch(self, request, *args, **kwargs): if not request.user.is_authenticated: raise PermissionDenied(request) return super(PostsView, self).dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): - return HttpResponse() + return self.render_to_response(context={}) + class CommentsView(TemplateView): template_name = "comments.html" -- GitLab From 437eddd85720ece573187874fd3c3069af81c176 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Fri, 2 Oct 2020 00:17:21 +0700 Subject: [PATCH 07/14] [RED] Add tests for context data returned by PostsView --- app/tests.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app/tests.py b/app/tests.py index f16da15..542442c 100644 --- a/app/tests.py +++ b/app/tests.py @@ -162,6 +162,42 @@ class DetailMateriTest(TestCase): class PostsViewTest(TestCase): + @classmethod + def generate_posts_data(cls, user): + POST_COUNT = 3 + COMMENTS_COUNT_PER_POST = [1, 0, 3] + + assert POST_COUNT == len(COMMENTS_COUNT_PER_POST) + + sample_file = SimpleUploadedFile("Test.jpg", b"Test file") + sample_category = Category.objects.create(name="Test Category") + + post_comment_group_dict = {} + for _ in range(POST_COUNT): + post = Materi.objects.create( + uploader=user, + cover=sample_file, + content=sample_file, + ) + post.categories.add(sample_category) + + post_comment_group_dict[post.id] = { + "data": post, + "comments": [], + } + + for i, post_id in enumerate(post_comment_group_dict): + post = post_comment_group_dict[post_id]["data"] + + for _ in range(COMMENTS_COUNT_PER_POST[i]): + comment = Comment.objects.create(materi=post) + post_comment_group_dict[post_id]["comments"].append(comment) + + # order by latest (-timestamp) + post_comment_group_dict[post_id]["comments"].reverse() + + return post_comment_group_dict + @classmethod def setUpTestData(cls): cls.url = '/posts/' @@ -170,6 +206,7 @@ class PostsViewTest(TestCase): "password": "justpass" } cls.user = User.objects.create_user(**cls.user_credentials, is_contributor=True) + cls.data = cls.generate_posts_data(cls.user) def test_url_resolves_to_posts_view(self): found = resolve(self.url) @@ -190,6 +227,19 @@ class PostsViewTest(TestCase): response = self.client.get(self.url) self.assertTemplateUsed(response, "user_uploaded_posts.html") + def test_success_returns_correct_comment_post_groupings_by_context(self): + post_comment_group_dict = self.data + + self.client.login(**self.user_credentials) + response = self.client.get(self.url) + + response_user = response.context_data["user"] + self.assertEqual(response_user, self.user) + + response_data = response.context_data["posts"] + actual_data = post_comment_group_dict + self.assertDictEqual(response_data, actual_data) + class ViewCommentsTest(TestCase): def setUp(self): -- GitLab From 47eaf2bd306e0f8600a291302b538ce9c2d92648 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Fri, 2 Oct 2020 00:45:15 +0700 Subject: [PATCH 08/14] [GREEN] Implement view returns grouped post and comments by context --- app/views.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/views.py b/app/views.py index c72bc93..538ae3b 100644 --- a/app/views.py +++ b/app/views.py @@ -443,7 +443,23 @@ class PostsView(TemplateView): return super(PostsView, self).dispatch(request, *args, **kwargs) def get(self, request, *args, **kwargs): - return self.render_to_response(context={}) + context = super().get_context_data(**kwargs) + user = self.request.user + + posts = Materi.objects.filter(uploader=user).order_by("-date_created") + posts_data = { post.id: { "data": post, "comments": [] } for post in posts } + + comments = Comment.objects \ + .filter(materi__id__in=posts_data.keys()) \ + .order_by("-timestamp") + + for comment in comments: + posts_data[comment.materi.id]["comments"].append(comment) + + context["user"] = user + context["posts"] = posts_data + + return self.render_to_response(context=context) class CommentsView(TemplateView): -- GitLab From 431b8acb568ecc102e5b56a504f95fd8c4e6acc4 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Fri, 2 Oct 2020 01:18:38 +0700 Subject: [PATCH 09/14] [RED] Add test view groups comments per post in the html template --- app/tests.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/tests.py b/app/tests.py index 542442c..485d8b1 100644 --- a/app/tests.py +++ b/app/tests.py @@ -240,6 +240,21 @@ class PostsViewTest(TestCase): actual_data = post_comment_group_dict self.assertDictEqual(response_data, actual_data) + def test_html_contains_grouped_posts_and_comments(self): + self.client.login(**self.user_credentials) + response = self.client.get(self.url) + + self.assertRegex( + str(response.content), + r'.*(<div id="post-3">)' + \ + r'.*(<div id="post-3-comment-4">)' + \ + r'.*(<div id="post-3-comment-3">)' + \ + r'.*(<div id="post-3-comment-2">)' + \ + r'.*(<div id="post-2">)' + \ + r'.*(<div id="post-1">)' + \ + r'.*(<div id="post-1-comment-1">)' + ) + class ViewCommentsTest(TestCase): def setUp(self): -- GitLab From 559c465fc8d8c3fd11289f6e3a917437ef7555a8 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Sat, 3 Oct 2020 20:42:51 +0700 Subject: [PATCH 10/14] [GREEN] Implement grouping on the html template returned by PostsView --- app/static/app/css/user_uploaded_posts.css | 50 +++++++++++++ app/templates/user_uploaded_posts.html | 83 +++++++++++++++++++--- 2 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 app/static/app/css/user_uploaded_posts.css diff --git a/app/static/app/css/user_uploaded_posts.css b/app/static/app/css/user_uploaded_posts.css new file mode 100644 index 0000000..46d49de --- /dev/null +++ b/app/static/app/css/user_uploaded_posts.css @@ -0,0 +1,50 @@ +.posts-vertically-centered { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: 1rem; +} + +.posts-space-between-container { + display: flex; + flex-direction: row; + justify-content: space-between; +} + +#posts-profile-picture { + border-radius: 50%; +} + +#posts-user-profile { + display: flex; + flex-direction: row; +} + +#posts-user-name { + display: flex; + justify-content: center; + align-items: center; + margin-left: 1rem; + font-size: 1.5rem; +} + +#posts-img { + width: 100px; + height: auto; + height: inherit !important; + margin-right: 1rem; +} + +#posts-info { + display: flex; + flex-direction: column; + justify-content: center; +} + +#posts-comment-info { + display: flex; + flex-direction: row; + margin: 0.5rem 6rem 0.5rem 6rem; + padding: 0.5rem; +} diff --git a/app/templates/user_uploaded_posts.html b/app/templates/user_uploaded_posts.html index bd2bde6..7d3d893 100644 --- a/app/templates/user_uploaded_posts.html +++ b/app/templates/user_uploaded_posts.html @@ -1,11 +1,72 @@ -<!DOCTYPE html> -<html lang="en"> -<head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>Document</title> -</head> -<body> - -</body> -</html> +{% extends 'app/base_dashboard.html' %} +{% load static %} + +{% block title %} +<title>Materi Diunggah | Digipus</title> +{% endblock %} + +{% block stylesheets %} +<link rel="stylesheet" type="text/css" href="{% static 'app/css/user_uploaded_posts.css' %}"/> +{% endblock %} + +{% block content %} +<div class="posts-space-between-container bg-white rounded shadow bd-highlight" style="padding: 1rem 1.5rem; margin-bottom: 1.5rem;"> + <div id="posts-user-profile"> + <img id="posts-profile-picture" src="{{ user.profile_picture.url }}" alt="profile-picture" width="100px" height="100px"/> + <div id="posts-user-name"> + Materi oleh <b> {{ user.name }}</b> + </div> + </div> + <div class="posts-vertically-centered"> + <span>{{ posts|length }}</span> + <span>Materi</span> + </div> +</div> + +<div style="padding: 1rem 0" id="posts"> + {% if posts %} + {% for _, post in posts.items %} + <div id="post-{{ post.data.id }}"> + <div class="posts-space-between-container bg-white rounded shadow" style="margin: 0.5rem 2rem; padding: 1rem;"> + <div id="posts-user-profile"> + {% if post.data.cover %} + <img id="posts-img" src="{{ post.data.cover.url }}" alt="profile-picture"/> + {% else %} + </div style="background-color: grey; width: 100px; height: 100px;"> + {% endif %} + <div id="posts-info"> + <span><a class="ml-auto p-1 link" style="text-align: left; font-size: 2rem;" href="{% url 'detail-materi' post.data.id %}"> + {{ post.data.title }} + </a></span> + <span style="font-size: 0.75rem; padding-left: 0.3rem;">{{ post.data.date_created }}</span> + </div> + </div> + <div class="posts-vertically-centered"> + <span>{{ post.comments|length }}</span> + <span>Komentar</span> + </div> + </div> + {% for comment in post.comments %} + <div id="post-{{ post.data.id }}-comment-{{ comment.id }}"> + <div id="posts-comment-info" class="bg-white rounded shadow" > + <img id="posts-profile-picture" src="{{ comment.user.profile_picture.url }}" alt="profile-picture" width="40px" height="40px" style="margin: 18px"/> + <div style="display: flex; align-items: center;"> + <div style="display: flex; flex-direction: column;"> + <span style="font-size: 0.75rem;"><b>{{ comment.user.name }}</b> - {{ comment.timestamp }}</span> + {{ comment.comment }} + </div> + </div> + </div> + </div> + {% endfor %} + </div> + {% endfor %} + {% else %} + <div class="text-center h5"> + Anda belum memiliki materi yang telah diunggah / disetujui + </div> + {% endif %} + +</div> + +{% endblock %} -- GitLab From b25bd36d68f4605cbd1738097ed6e7459195338f Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Sat, 3 Oct 2020 21:21:07 +0700 Subject: [PATCH 11/14] [GREEN] Fix id on tests to use dynamic generated ID --- app/tests.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/tests.py b/app/tests.py index 485d8b1..7964dfa 100644 --- a/app/tests.py +++ b/app/tests.py @@ -244,15 +244,21 @@ class PostsViewTest(TestCase): self.client.login(**self.user_credentials) response = self.client.get(self.url) + posts = list(self.data.keys()) + comments = { + i: [comment.id for comment in self.data[post_id]["comments"]] + for i, post_id in enumerate(posts) + } + self.assertRegex( str(response.content), - r'.*(<div id="post-3">)' + \ - r'.*(<div id="post-3-comment-4">)' + \ - r'.*(<div id="post-3-comment-3">)' + \ - r'.*(<div id="post-3-comment-2">)' + \ - r'.*(<div id="post-2">)' + \ - r'.*(<div id="post-1">)' + \ - r'.*(<div id="post-1-comment-1">)' + rf'.*(<div id="post-{posts[2]}">)' + \ + rf'.*(<div id="post-{posts[2]}-comment-{comments[2][0]}">)' + \ + rf'.*(<div id="post-{posts[2]}-comment-{comments[2][1]}">)' + \ + rf'.*(<div id="post-{posts[2]}-comment-{comments[2][2]}">)' + \ + rf'.*(<div id="post-{posts[1]}">)' + \ + rf'.*(<div id="post-{posts[0]}">)' + \ + rf'.*(<div id="post-{posts[0]}-comment-{comments[0][0]}">)' ) -- GitLab From c647417a9cadc6141c52a295ab24a311498aeaae Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Sat, 3 Oct 2020 21:39:00 +0700 Subject: [PATCH 12/14] [GREEN] Remove old CommentsView and all references to it --- app/templates/app/includes/sidebar.html | 6 +-- app/tests.py | 66 +------------------------ app/urls.py | 3 +- app/views.py | 27 ---------- 4 files changed, 5 insertions(+), 97 deletions(-) diff --git a/app/templates/app/includes/sidebar.html b/app/templates/app/includes/sidebar.html index 59f4e67..95d5ea7 100644 --- a/app/templates/app/includes/sidebar.html +++ b/app/templates/app/includes/sidebar.html @@ -29,8 +29,8 @@ </a> </li> <li class="nav-item"> - <a class="nav-link" href="{% url 'comments' user.id %}"> - <span>Komentar</span></a> + <a class="nav-link" href="{% url 'posts' %}"> + <span>Materi Diunggah</span></a> </li> -</ul> \ No newline at end of file +</ul> diff --git a/app/tests.py b/app/tests.py index 7964dfa..ee9585a 100644 --- a/app/tests.py +++ b/app/tests.py @@ -17,7 +17,7 @@ from .models import Category, Comment, Materi, Like, Rating from .views import (DaftarKatalog, DashboardKontributorView, DetailMateri, ProfilKontributorView, SuksesLoginAdminView, SuksesLoginKontributorView, SuntingProfilView, - ProfilAdminView, CommentsView, PostsView, SuntingProfilAdminView, RevisiMateriView) + ProfilAdminView, PostsView, SuntingProfilAdminView, RevisiMateriView) class DaftarKatalogTest(TestCase): @@ -262,70 +262,6 @@ class PostsViewTest(TestCase): ) -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" diff --git a/app/urls.py b/app/urls.py index a467666..32b1a47 100644 --- a/app/urls.py +++ b/app/urls.py @@ -4,7 +4,7 @@ from app import views from app.views import (DashboardKontributorView, ProfilKontributorView, SuksesLoginAdminView, SuksesLoginKontributorView, SuntingProfilView, UploadMateriHTML, UploadMateriView, - ProfilAdminView, CommentsView, PostsView, SuntingProfilAdminView) + ProfilAdminView, PostsView, SuntingProfilAdminView) urlpatterns = [ path("", views.DaftarKatalog.as_view(), name="daftar_katalog"), @@ -24,7 +24,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('kontributor/<int:pk_user>/comments/', CommentsView.as_view(), name='comments'), path("posts/", PostsView.as_view(), name='posts'), path("sunting-admin/", SuntingProfilAdminView.as_view(), name="sunting-admin"), ] diff --git a/app/views.py b/app/views.py index 538ae3b..d5cd67a 100644 --- a/app/views.py +++ b/app/views.py @@ -462,33 +462,6 @@ class PostsView(TemplateView): return self.render_to_response(context=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) - class RevisiMateriView(TemplateView): template_name = "revisi.html" -- GitLab From 12894b722b1fd7e887068529e3f71f6f224526ad Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Sat, 3 Oct 2020 21:42:36 +0700 Subject: [PATCH 13/14] [REFACTOR] Refactor test to use reusable logged in access function --- app/tests.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/tests.py b/app/tests.py index ee9585a..39f14f5 100644 --- a/app/tests.py +++ b/app/tests.py @@ -208,13 +208,16 @@ class PostsViewTest(TestCase): cls.user = User.objects.create_user(**cls.user_credentials, is_contributor=True) cls.data = cls.generate_posts_data(cls.user) + def _request_as_user(self): + self.client.login(**self.user_credentials) + return self.client.get(self.url) + def test_url_resolves_to_posts_view(self): found = resolve(self.url) self.assertEqual(found.func.__name__, PostsView.as_view().__name__) def test_returns_200_on_authenticated_access(self): - self.client.login(**self.user_credentials) - response = self.client.get(self.url) + response = self._request_as_user() self.assertEqual(response.status_code, 200) def test_returns_403_on_unauthenticated_access(self): @@ -223,15 +226,13 @@ class PostsViewTest(TestCase): self.assertEqual(response.status_code, 403) def test_returns_correct_template(self): - self.client.login(**self.user_credentials) - response = self.client.get(self.url) + response = self._request_as_user() self.assertTemplateUsed(response, "user_uploaded_posts.html") def test_success_returns_correct_comment_post_groupings_by_context(self): post_comment_group_dict = self.data - self.client.login(**self.user_credentials) - response = self.client.get(self.url) + response = self._request_as_user() response_user = response.context_data["user"] self.assertEqual(response_user, self.user) @@ -241,8 +242,7 @@ class PostsViewTest(TestCase): self.assertDictEqual(response_data, actual_data) def test_html_contains_grouped_posts_and_comments(self): - self.client.login(**self.user_credentials) - response = self.client.get(self.url) + response = self._request_as_user() posts = list(self.data.keys()) comments = { -- GitLab From ad4b624cfdb43b0a77c121260e8fe1750d36bf28 Mon Sep 17 00:00:00 2001 From: Jonathan Christopher Jakub <jonathan.christopher@ui.ac.id> Date: Sat, 3 Oct 2020 22:05:50 +0700 Subject: [PATCH 14/14] [REFACTOR] Fix strong tag on html and adjust font size --- app/templates/user_uploaded_posts.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/templates/user_uploaded_posts.html b/app/templates/user_uploaded_posts.html index 7d3d893..2ce0d68 100644 --- a/app/templates/user_uploaded_posts.html +++ b/app/templates/user_uploaded_posts.html @@ -14,7 +14,7 @@ <div id="posts-user-profile"> <img id="posts-profile-picture" src="{{ user.profile_picture.url }}" alt="profile-picture" width="100px" height="100px"/> <div id="posts-user-name"> - Materi oleh <b> {{ user.name }}</b> + Materi oleh <strong> {{ user.name }}</strong> </div> </div> <div class="posts-vertically-centered"> @@ -52,7 +52,7 @@ <img id="posts-profile-picture" src="{{ comment.user.profile_picture.url }}" alt="profile-picture" width="40px" height="40px" style="margin: 18px"/> <div style="display: flex; align-items: center;"> <div style="display: flex; flex-direction: column;"> - <span style="font-size: 0.75rem;"><b>{{ comment.user.name }}</b> - {{ comment.timestamp }}</span> + <span style="font-size: 0.9rem;"><strong>{{ comment.user.name }}</strong> - {{ comment.timestamp }}</span> {{ comment.comment }} </div> </div> -- GitLab