From 5eaab761b37f6e880ccc6e743675d7e2ad59c1e4 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 10 Jun 2021 00:20:09 +0000 Subject: delete post template, fuzziness for timestamps etc --- apioforum/__init__.py | 3 +++ apioforum/fuzzy.py | 35 +++++++++++++++++++++++++++++++++++ apioforum/templates/common.html | 9 ++++++++- apioforum/templates/delete_post.html | 17 +++++++++++++++++ apioforum/thread.py | 3 ++- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 apioforum/fuzzy.py create mode 100644 apioforum/templates/delete_post.html 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 @@
- {{post.author}} {{post.created}} + {{post.author}} {{ts(post.created)}} + {% if post.edited %} + (edited {{ts(post.updated)}}) + {% endif %} {% if buttons and post.author == g.user %} @@ -23,3 +26,7 @@
{% endmacro %} + +{% macro ts(dt) -%} + +{%- 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 %} +

{% block title %}deleting post{% endblock %} +{% endblock %} + +{% block content %} +{% call disp_post(post, False) %} +{{ rendered_content | safe }} +{% endcall %} + +
+

confirm delete?

+ +cancel +
+{% 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'])) -- cgit v1.2.3