commit 7ff09c86b5206bd0e0495621829f7002c4686549
parent d3d3fd527e258bca8a83b7c16f395d68e429b520
Author: knutsen <samuel@knutsen.co>
Date:   Tue, 10 Aug 2021 09:26:06 +0100
Filter out duplicate comments.
Diffstat:
2 files changed, 21 insertions(+), 13 deletions(-)
diff --git a/shiimoe.py b/shiimoe.py
@@ -76,14 +76,16 @@ def home():
 
 def render_guestbook(**kw):
     comments = read_comments()
+    comments.reverse()
 
-    kw['comments'] = reversed(comments)
     for comment in comments:
         comment['comment'] = bleach.clean(comment['comment'],
             tags=ALLOWED_TAGS,
             attributes=ALLOWED_ATTRIBUTES,
             protocols=ALLOWED_PROTOCOLS)
         comment['comment'] = md.convert(comment['comment'])
+
+    kw['comments'] = comments
     return render_template('guestbook.html', **kw)
 
 @app.route('/guestbook')
@@ -100,11 +102,11 @@ def postcomment():
     name = request.form.get('name')
     comment = request.form.get('comment')
     date = request.form.get('date') or datetime.strftime(now, DATE_FORMAT)
-    ip = (request.environ.get('HTTP_X_REAL_IP') or
-          request.environ.get('REMOTE_ADDR')    or
-          request.remote_addr)
+    ip = (request.environ.get('HTTP_X_REAL_IP')
+       or request.environ.get('REMOTE_ADDR')
+       or request.remote_addr)
 
-    err = lambda msg: render_guestbook(error_msg=msg)
+    err = lambda msg: render_guestbook(error_msg=msg, dcomment=comment, dname=name)
 
     # Comment or name left empty, send error message.
     if not (name or "").strip() or not (comment or "").strip():
@@ -121,6 +123,9 @@ def postcomment():
         return err("No links!")
 
     comments = read_comments()
+    if any(c['comment'] == comment for c in comments):
+        return err("Duplicate comment. Please write unique comments.")
+
     comments.append({
         'name': name,
         'comment': comment,
diff --git a/templates/guestbook.html b/templates/guestbook.html
@@ -85,9 +85,11 @@
         .error {
             margin: 1em 0 2em 0;
             padding: 1em 1.5em;
-            border: 2pt solid red;
-            border-radius: 7pt;
-            background: #f464;
+            border: 2pt solid #ff6b0091;
+            border-radius: 10pt;
+            border-top-left-radius: 0;
+            background: #ce285bad;
+            color: white;
         }
     </style>
 
@@ -114,11 +116,14 @@
     {% endif %}
 
     <form action="/postcomment" method="POST" name="commentBox">
-        <input class="name-input input" name="name" placeholder="Your name" required>
+        <input class="name-input input"
+               name="name" placeholder="Your name"
+               {% if dname is defined %} value="{{ dname }}" {% endif %}
+               required>
         <div class="textbox">
         <textarea class="comment-input input" name="comment"
                   placeholder="# Your comment

(no links, ≤850 characters)"
-                  required></textarea>
+				  required>{{ dcomment }}</textarea>
         <i class="fab fa-markdown"></i>
         </div>
         <input type="submit" value="Post">
@@ -155,10 +160,8 @@
             req.onload = event => {
                 document.write(req.response);
             };
-            req.send(formData);
 
-			req.addEventListener('load',
-				window.location.reload.bind(window.location));
+            req.send(formData);
         }, false);
     </script>