Fakultas Ilmu Komputer UI

Commit ed5a2b94 authored by Saul Andre's avatar Saul Andre
Browse files

[RED]Create test for comment form, and update models

parent 1277a8be
......@@ -18,5 +18,10 @@
"model": "app.category",
"pk": 4,
"fields": { "name": "kategori 2", "description": "Sebuah kategori 2" }
},
{
"model": "app.comment",
"pk": 1,
"fields": { "user": "Admin", "profile": "ffffff", "comment": "Diskusi disini yuk!" }
}
]
# Generated by Django 3.0.3 on 2020-04-30 10:49
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='comment',
name='profile',
field=models.CharField(default='dc2050', max_length=100),
),
migrations.AlterField(
model_name='comment',
name='user',
field=models.CharField(default='558d2f17185f4a0596fe3362549614a2', max_length=100),
),
migrations.RemoveField(
model_name='materi',
name='comments',
),
migrations.AddField(
model_name='materi',
name='comments',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='app.Comment'),
),
]
......@@ -47,6 +47,6 @@ class Materi(models.Model):
publisher = models.CharField(max_length=30, default="publiser")
descriptions = models.TextField(default="descriptions")
status = models.CharField(max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0])
comments = models.ForeignKey(Comment, on_delete=models.CASCADE, default=1)
categories = models.ManyToManyField(Category)
comments = models.ManyToManyField(Comment)
feedback = models.TextField(blank=True, default="")
No preview for this file type
No preview for this file type
......@@ -4,6 +4,7 @@
{% block header %}
<link rel="stylesheet" type="text/css" href="{% static 'app/css/detail_materi.css' %}">
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,500;1,100&display=swap" rel="stylesheet">
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v6.0"></script>
{% endblock header %}
{% block title %}
......@@ -12,6 +13,7 @@ Materi
{% block content %}
{% block verification %} {% endblock verification %}
<div id="fb-root"></div>
<div class="container">
<div class="row materi-data">
<div class="col col-3 cover">
......@@ -58,82 +60,71 @@ Materi
</dd>
</div>
</div>
<a href="{% url 'view-materi' materi_data.id %}" class="btn btn-link btn-book shadow-sm p-2 mb-1 bg-white rounded">Baca</a>
<a href="{% url 'download-materi' materi_data.id %}" class="btn btn-link btn-book shadow-sm p-2 mb-1 bg-white rounded">Unduh</a>
<button class="btn btn-link btn-book shadow-sm p-2 mb-1 bg-white rounded">Bagikan</button>
<div class="buttons d-flex flex-row bd-highlight mb-1">
<a href="{% url 'view-materi' materi_data.id %}" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded">Baca</a>
<a href="{% url 'download-materi' materi_data.id %}" class="btn btn-link btn-book shadow-sm p-2 mr-2 bg-white rounded">Unduh</a>
<div class="dropdown">
<button class="btn dropdown-toggle btn-book shadow-sm p-2 mr-2 bg-white rounded" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Bagikan
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<div class="fb-share-button" data-href="{% url 'detail-materi' materi_data.id %}" data-layout="button" data-size="small">
<a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdigipus-staging-2.herokuapp.com%2Fmateri%2F{{materi_data.id}}%2F&amp;src=sdkpreparse" class="dropdown-item fb-xfbml-parse-ignore">Facebook</a>
</div>
<a class="twitter-share-button dropdown-item"
href="https://twitter.com/intent/tweet?text=Cek%20materi%20ini%20yuk%20https%3A%2F%2Fdigipus-staging-2.herokuapp.com%2Fmateri%2F{{materi_data.id}}%2F"
data-size="large">
Twitter
</a>
</div>
</div>
</div>
</div>
</div>
<div class="row menu-wrapper">
<nav class="navbar navbar-expand-sm">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#section1">Deskripsi</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Komentar</a>
</li>
</ul>
</nav>
<div id="section1" class="container-fluid description-wrapper">
<h1>Deskripsi</h1>
<div class="col col-8 description">
<p>{{materi_data.descriptions}}</p>
</div>
<nav class="navbar navbar-expand-sm">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#section1">Deskripsi</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Komentar</a>
</li>
</ul>
</nav>
<div id="section1" class="container-fluid description-wrapper">
<h1>Deskripsi</h1>
<div class="col col-8 description">
<p>{{materi_data.descriptions}}</p>
</div>
<div id="section2" class="container-fluid comments-wrapper">
<h1>Komentar</h1>
<div class="add-comments col col-8 ">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<textarea placeholder="komentar..."
class="form-control mb-2"
id="exampleFormControlTextarea1"
rows="3"
name="comment">
</textarea>
<button type="submit" class="btn btn-book shadow-sm p-2 mb-1 bg-white rounded">Kirim</button>
</div>
</form>
</div>
{% for comment in materi_data.comments.all %}
<div class="col col-8 comment shadow-sm p-3 mb-1 bg-white rounded">
<div class="d-flex bd-highlight mb-3 align-items-center user">
<span style="background-color: #{{comment.profile}}" class="profile p-1 bd-highligh"></span>
<p class="p-1 bd-highligh"><b>{{comment.user}}</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>
<div id="section2" class="container-fluid comments-wrapper">
<h1>Komentar</h1>
<div class="add-comments col col-8 ">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<textarea placeholder="komentar..." class="form-control" id="exampleFormControlTextarea1" rows="3" name="comment"></textarea>
</div>
<button type="submit" class="btn btn-book shadow-sm p-2 mb-1 bg-white rounded">Kirim</button>
</div>
{% endfor %}
</form>
</div>
<!-- <div class="col col-12">
<nav id="navbar-example2" class="navbar ">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#fat">Deskripsi</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#mdo">Komentar</a>
</li>
</ul>
</nav>
</div>
<div class="col col-8 offset-md-1">
<div data-spy="scroll" data-target="#navbar-example2" data-offset="0">
<div class="description-wrapper">
<h4 id="fat">Deskripsi</h4>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
{% for comment in materi_data.comments.all %}
<div class="col col-8 comment shadow-sm p-3 mb-1 bg-white rounded">
<div class="d-flex bd-highlight mb-3 align-items-center user">
<span style="background-color: #{{comment.profile}}" class="profile p-1 bd-highligh"></span>
<p class="p-1 bd-highligh"><b>{{comment.user}}</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>
</div>
</div> -->
{% endfor %}
</div>
</div>
</div>
{% endblock content %}
\ No newline at end of file
......@@ -3,7 +3,8 @@ import json
from django.core import serializers
from django.test import Client, TestCase
from django.urls import resolve
from authentication.models import User
from django.core.files import File
from .models import Category, Comment, Materi
from .views import DaftarKatalog, DetailMateri
......@@ -60,31 +61,30 @@ class DaftarKatalogTest(TestCase):
class DetailMateriTest(TestCase):
# def test_detail_materi_url_exist(self):
# materi = Materi.objects.create(title="wahyu",
# cover='https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg',
# author='Saul Andre Lumban Gaol',
# publisher='Diskominfo Depok',
# descriptions="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
# )
# url = "/materi/" + str(materi.id) + "/"
# response = Client().get(url)
# self.assertEqual(response.status_code,200)
# self.assertNotEqual(response.status_code, 404)
# def test_detail_materi_using_detail_materi_template(self):
# materi = Materi.objects.create(title="wahyu",
# cover='https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg',
# author='Saul Andre Lumban Gaol',
# publisher='Diskominfo Depok',
# descriptions="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
# )
# url = "/materi/" + str(materi.id) + "/"
# response = Client().get(url)
# self.assertTemplateUsed(response, 'app/detail_materi.html')
def setUp(self):
User.objects._create_user(email="alice@acme.com", password="acmecorp", is_admin=True)
User.objects._create_user(email="kontrib@kontrib.com", password="acmecorp", is_contributor=True)
def get_url_for_detail_materi():
materi = Materi.objects.create()
materi.title = "wahyu"
materi.author = "Saul Andre Lumban Gaol"
materi.publisher = "Diskominfo Depok"
materi.descriptions = "This is test descriptions"
url = "/materi/" + str(materi.id) + "/"
return url
def test_detail_materi_url_exist(self):
response = Client().get(self.get_url_for_detail_materi())
self.assertEqual(response.status_code,200)
self.assertNotEqual(response.status_code, 404)
def test_detail_materi_using_detail_materi_template(self):
response = Client().get(self.get_url_for_detail_materi())
self.assertTemplateUsed(response, 'app/detail_materi.html')
def test_detail_materi_using_detail_materi_func(self):
found = resolve("/materi/1/")
found = resolve(self.get_url_for_detail_materi())
self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
def test_category_models_can_create_new_object(self):
......@@ -102,7 +102,45 @@ class DetailMateriTest(TestCase):
self.assertNotEqual(0, countData)
self.assertEqual(test.__str__(), "saul")
self.assertNotEqual(test.__str__(), "userlain")
def test_post_blank_comment(self):
url = self.get_url_for_detail_materi()
response = Client().post(url, {"comment": ""})
self.assertIn("error_message", response.context_data)
self.assertIn("Anda belum menuliskan komentar", response.context_data["error_message"])
def test_comment_rendered_to_interface(self):
url = self.get_url_for_detail_materi()
Client().post(url, {"comment": "This is my new comment"})
response = Client().get(url)
self.assertContains(response, "This is my new comment")
def test_comment_by_admin(self):
url = self.get_url_for_detail_materi()
Client().post("/login_admin/", {"email": "alice@acme.com", "pass": "acmecorp"})
Client().post(url, {"comment": "This is new comment by Admin"})
response = Client.get(url)
self.assertContains(response, "Admin")
def test_comment_by_kontributor(self):
url = self.get_url_for_detail_materi()
Client().post("/login/", {"email": "kontrib@kontrib.com", "pass": "acmecorp"})
Client().post(url, {"comment": "This is new comment by Contributor"})
response = Client.get(url)
self.assertContains(response, "Contributor")
def test_comment_by_anonymous(self):
url = self.get_url_for_detail_materi()
Client().get("/logout/")
Client().post(url, {"comment": "This is new comment by Anonymous"})
response = Client.get(url)
self.assertContains(response, "Anonymous")
def test_detail_materi_contains_form_comment(self):
response = Client.get(self.get_url_for_detail_materi())
self.assertContains(response, "komentar...")
class TemplateLoaderTest(TestCase):
def test_template_loader_url_exist(self):
......
......@@ -72,15 +72,15 @@ class DetailMateri(TemplateView):
return self.render_to_response(context=context)
def post(self, request, *args, **kwargs):
comment = request.POST.get("comment", None)
commentText = request.POST.get("comment", None)
url = request.path
comment = Comment.objects.create(comment=comment)
materi = get_object_or_404(Materi, pk=kwargs['pk'])
if comment == None or comment =='':
if commentText == None or commentText== '':
context = self.get_context_data(*args, **kwargs)
context["error_message"] = "Anda belum menuliskan komentar"
return self.render_to_response(context=context)
else:
comment = Comment.objects.create(comment=commentText)
if request.user.is_authenticated:
if request.user.is_superuser:
username = "Admin"
......@@ -106,6 +106,13 @@ def delete_comment(request, pk_materi, pk_comment):
comment.delete()
return HttpResponseRedirect(url)
def method1(param1, param2):
temp1 = param1*param2
return temp1
def total_gaji_setahun(gaji, jml_bulan):
total_gaji = gaji*jml_bulan
return total_gaji
def download_materi(request, pk):
materi = get_object_or_404(Materi, pk=pk)
......
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