More poll/question bugfixes

This commit is contained in:
Thomas Sileo 2019-07-12 22:03:04 +02:00
parent 5b76fe65aa
commit f5f4e7f9df
3 changed files with 28 additions and 11 deletions

19
app.py
View file

@ -23,7 +23,6 @@ import mf2py
import requests import requests
import timeago import timeago
from bson.objectid import ObjectId from bson.objectid import ObjectId
from dateutil import parser
from flask import Flask from flask import Flask
from flask import Response from flask import Response
from flask import abort from flask import abort
@ -87,6 +86,7 @@ from config import VERSION_DATE
from config import _drop_db from config import _drop_db
from poussetaches import PousseTaches from poussetaches import PousseTaches
from tasks import Tasks from tasks import Tasks
from utils import parse_datetime
from utils import opengraph from utils import opengraph
from utils.key import get_secret_key from utils.key import get_secret_key
from utils.lookup import lookup from utils.lookup import lookup
@ -262,7 +262,7 @@ def gtone(n):
@app.template_filter() @app.template_filter()
def gtnow(dtstr): def gtnow(dtstr):
return format_datetime(datetime.now().astimezone()) > dtstr return format_datetime(datetime.now(timezone.utc)) > dtstr
@app.template_filter() @app.template_filter()
@ -381,7 +381,7 @@ def get_actor(url):
@app.template_filter() @app.template_filter()
def format_time(val): def format_time(val):
if val: if val:
dt = parser.parse(val) dt = parse_datetime(val)
return datetime.strftime(dt, "%B %d, %Y, %H:%M %p") return datetime.strftime(dt, "%B %d, %Y, %H:%M %p")
return val return val
@ -399,7 +399,7 @@ def gt_ts(val):
@app.template_filter() @app.template_filter()
def format_timeago(val): def format_timeago(val):
if val: if val:
dt = parser.parse(val) dt = parse_datetime(val)
return timeago.format(dt.astimezone(timezone.utc), datetime.now(timezone.utc)) return timeago.format(dt.astimezone(timezone.utc), datetime.now(timezone.utc))
return val return val
@ -1195,7 +1195,7 @@ def _add_answers_to_question(raw_doc: Dict[str, Any]) -> None:
.get("question_answers", {}) .get("question_answers", {})
.get(_answer_key(choice["name"]), 0), .get(_answer_key(choice["name"]), 0),
} }
now = datetime.now().astimezone() now = datetime.now(timezone.utc)
if format_datetime(now) >= activity["object"]["endTime"]: if format_datetime(now) >= activity["object"]["endTime"]:
activity["object"]["closed"] = 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")) open_for = int(_user_api_arg("open_for"))
choices = { choices = {
"endTime": ap.format_datetime( "endTime": ap.format_datetime(
datetime.now().astimezone() + timedelta(minutes=open_for) datetime.now(timezone.utc) + timedelta(minutes=open_for)
) )
} }
of = _user_api_arg("of") of = _user_api_arg("of")
@ -2923,8 +2923,11 @@ def task_process_new_activity():
in_reply_to = note.get_in_reply_to() 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 # 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 # answer
if (not in_reply_to or in_reply_to.startswith(ID)) and not note.has_type( # FIXME(tsileo): this will block "regular replies" to a Poll, maybe the adressing will help make the
ap.ActivityType.QUESTION # difference?
if not in_reply_to or (
in_reply_to.startswith(ID)
and not note.has_type(ap.ActivityType.QUESTION)
): ):
tag_stream = True tag_stream = True

View file

@ -2,8 +2,7 @@ import os
from datetime import datetime from datetime import datetime
from datetime import timezone from datetime import timezone
from dateutil import parser from utils import parse_datetime
from poussetaches import PousseTaches from poussetaches import PousseTaches
p = PousseTaches( p = PousseTaches(
@ -61,7 +60,7 @@ class Tasks:
@staticmethod @staticmethod
def fetch_remote_question(question) -> None: def fetch_remote_question(question) -> None:
now = datetime.now(timezone.utc) 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) minutes = int((dt - now).total_seconds() / 60)
if minutes > 0: if minutes > 0:

View file

@ -1,4 +1,8 @@
import logging import logging
from datetime import datetime
from datetime import timezone
from dateutil import parser
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -10,3 +14,14 @@ def strtobool(s: str) -> bool:
return False return False
raise ValueError(f"cannot convert {s} to bool") 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