summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--apioforum/__init__.py3
-rw-r--r--apioforum/fuzzy.py35
-rw-r--r--apioforum/templates/common.html9
-rw-r--r--apioforum/templates/delete_post.html17
-rw-r--r--apioforum/thread.py3
5 files changed, 65 insertions, 2 deletions
diff --git a/apioforum/__init__.py b/apioforum/__init__.py
index e4bea05..41614f0 100644
--- a/apioforum/__init__.py
+++ b/apioforum/__init__.py
@@ -29,4 +29,7 @@ def create_app():
from . import thread
app.register_blueprint(thread.bp)
+ from .fuzzy import fuzzy
+ app.jinja_env.filters['fuzzy']=fuzzy
+
return app
diff --git a/apioforum/fuzzy.py b/apioforum/fuzzy.py
new file mode 100644
index 0000000..58f8c6b
--- /dev/null
+++ b/apioforum/fuzzy.py
@@ -0,0 +1,35 @@
+# fuzzy datetime things
+
+times = (
+ ("year","years",365*24*60*60), # leap years aren't real
+ ("day","days",24*60*60),
+ ("hour","hours",60*60),
+ ("minute","minutes",60),
+ ("second","seconds",1),
+)
+
+from datetime import datetime, timedelta
+
+def fuzzy(seconds,ago=True):
+ if isinstance(seconds,timedelta):
+ seconds = seconds.total_seconds()
+ elif isinstance(seconds,datetime):
+ seconds = (seconds-datetime.now()).total_seconds()
+
+ fmt = "{}"
+ if ago:
+ fmt = "in {}" if seconds > 0 else "{} ago"
+ seconds = abs(seconds)
+ for t in times:
+ if seconds >= t[2]:
+ rounded = round((seconds / t[2])*100)/100
+ if int(rounded) == rounded:
+ rounded = int(rounded)
+ if rounded == 1:
+ word = t[0]
+ else:
+ word = t[1]
+ return fmt.format(f'{rounded} {word}')
+ else:
+ return "now"
+
diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html
index d4178d7..c5d1c26 100644
--- a/apioforum/templates/common.html
+++ b/apioforum/templates/common.html
@@ -2,7 +2,10 @@
<div class="post">
<div class="post-heading">
<span class="post-heading-a">
- <span class="post-heading-em">{{post.author}}</span> {{post.created}}
+ <span class="post-heading-em">{{post.author}}</span> {{ts(post.created)}}
+ {% if post.edited %}
+ (edited {{ts(post.updated)}})
+ {% endif %}
</span>
<span class="post-heading-b">
{% if buttons and post.author == g.user %}
@@ -23,3 +26,7 @@
</div>
</div>
{% endmacro %}
+
+{% macro ts(dt) -%}
+<time title="{{dt.isoformat(' ')}}" datetime="{{dt.isoformat(' ')}}">{{dt | fuzzy}}</time>
+{%- endmacro %}
diff --git a/apioforum/templates/delete_post.html b/apioforum/templates/delete_post.html
new file mode 100644
index 0000000..6f99704
--- /dev/null
+++ b/apioforum/templates/delete_post.html
@@ -0,0 +1,17 @@
+{% from 'common.html' import disp_post %}
+{% extends 'base.html' %}
+{% block header %}
+<h1>{% block title %}deleting post{% endblock %}</1h>
+{% endblock %}
+
+{% block content %}
+{% call disp_post(post, False) %}
+{{ rendered_content | safe }}
+{% endcall %}
+
+<form method="post">
+<p>confirm delete?</p>
+<input type="submit" value="delete">
+<a href="{{url_for('thread.view_thread',thread_id=post.thread)}}">cancel</a>
+</form>
+{% endblock %}
diff --git a/apioforum/thread.py b/apioforum/thread.py
index d64055a..b9697ce 100644
--- a/apioforum/thread.py
+++ b/apioforum/thread.py
@@ -96,7 +96,8 @@ def edit_post(post_id):
print(err)
if err is None:
print("a")
- db.execute("UPDATE posts SET content = ? WHERE id = ?",(newcontent,post_id))
+ db.execute(
+ "UPDATE posts SET content = ?, edited = 1, updated = current_timestamp WHERE id = ?",(newcontent,post_id))
db.commit()
flash("post edited editiously")
return redirect(url_for("thread.view_thread",thread_id=post['thread']))