From 563ffecdac3b7281b0e70eb880efce466f81d0fd Mon Sep 17 00:00:00 2001
From: Saul Andre <saulandreee@gmail.com>
Date: Thu, 23 Apr 2020 12:34:21 +0700
Subject: [PATCH] [GREEN] Add post method for comments in Detail Materi page

---
 app/migrations/0011_auto_20200423_0906.py | 28 +++++++++++++
 app/migrations/0012_auto_20200423_1032.py | 23 ++++++++++
 app/models.py                             |  1 +
 app/templates/app/detail_materi.html      |  8 ++--
 app/templates/app/katalog_materi.html     |  2 +-
 app/tests.py                              | 51 ++++++++++++++++++++++-
 app/views.py                              | 40 ++++++++++++------
 static/css/main.css                       |  2 +-
 8 files changed, 135 insertions(+), 20 deletions(-)
 create mode 100644 app/migrations/0011_auto_20200423_0906.py
 create mode 100644 app/migrations/0012_auto_20200423_1032.py

diff --git a/app/migrations/0011_auto_20200423_0906.py b/app/migrations/0011_auto_20200423_0906.py
new file mode 100644
index 0000000..c75a37d
--- /dev/null
+++ b/app/migrations/0011_auto_20200423_0906.py
@@ -0,0 +1,28 @@
+# 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),
+        ),
+    ]
diff --git a/app/migrations/0012_auto_20200423_1032.py b/app/migrations/0012_auto_20200423_1032.py
new file mode 100644
index 0000000..0137801
--- /dev/null
+++ b/app/migrations/0012_auto_20200423_1032.py
@@ -0,0 +1,23 @@
+# 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),
+        ),
+    ]
diff --git a/app/models.py b/app/models.py
index 856a855..cc3263a 100644
--- a/app/models.py
+++ b/app/models.py
@@ -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)
diff --git a/app/templates/app/detail_materi.html b/app/templates/app/detail_materi.html
index 1b8796e..fa87498 100644
--- a/app/templates/app/detail_materi.html
+++ b/app/templates/app/detail_materi.html
@@ -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>
                         
diff --git a/app/templates/app/katalog_materi.html b/app/templates/app/katalog_materi.html
index 33c447d..928b7a4 100644
--- a/app/templates/app/katalog_materi.html
+++ b/app/templates/app/katalog_materi.html
@@ -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 %}
diff --git a/app/tests.py b/app/tests.py
index e973b8d..c733396 100644
--- a/app/tests.py
+++ b/app/tests.py
@@ -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"
diff --git a/app/views.py b/app/views.py
index 907747e..9b7466c 100644
--- a/app/views.py
+++ b/app/views.py
@@ -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 = {}
diff --git a/static/css/main.css b/static/css/main.css
index 5b6cb54..9c391d6 100644
--- a/static/css/main.css
+++ b/static/css/main.css
@@ -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 {
-- 
GitLab