Fakultas Ilmu Komputer UI

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

[GREEN] implementing form input for add comment

parent ed5a2b94
......@@ -18,10 +18,5 @@
"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
# Generated by Django 3.0.3 on 2020-05-01 07:26
from django.db import migrations, models
import django.db.models.deletion
......@@ -11,23 +11,23 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RemoveField(
model_name='materi',
name='comments',
),
migrations.AddField(
model_name='comment',
name='materi',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='app.Materi'),
),
migrations.AlterField(
model_name='comment',
name='profile',
field=models.CharField(default='dc2050', max_length=100),
field=models.CharField(default='a7f173', 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'),
field=models.CharField(default='8e66c40ba5d8428b8e6f31ee283cd114', max_length=100),
),
]
# Generated by Django 3.0.3 on 2020-05-04 05:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0002_auto_20200501_1426'),
]
operations = [
migrations.AlterField(
model_name='comment',
name='profile',
field=models.CharField(default='57aa9d', max_length=100),
),
migrations.AlterField(
model_name='comment',
name='user',
field=models.CharField(default='20c57b83234747c2a7fe3bcf3f484d99', max_length=100),
),
]
......@@ -29,15 +29,6 @@ class Category(models.Model):
def __str__(self):
return self.name
class Comment(models.Model):
user = models.CharField(max_length=100, default=getRandomUserId())
profile = models.CharField(max_length=100, default=getRandomColor())
comment = models.CharField(max_length=150, default="comments")
def __str__(self):
return self.user
class Materi(models.Model):
cover = models.ImageField()
content = models.FileField()
......@@ -47,6 +38,14 @@ 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)
feedback = models.TextField(blank=True, default="")
class Comment(models.Model):
user = models.CharField(max_length=100, default=getRandomUserId())
profile = models.CharField(max_length=100, default=getRandomColor())
comment = models.CharField(max_length=150, default="comments")
materi = models.ForeignKey(Materi, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.user
......@@ -85,32 +85,34 @@ Materi
<nav class="navbar navbar-expand-sm">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#section1">Deskripsi</a>
<a class="nav-link" href="#deskripsi">Deskripsi</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Komentar</a>
<a class="nav-link" href="#komentar">Komentar</a>
</li>
</ul>
</nav>
<div id="section1" class="container-fluid description-wrapper">
<div id="deskripsi" class="container-fluid description-wrapper">
<h1>Deskripsi</h1>
<div class="col col-8 description">
<p>{{materi_data.descriptions}}</p>
</div>
</div>
<div id="section2" class="container-fluid comments-wrapper">
<div id="komentar" 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>
<textarea placeholder="komentar..." class="form-control"
id="exampleFormControlTextarea1"
rows="3" name="comment" required
></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 %}
{% for comment in comment_data %}
<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>
......
import json
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core import serializers
from django.test import Client, TestCase
from django.urls import resolve
......@@ -7,6 +9,10 @@ from authentication.models import User
from django.core.files import File
from .models import Category, Comment, Materi
from .views import DaftarKatalog, DetailMateri
from django.contrib.auth import get_user_model
class DaftarKatalogTest(TestCase):
......@@ -62,29 +68,46 @@ class DaftarKatalogTest(TestCase):
class DetailMateriTest(TestCase):
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
self.client = Client()
self.admin_credential = {
"email": "admin@gov.id",
"password": "passwordtest"
}
self.contributor_credential = {
"email": "kontributor@gov.id",
"password": "passwordtest"
}
self.anonymous_credential = {
"email": "anonymous@gov.id",
"password": "passwordtest"
}
self.admin = get_user_model().objects.create_user(
**self.admin_credential, name="Admin", is_admin=True)
self.contributor = get_user_model().objects.create_user(
**self.contributor_credential, name="Kontributor", is_contributor=True)
self.anonymous = get_user_model().objects.create_user(
**self.anonymous_credential, name="Anonymous"
)
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="PENDING", cover=self.cover, content=self.content, feedback="").save()
self.materi1 = Materi.objects.first()
self.url= "/materi/" + str(self.materi1.id) + "/"
def test_detail_materi_url_exist(self):
response = Client().get(self.get_url_for_detail_materi())
response = Client().get(self.url)
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())
response = Client().get(self.url)
self.assertTemplateUsed(response, 'app/detail_materi.html')
def test_detail_materi_using_detail_materi_func(self):
found = resolve(self.get_url_for_detail_materi())
found = resolve(self.url)
self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
def test_category_models_can_create_new_object(self):
......@@ -104,40 +127,43 @@ class DetailMateriTest(TestCase):
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"})
url = self.url
self.client.login(**self.anonymous_credential)
response = self.client.post(url, {"comment": ""})
self.assertIn("error_message", response.context)
self.assertIn("Anda belum menuliskan komentar", response.context["error_message"])
def test_comment_rendered_to_template(self):
url = self.url
self.client.login(**self.contributor_credential)
self.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)
url = self.url
self.client.login(**self.admin_credential)
self.client.post(url, {"comment": "This is new comment by Admin"})
response = self.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")
url = self.url
self.client.login(**self.contributor_credential)
self.client.post(url, {"comment": "This is new comment by Contributor"})
response = self.client.get(url)
self.assertContains(response, "Kontributor")
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")
url = self.url
self.client.get("/logout/")
self.client.login(**self.anonymous_credential)
self.client.post(url, {"comment": "This is new comment by Anonymous"})
response = self.client.get(url)
self.assertContains(response, "Anonim")
def test_detail_materi_contains_form_comment(self):
response = Client.get(self.get_url_for_detail_materi())
response = self.client.get(self.url)
self.assertContains(response, "komentar...")
......
......@@ -69,51 +69,43 @@ class DetailMateri(TemplateView):
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
query_set_for_comment = Comment.objects.filter(materi = context["materi_data"])
context["comment_data"] = query_set_for_comment
return self.render_to_response(context=context)
def get_user_name(self, request):
user_name = ""
if request.user.is_admin:
user_name = "Admin"
elif request.user.is_contributor:
user_name = request.user.name
else:
user_name = "Anonim"
return user_name
def post(self, request, *args, **kwargs):
commentText = request.POST.get("comment", None)
url = request.path
materi = get_object_or_404(Materi, pk=kwargs['pk'])
if commentText == None or commentText== '':
if commentText == None or commentText == "":
context = self.get_context_data(*args, **kwargs)
context["error_message"] = "Anda belum menuliskan komentar"
context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
query_set_for_comment = Comment.objects.filter(materi = context["materi_data"])
context["comment_data"] = query_set_for_comment
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"
else:
username = request.user.name
comment.user = username
comment.save()
materi.comments.add(comment)
materi.save()
return HttpResponseRedirect(url)
else:
comment.user = "Anonim"
comment.save()
materi.comments.add(comment)
materi.save()
return HttpResponseRedirect(url)
materi = get_object_or_404(Materi, pk=kwargs["pk"])
comment = Comment.objects.create(comment=commentText,
user=self.get_user_name(request), materi=materi)
comment.save()
return HttpResponseRedirect(request.path)
def delete_comment(request, pk_materi, pk_comment):
materi = get_object_or_404(Materi, pk=pk_materi)
comment = get_object_or_404(Comment, pk=pk_comment)
url = '/materi/' + str(pk_materi) + "/"
materi.comments.remove(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)
path = materi.content.path
......
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