From 6fd9c699e656eda0fb7b2e977addb2ed20baa7e4 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Jan 16 2018 05:53:17 +0000 Subject: make members widget match the mockup --- diff --git a/hubs/app.py b/hubs/app.py index af16621..a596e63 100644 --- a/hubs/app.py +++ b/hubs/app.py @@ -115,6 +115,7 @@ def check_auth(): user = hubs.models.User.get_or_create( username=flask.session["auth"]["nickname"], fullname=flask.session["auth"]["fullname"]) + user.last_active = datetime.datetime.utcnow() flask.g.user = user else: flask.g.auth = munch.Munch(logged_in=False) diff --git a/hubs/migrations/versions/a245837dd23c_add_user_last_activity_field.py b/hubs/migrations/versions/a245837dd23c_add_user_last_activity_field.py new file mode 100644 index 0000000..2f03f3e --- /dev/null +++ b/hubs/migrations/versions/a245837dd23c_add_user_last_activity_field.py @@ -0,0 +1,45 @@ +# This Alembic database migration is part of the Fedora Hubs project. +# Copyright (C) 2018 The Fedora Project +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +""" +add user last activity field + +Revision ID: a245837dd23c +Revises: 20b23e867aeb +Create Date: 2018-01-05 07:32:29.858380 +""" + +from __future__ import absolute_import, unicode_literals + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a245837dd23c' +down_revision = u'20b23e867aeb' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('users', sa.Column('last_active', + sa.DateTime(), + nullable=True)) + + +def downgrade(): + with op.batch_alter_table("users") as batch_op: + batch_op.drop_column('last_active') diff --git a/hubs/models/user.py b/hubs/models/user.py index 8536167..ffb5a9e 100644 --- a/hubs/models/user.py +++ b/hubs/models/user.py @@ -49,6 +49,7 @@ class User(BASE): saved_notifications = relation( 'SavedNotification', backref='user', lazy='dynamic', order_by=SavedNotification.created.desc()) + last_active = sa.Column(sa.DateTime, default=datetime.datetime.utcnow) def __json__(self): return { @@ -56,6 +57,8 @@ class User(BASE): 'avatar': username2avatar(self.username), 'fullname': self.fullname, 'created_on': self.created_on, + 'last_active': self.last_active, + # We'll need hubs subscribed to, owned, etc.. # 'hubs': [hub.idx for hub in self.hubx], } diff --git a/hubs/utils/__init__.py b/hubs/utils/__init__.py index dfa6d15..f825d40 100755 --- a/hubs/utils/__init__.py +++ b/hubs/utils/__init__.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import os from hashlib import sha256, md5 +import humanize import fedmsg.config import fedmsg.meta @@ -46,3 +47,7 @@ def hub2groupavatar(hub): "%s") % (hubname2monogramcolour(hub.name), hubname2monogramcolour(hub.name), hub.name[0].upper()) + + +def relative_time(date): + return humanize.naturaltime(date) diff --git a/hubs/widgets/base.py b/hubs/widgets/base.py index ad56e9b..cb8990d 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 +from hubs.utils import hub2groupavatar, relative_time from .caching import CachedFunction from .view import WidgetView @@ -142,6 +142,7 @@ class Widget(object): ) env.filters['tojson'] = flask.json.tojson_filter env.filters['hub2groupavatar'] = hub2groupavatar + env.filters['relative_time'] = relative_time env.globals.update({ 'session': flask.app.session, 'g': flask.g, diff --git a/hubs/widgets/memberships/__init__.py b/hubs/widgets/memberships/__init__.py index b83a7fa..1731849 100644 --- a/hubs/widgets/memberships/__init__.py +++ b/hubs/widgets/memberships/__init__.py @@ -1,10 +1,12 @@ from __future__ import unicode_literals +from datetime import datetime + from hubs.widgets.base import Widget from hubs.widgets.view import RootWidgetView -ELLIPSIS_LIMIT = 3 +ELLIPSIS_LIMIT = 5 """ This widget displays who is a member of the Hub it is placed on. @@ -16,6 +18,7 @@ class Memberships(Widget): name = "memberships" label = "Members" position = "both" + hub_types = ['team'] class BaseView(RootWidgetView): @@ -35,7 +38,10 @@ class BaseView(RootWidgetView): owners.append(member.username) oldest_members = sorted( - members, key=lambda m: m.get('created_on'))[:ELLIPSIS_LIMIT] + members, + key=lambda m: m['last_active'] or datetime.utcfromtimestamp(0), + reverse=True)[:ELLIPSIS_LIMIT] + return dict( memberships=list(members), oldest_members=list(oldest_members), diff --git a/hubs/widgets/memberships/templates/root.html b/hubs/widgets/memberships/templates/root.html index a2e3b89..90b05fa 100644 --- a/hubs/widgets/memberships/templates/root.html +++ b/hubs/widgets/memberships/templates/root.html @@ -1,32 +1,24 @@ -
- {% if memberships|length > oldest_members|length %} - {% for member in oldest_members %} -
- Hub avatar for {{ member.name }} - {{ member.username }} - {% if member.username in owners %} -

Owner

- {% else %} -

Member

- {% endif %} -
- {% endfor %} - View All - {% else %} - {% for member in memberships %} -
- Hub avatar for {{ member.name }} - {{ member }} - {% if member.username in owners %} -

Owner

- {% else %} -

Member

+ + {% if memberships|length > oldest_members|length %} + View All {{memberships|length}} Members {% endif %} -
- {% endfor %} - {%endif%} -
+{% if memberships|length > oldest_members|length %}