Fakultas Ilmu Komputer UI

Commit 563ffecd authored by Saul Andre's avatar Saul Andre
Browse files

[GREEN] Add post method for comments in Detail Materi page

parent ebfd07e1
# Generated by Django 3.0.3 on 2020-04-23 02:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0010_auto_20200422_2206'),
]
operations = [
migrations.AddField(
model_name='materi',
name='uploadDate',
field=models.DateField(auto_now=True),
),
migrations.AlterField(
model_name='comment',
name='profile',
field=models.CharField(default='2762c7', max_length=100),
),
migrations.AlterField(
model_name='comment',
name='user',
field=models.CharField(default='fa2313dd18ef4ad898cdb71f6c17a2b8', max_length=100),
),
]
# Generated by Django 3.0.3 on 2020-04-23 03:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0011_auto_20200423_0906'),
]
operations = [
migrations.AlterField(
model_name='comment',
name='profile',
field=models.CharField(default='6e9f9f', max_length=100),
),
migrations.AlterField(
model_name='comment',
name='user',
field=models.CharField(default='70e92c7708a34f8997c7cbe9f595c5a9', max_length=100),
),
]
......@@ -35,6 +35,7 @@ class Materi(models.Model):
uploader = models.CharField(max_length=30, default='uploader') #ubah jadi one to one ke kontributor
publisher = models.CharField(max_length=30, default='publiser')
descriptions = models.TextField(default='descriptions')
uploadDate = models.DateField(auto_now=True)
verified = models.BooleanField(default=False, null=True)
categories = models.ManyToManyField(Category)
comments = models.ManyToManyField(Comment)
......
......@@ -83,14 +83,16 @@ Materi
<div id="section2" class="container-fluid comments-wrapper">
<h1>Komentar</h1>
<div class="add-comments col col-8 ">
<form>
<form method="POST">
{% csrf_token %}
<div class="form-group">
<textarea placeholder="komentar..."
class="form-control mb-2"
id="exampleFormControlTextarea1"
rows="3">
rows="3"
name="comment">
</textarea>
<button type="submit" class="btn btn-book shadow-sm p-2 mb-1 bg-white rounded">Confirm identity</button>
<button type="submit" class="btn btn-book shadow-sm p-2 mb-1 bg-white rounded">Kirim</button>
</div>
</form>
......
......@@ -73,7 +73,7 @@
<h5 class="card-title">{{item.title}}</h5>
<p class="card-text">{{item.author}}</p>
<button class="btn btn-book">Baca</button>
<button class="btn btn-book" onclick="">Detail</button>
<a class="btn btn-book" href= {% url "detailMateri" item.id %}>Detail</a>
</div>
</div>
{% endfor %}
......
......@@ -4,6 +4,7 @@ from .views import DaftarKatalog, DetailMateri
from .models import Materi, Category, Comment
from django.core import serializers
import json
import uuid
class DaftarKatalogTest(TestCase):
......@@ -31,11 +32,17 @@ class DaftarKatalogTest(TestCase):
self.assertEqual(resp,materi)
class DetailMateriTest(TestCase):
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.",
)
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',
uploader= "Bayu",
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.",
)
......@@ -48,7 +55,6 @@ class DetailMateriTest(TestCase):
materi = Materi.objects.create(title="wahyu",
cover='https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg',
author='Saul Andre Lumban Gaol',
uploader= "Bayu",
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.",
)
......@@ -76,6 +82,47 @@ class DetailMateriTest(TestCase):
self.assertEqual(test.__str__(), "saul")
self.assertNotEqual(test.__str__(), "userlain")
def test_comment_post_rendered_the_comment(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().post(url, {'comment': 'ini test comment'})
response2 = Client().get(url)
self.assertContains(response2, "ini test comment")
self.assertNotContains(response2, "ini bukan test comment")
def test_register_form_field(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)
# Positive test
self.assertContains(response, "komentar...")
# Negative tests
self.assertNotContains(response, 'Email')
def test_submit_comment(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.",
)
# 302 meaning successful login and redirected
url = "/materi/" + str(materi.id) + "/"
response = Client().post(
url, {'comment': 'ini comment baru'})
self.assertEqual(302, response.status_code)
class TemplateLoaderTest(TestCase):
def test_template_loader_url_exist(self):
url = "/test-page.html"
......
......@@ -3,7 +3,7 @@ from django.template import loader
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.views.generic import TemplateView
from django.core import serializers
from .models import Materi
from .models import Materi, Comment
class DaftarKatalog(TemplateView):
template_name = "app/katalog_materi.html"
......@@ -25,21 +25,35 @@ class DetailMateri(TemplateView):
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
print(context['slug'])
# print(request.path)
context["materi_data"] = get_object_or_404(Materi, pk=kwargs['slug'])
print(context["materi_data"])
return self.render_to_response(context=context)
# class DetailMateri(TemplateView):
# template_name = "app/detail_materi.html"
# def get_context_data(self, **kwargs):
# return super().get_context_data(**kwargs)
# def get(self, request, *args, **kwargs):
# context = self.get_context_data(**kwargs)
# return self.render_to_response(context=context)
def post(self, request, *args, **kwargs):
comment = request.POST.get("comment", None)
url = request.path
comment = Comment.objects.create(comment=comment)
materi = get_object_or_404(Materi, pk=kwargs['slug'])
if comment == None or comment =='':
context = self.get_context_data(*args, **kwargs)
context["error_message"] = "Anda belum menuliskan komentar"
return self.render_to_response(context=context)
else:
if request.user.is_authenticated:
if request.user.is_superuser:
username = "Admin"
else:
username = request.user.name
comment.user = username
comment.save()
materi.comments.add(comment)
materi.save()
return HttpResponseRedirect(url)
else:
comment.save()
materi.comments.add(comment)
materi.save()
return HttpResponseRedirect(url)
def pages(request):
context = {}
......
......@@ -260,7 +260,7 @@ iframe {
min-height: 100vh;
display: block;
background-color: #f7f7f7;
padding: 100px 55px 55px 55px;
padding: 173px 55px 55px 55px;
}
.login100-form-title {
......
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