diff options
-rw-r--r-- | _pytest/test_unfurl.py | 34 | ||||
-rw-r--r-- | wee_slack.py | 31 |
2 files changed, 62 insertions, 3 deletions
diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py index 9523bc6..3545332 100644 --- a/_pytest/test_unfurl.py +++ b/_pytest/test_unfurl.py @@ -1,7 +1,8 @@ from __future__ import print_function, unicode_literals -import wee_slack +from datetime import datetime, timedelta import pytest +import wee_slack @pytest.mark.parametrize('case', ( @@ -103,8 +104,35 @@ import pytest 'output': "@othersubteam This is announcement for the dev team" }, { - 'input': "Ends <!date^1584573568^{date_short} at {time}|Mar 18, 2020 at 23:19 PM>.", - 'output': "Ends Mar 18, 2020 at 23:19 PM." + 'input': "Ends <!date^1584573568^{date_num} - {date} - {date_short} - {date_long} at {time} - {time_secs}|Mar 18, 2020 at 23:19 PM>.", + 'output': "Ends 2020-03-19 - March 19, 2020 - Mar 19, 2020 - Thursday, March 19, 2020 at 00:19 - 00:19:28." + }, + { + 'input': "Ends <!date^1584573568^{date_num} {invalid_token}>.", + 'output': "Ends 2020-03-19 {invalid_token}." + }, + { + 'input': "Ends <!date^1584573568^{date_num}^http://github.com>.", + 'output': "Ends 2020-03-19 (http://github.com)." + }, + { + 'input': "Ends <!date^{}^{{date_pretty}} - {{date_short_pretty}} - {{date_long_pretty}}>.".format( + int((datetime.today()).timestamp())), + 'output': "Ends today - today - today." + }, + { + 'input': "Ends <!date^{}^{{date_pretty}} - {{date_short_pretty}} - {{date_long_pretty}}>.".format( + int((datetime.today() - timedelta(days=1)).timestamp())), + 'output': "Ends yesterday - yesterday - yesterday." + }, + { + 'input': "Ends <!date^{}^{{date_pretty}} - {{date_short_pretty}} - {{date_long_pretty}}>.".format( + int((datetime.today() + timedelta(days=1)).timestamp())), + 'output': "Ends tomorrow - tomorrow - tomorrow." + }, + { + 'input': "Ends <!date^1577833200^{date_pretty} - {date_short_pretty} - {date_long_pretty}>.", + 'output': "Ends January 01, 2020 - Jan 01, 2020 - Wednesday, January 01, 2020." } )) def test_unfurl_refs(case, realish_eventrouter): diff --git a/wee_slack.py b/wee_slack.py index 9370740..338c23a 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -6,6 +6,7 @@ from __future__ import print_function, unicode_literals from collections import OrderedDict +from datetime import date, datetime, timedelta from functools import wraps from io import StringIO from itertools import chain, count, islice @@ -3562,6 +3563,36 @@ def resolve_ref(ref): subteam = team.subteams.get(subteam_id) if subteam: return subteam.handle + elif ref.startswith("!date"): + parts = ref.split('^') + ref_datetime = datetime.fromtimestamp(int(parts[1])) + link_suffix = ' ({})'.format(parts[3]) if len(parts) > 3 else '' + token_to_format = { + 'date_num': '%Y-%m-%d', + 'date': '%B %d, %Y', + 'date_short': '%b %d, %Y', + 'date_long': '%A, %B %d, %Y', + 'time': '%H:%M', + 'time_secs': '%H:%M:%S' + } + + def replace_token(match): + token = match.group(1) + if token.startswith('date_') and token.endswith('_pretty'): + if ref_datetime.date() == date.today(): + return 'today' + elif ref_datetime.date() == date.today() - timedelta(days=1): + return 'yesterday' + elif ref_datetime.date() == date.today() + timedelta(days=1): + return 'tomorrow' + else: + token = token.replace('_pretty', '') + if token in token_to_format: + return ref_datetime.strftime(token_to_format[token]) + else: + return match.group(0) + + return re.sub(r"{([^}]+)}", replace_token, parts[2]) + link_suffix # Something else, just return as-is return ref |