From b183c7f614181ee5c1cc3ca7dde2b603ab5e7d42 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Feb 08 2018 10:05:27 +0000 Subject: abstract out the humainze filter used in widgets --- diff --git a/fedora-hubs.spec b/fedora-hubs.spec index 8adda09..7a4504c 100644 --- a/fedora-hubs.spec +++ b/fedora-hubs.spec @@ -34,7 +34,6 @@ BuildRequires: python3-fedora BuildRequires: python3-flask BuildRequires: python3-flask-oidc BuildRequires: python3-html5lib -BuildRequires: python3-humanize BuildRequires: python3-iso3166 BuildRequires: python3-markdown BuildRequires: python3-munch diff --git a/hubs/utils/__init__.py b/hubs/utils/__init__.py index 7db4aff..2e7cb93 100755 --- a/hubs/utils/__init__.py +++ b/hubs/utils/__init__.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals from hashlib import sha256, md5 -import humanize +import arrow from six.moves.urllib_parse import urlencode @@ -29,5 +29,5 @@ def hub2groupavatar(hub): hub.name[0].upper()) -def relative_time(date): - return humanize.naturaltime(date) +def humanize(timestamp): + return arrow.get(timestamp).humanize() diff --git a/hubs/utils/github.py b/hubs/utils/github.py index 40bebcd..3096ca3 100644 --- a/hubs/utils/github.py +++ b/hubs/utils/github.py @@ -54,7 +54,7 @@ def github_pulls(repo, token=None): pr_assignee=result['assignee'], pr_repo=repo, pr_hosting_type="Github", - pr_opened_date=arrow.get(result['created_at']), + pr_opened_date=arrow.get(result['created_at']).timestamp, ) diff --git a/hubs/utils/hyperkitty.py b/hubs/utils/hyperkitty.py index 3991a05..1e250e5 100644 --- a/hubs/utils/hyperkitty.py +++ b/hubs/utils/hyperkitty.py @@ -22,5 +22,5 @@ def last_ten_threads(listname): thread_subject=subject, thread_replies=thread["replies_count"], thread_url=url, - thread_active_date=arrow.get(thread["date_active"]) + thread_active_date=arrow.get(thread["date_active"]).timestamp ) diff --git a/hubs/widgets/badges/__init__.py b/hubs/widgets/badges/__init__.py index dba5f7f..344048b 100644 --- a/hubs/widgets/badges/__init__.py +++ b/hubs/widgets/badges/__init__.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals import requests -import arrow from collections import defaultdict, OrderedDict from hubs.widgets.base import Widget @@ -19,12 +18,6 @@ class Badges(Widget): "team": "Not shown on team hub" } - def get_template_environment(self): - env = super(Badges, self).get_template_environment() - # Add a filter - env.filters['humanize'] = lambda d: arrow.get(d).humanize() - return env - class BaseView(RootWidgetView): diff --git a/hubs/widgets/base.py b/hubs/widgets/base.py index 509853c..d7444f9 100644 --- a/hubs/widgets/base.py +++ b/hubs/widgets/base.py @@ -8,7 +8,7 @@ import six from importlib import import_module from hubs.models.constants import HUB_TYPES -from hubs.utils import hub2groupavatar, relative_time +from hubs.utils import hub2groupavatar, humanize from .caching import CachedFunction from .view import WidgetView @@ -143,7 +143,7 @@ class Widget(object): ) env.filters['tojson'] = flask.json.tojson_filter env.filters['hub2groupavatar'] = hub2groupavatar - env.filters['relative_time'] = relative_time + env.filters['humanize'] = humanize env.globals.update({ 'session': flask.app.session, 'g': flask.g, diff --git a/hubs/widgets/halp/__init__.py b/hubs/widgets/halp/__init__.py index f48926e..d61900a 100644 --- a/hubs/widgets/halp/__init__.py +++ b/hubs/widgets/halp/__init__.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals -import arrow import flask from hubs.utils import validators @@ -42,11 +41,6 @@ class Halp(Widget): is_large = True hub_types = ['team', 'stream'] - def get_template_environment(self): - env = super(Halp, self).get_template_environment() - env.filters['humanize'] = lambda d: arrow.get(d).humanize() - return env - def register_routes(self, app): super(Halp, self).register_routes(app) # Add the hubs suggestion view diff --git a/hubs/widgets/mailinglist/__init__.py b/hubs/widgets/mailinglist/__init__.py index d3b9b7c..faae5a6 100644 --- a/hubs/widgets/mailinglist/__init__.py +++ b/hubs/widgets/mailinglist/__init__.py @@ -22,12 +22,6 @@ class MailingList(Widget): "mailing list"), } - def get_template_environment(self): - env = super(MailingList, self).get_template_environment() - # Add a filter - env.filters['humanize'] = lambda d: d.humanize() - return env - class BaseView(RootWidgetView): diff --git a/hubs/widgets/meetings/__init__.py b/hubs/widgets/meetings/__init__.py index 6847df3..77cecc0 100644 --- a/hubs/widgets/meetings/__init__.py +++ b/hubs/widgets/meetings/__init__.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals -import arrow import collections import datetime import requests @@ -34,12 +33,6 @@ class Meetings(Widget): "team": "Show upcoming meetings for this hub ", } - def get_template_environment(self): - env = super(Meetings, self).get_template_environment() - # Add a filter - env.filters['humanize'] = lambda d: arrow.get(d).humanize() - return env - class BaseView(RootWidgetView): diff --git a/hubs/widgets/memberships/__init__.py b/hubs/widgets/memberships/__init__.py index 1731849..936fb42 100644 --- a/hubs/widgets/memberships/__init__.py +++ b/hubs/widgets/memberships/__init__.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from datetime import datetime +import arrow from hubs.widgets.base import Widget from hubs.widgets.view import RootWidgetView @@ -36,10 +36,9 @@ class BaseView(RootWidgetView): members.append(member.__json__()) if member in hub.owners: owners.append(member.username) - oldest_members = sorted( members, - key=lambda m: m['last_active'] or datetime.utcfromtimestamp(0), + key=lambda m: m['last_active'] or arrow.get(0).timestamp, reverse=True)[:ELLIPSIS_LIMIT] return dict( diff --git a/hubs/widgets/memberships/templates/root.html b/hubs/widgets/memberships/templates/root.html index 90b05fa..1015e8d 100644 --- a/hubs/widgets/memberships/templates/root.html +++ b/hubs/widgets/memberships/templates/root.html @@ -6,7 +6,7 @@
{{ member.username }}
{% if member.last_active %} -
last active {{ member.last_active | relative_time }}
+
last active {{ member.last_active | humanize }}
{% endif %}
@@ -35,7 +35,7 @@ Hub avatar for {{ member.name }}
{{ member.username }}
-
last active {{ member.last_active | relative_time }}
+
last active {{ member.last_active | humanize }}
diff --git a/hubs/widgets/pullrequests/__init__.py b/hubs/widgets/pullrequests/__init__.py index 9373eae..929f870 100644 --- a/hubs/widgets/pullrequests/__init__.py +++ b/hubs/widgets/pullrequests/__init__.py @@ -17,12 +17,6 @@ class PullRequests(Widget): position = "right" hub_types = ["team"] - def get_template_environment(self): - env = super(PullRequests, self).get_template_environment() - # Add a filter - env.filters['humanize'] = lambda d: d.humanize() - return env - class BaseView(RootWidgetView): diff --git a/requirements.txt b/requirements.txt index 431b378..cc9e29e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,6 @@ python-fedora flask flask-oidc>=1.0.3 html5lib -humanize iso3166 markdown munch