From f5f4e7f9dff956f127866e66f2d0b1fc9f3122b1 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 12 Jul 2019 22:03:04 +0200 Subject: [PATCH] More poll/question bugfixes --- app.py | 19 +++++++++++-------- tasks.py | 5 ++--- utils/__init__.py | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 886eda9..bd0cbce 100644 --- a/app.py +++ b/app.py @@ -23,7 +23,6 @@ import mf2py import requests import timeago from bson.objectid import ObjectId -from dateutil import parser from flask import Flask from flask import Response from flask import abort @@ -87,6 +86,7 @@ from config import VERSION_DATE from config import _drop_db from poussetaches import PousseTaches from tasks import Tasks +from utils import parse_datetime from utils import opengraph from utils.key import get_secret_key from utils.lookup import lookup @@ -262,7 +262,7 @@ def gtone(n): @app.template_filter() def gtnow(dtstr): - return format_datetime(datetime.now().astimezone()) > dtstr + return format_datetime(datetime.now(timezone.utc)) > dtstr @app.template_filter() @@ -381,7 +381,7 @@ def get_actor(url): @app.template_filter() def format_time(val): if val: - dt = parser.parse(val) + dt = parse_datetime(val) return datetime.strftime(dt, "%B %d, %Y, %H:%M %p") return val @@ -399,7 +399,7 @@ def gt_ts(val): @app.template_filter() def format_timeago(val): if val: - dt = parser.parse(val) + dt = parse_datetime(val) return timeago.format(dt.astimezone(timezone.utc), datetime.now(timezone.utc)) return val @@ -1195,7 +1195,7 @@ def _add_answers_to_question(raw_doc: Dict[str, Any]) -> None: .get("question_answers", {}) .get(_answer_key(choice["name"]), 0), } - now = datetime.now().astimezone() + now = datetime.now(timezone.utc) if format_datetime(now) >= activity["object"]["endTime"]: activity["object"]["closed"] = activity["object"]["endTime"] @@ -2065,7 +2065,7 @@ def api_new_question(): open_for = int(_user_api_arg("open_for")) choices = { "endTime": ap.format_datetime( - datetime.now().astimezone() + timedelta(minutes=open_for) + datetime.now(timezone.utc) + timedelta(minutes=open_for) ) } of = _user_api_arg("of") @@ -2923,8 +2923,11 @@ def task_process_new_activity(): in_reply_to = note.get_in_reply_to() # Make the note part of the stream if it's not a reply, or if it's a local reply **and** it's not a poll # answer - if (not in_reply_to or in_reply_to.startswith(ID)) and not note.has_type( - ap.ActivityType.QUESTION + # FIXME(tsileo): this will block "regular replies" to a Poll, maybe the adressing will help make the + # difference? + if not in_reply_to or ( + in_reply_to.startswith(ID) + and not note.has_type(ap.ActivityType.QUESTION) ): tag_stream = True diff --git a/tasks.py b/tasks.py index c2020b2..9855f64 100644 --- a/tasks.py +++ b/tasks.py @@ -2,8 +2,7 @@ import os from datetime import datetime from datetime import timezone -from dateutil import parser - +from utils import parse_datetime from poussetaches import PousseTaches p = PousseTaches( @@ -61,7 +60,7 @@ class Tasks: @staticmethod def fetch_remote_question(question) -> None: now = datetime.now(timezone.utc) - dt = parser.parse(question.closed or question.endTime).astimezone(timezone.utc) + dt = parse_datetime(question.closed or question.endTime) minutes = int((dt - now).total_seconds() / 60) if minutes > 0: diff --git a/utils/__init__.py b/utils/__init__.py index cdf368d..97d057c 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,4 +1,8 @@ import logging +from datetime import datetime +from datetime import timezone + +from dateutil import parser logger = logging.getLogger(__name__) @@ -10,3 +14,14 @@ def strtobool(s: str) -> bool: return False raise ValueError(f"cannot convert {s} to bool") + + +def parse_datetime(s :str) -> datetime: + # Parses the datetime with dateutil + dt = parser.parse(s) + + # If no TZ is set, assumes it's UTC + if not dt.tzinfo: + dt = dt.replace(tzinfo=timezone.utc) + + return dt