From 13dede8ec5c912a245a59e301739bb725f5c96be Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Dec 07 2017 10:07:41 +0000 Subject: Make stars work This makes the stars add to the side bookmarks toolbar. Also, did some tweaking of how the bookmarks bar is generated and displayed. (in react only) Fixes #475 Signed-off-by: Ryan Lerch --- diff --git a/hubs/models.py b/hubs/models.py index 023db77..80c23bf 100644 --- a/hubs/models.py +++ b/hubs/models.py @@ -537,14 +537,34 @@ class User(BASE): @property def bookmarks(self): - # TODO -- someday make this editable/configurable. - return sorted(list(set([ - assoc.hub for assoc in self.associations - if assoc.role == 'member' or - assoc.role == 'subscriber' or - assoc.role == 'owner' and - assoc.hub.name != self.username - ])), key=operator.attrgetter('name')) + bookmarks = { + "starred": [], + "memberships": [], + "subscriptions": [], + } + starred_hubs = self.starred_hubs + memberships = self.memberships + for assoc in self.associations: + if assoc.hub.name == self.username: + continue + + if assoc.role == "stargazer": + bookmarks["starred"].append(assoc.hub) + + if ((assoc.role == "member" or assoc.role == "owner") + and assoc.hub not in starred_hubs): + bookmarks["memberships"].append(assoc.hub) + + if (assoc.role == "subscriber" + and assoc.hub not in starred_hubs + and assoc.hub not in memberships): + bookmarks["subscriptions"].append(assoc.hub) + + bookmarks = dict( + (key, sorted(list(set(values)), key=operator.attrgetter('name'))) + for key, values in bookmarks.items() + ) + return bookmarks @classmethod def by_username(cls, username): diff --git a/hubs/static/client/app/components/LeftMenu.js b/hubs/static/client/app/components/LeftMenu.js index 26ff2f5..521e25c 100644 --- a/hubs/static/client/app/components/LeftMenu.js +++ b/hubs/static/client/app/components/LeftMenu.js @@ -7,9 +7,8 @@ import "./LeftMenu.css"; class LeftMenu extends React.Component { render() { - let userEntries = []; - if (this.props.user.logged_in) { - userEntries = [ + let leftMenuEntries = []; + leftMenuEntries.push( ( - ), - ...this.props.user.bookmarks.map((entry) => ( + ), ( + + ) + ); + if (this.props.user.starred_hubs.length > 0){ + leftMenuEntries.push( + + ); + } + leftMenuEntries.push( + ...this.props.user.starred_hubs.map((entry) => ( - )) - ]; - } + )) + ) + if (this.props.user.memberships.length > 0){ + leftMenuEntries.push( + + ); + } + leftMenuEntries.push( + ...this.props.user.memberships.map((entry) => ( + + )) + ) + if (this.props.user.subscriptions.length > 0){ + leftMenuEntries.push( + + ); + } + leftMenuEntries.push( + ...this.props.user.subscriptions.map((entry) => ( + + )) + ) return (
    - {userEntries} - + {leftMenuEntries}
); @@ -93,6 +144,7 @@ class LeftMenuEntry extends React.Component { LeftMenuEntry.propTypes = { url: PropTypes.string.isRequired, + user_hub: PropTypes.bool, icon: PropTypes.string.isRequired, text: PropTypes.string.isRequired, cssClass: PropTypes.string, @@ -100,3 +152,17 @@ LeftMenuEntry.propTypes = { LeftMenuEntry.defaultProps = { cssClass: null, } + +class LeftMenuHeader extends React.Component { + render() { + return ( +
  • + {this.props.text} +
  • + ); + } + } + +LeftMenuHeader.propTypes = { + text: PropTypes.string.isRequired, +} diff --git a/hubs/templates/master.html b/hubs/templates/master.html index 5165328..206293b 100644 --- a/hubs/templates/master.html +++ b/hubs/templates/master.html @@ -110,8 +110,8 @@
    + {% if g.auth.logged_in %} + {% endif %}
    diff --git a/hubs/tests/test_models.py b/hubs/tests/test_models.py index 9886739..4ad6b1c 100644 --- a/hubs/tests/test_models.py +++ b/hubs/tests/test_models.py @@ -247,3 +247,58 @@ class ModelTest(hubs.tests.APPTest): hub.unsubscribe(ralph, role="owner") self.assertNotIn(ralph, hub.owners) self.assertIn(ralph, hub.members) + + +class ModelBookmarksTest(hubs.tests.APPTest): + + def _add_assoc(self, hubname, username, role): + hub = hubs.models.Hub.query.get(hubname) + user = hubs.models.User.query.get(username) + self.session.add(hubs.models.Association( + hub=hub, user=user, role=role)) + + def test_user_bookmarks(self): + """ + test that when we add bookmarks they show up. + when adding bookmarks for different hubs, we should + see them all in the result. + """ + commops = hubs.models.Hub(name="commops") + self.session.add(commops) + self._add_assoc("infra", "ralph", "stargazer") + self._add_assoc("i18n", "ralph", "member") + self._add_assoc("commops", "ralph", "subscriber") + ralph = hubs.models.User.query.get("ralph") + self.assertEquals(len(ralph.bookmarks["starred"]), 1) + self.assertEquals(ralph.bookmarks["starred"][0].name, "infra") + self.assertEquals(len(ralph.bookmarks["memberships"]), 1) + self.assertEquals(ralph.bookmarks["memberships"][0].name, "i18n") + self.assertEquals(len(ralph.bookmarks["subscriptions"]), 1) + self.assertEquals(ralph.bookmarks["subscriptions"][0].name, "commops") + + def test_user_bookmarks_star(self): + """ + test that when when adding associations for the same hub, + we should just see the stargazer one. + """ + self._add_assoc("infra", "ralph", "stargazer") + self._add_assoc("infra", "ralph", "member") + self._add_assoc("infra", "ralph", "subscriber") + ralph = hubs.models.User.query.get("ralph") + self.assertEquals(len(ralph.bookmarks["starred"]), 1) + self.assertEquals(ralph.bookmarks["starred"][0].name, "infra") + self.assertEquals(len(ralph.bookmarks["memberships"]), 0) + self.assertEquals(len(ralph.bookmarks["subscriptions"]), 0) + + def test_user_bookmarks_memberships(self): + """ + test that when when adding associations for the same hub, + we should just see the memberships one. + """ + self._add_assoc("infra", "ralph", "member") + self._add_assoc("infra", "ralph", "subscriber") + ralph = hubs.models.User.query.get("ralph") + self.assertEquals(len(ralph.bookmarks["starred"]), 0) + self.assertEquals(len(ralph.bookmarks["memberships"]), 1) + self.assertEquals(ralph.bookmarks["memberships"][0].name, "infra") + self.assertEquals(len(ralph.bookmarks["subscriptions"]), 0) diff --git a/hubs/utils/views.py b/hubs/utils/views.py index 2a4efd5..8cb05b9 100644 --- a/hubs/utils/views.py +++ b/hubs/utils/views.py @@ -65,13 +65,34 @@ def get_user_details(): user = flask.g.user current_user["hub"] = flask.url_for("hub", name=user.username) current_user["stream"] = flask.url_for("stream") - current_user["bookmarks"] = [] - for hub in user.bookmarks: - current_user["bookmarks"].append({ + + current_user["memberships"] = [] + for hub in user.bookmarks["memberships"]: + current_user["memberships"].append({ + "name": hub.name, + "user_hub": hub.user_hub, + "url": flask.url_for("hub", name=hub.name), + "cssClass": "idle-{}".format(hub.activity_class), + }) + + current_user["starred_hubs"] = [] + for hub in user.bookmarks["starred"]: + current_user["starred_hubs"].append({ "name": hub.name, + "user_hub": hub.user_hub, "url": flask.url_for("hub", name=hub.name), "cssClass": "idle-{}".format(hub.activity_class), }) + + current_user["subscriptions"] = [] + for hub in user.bookmarks["subscriptions"]: + current_user["subscriptions"].append({ + "name": hub.name, + "user_hub": hub.user_hub, + "url": flask.url_for("hub", name=hub.name), + "cssClass": "idle-{}".format(hub.activity_class), + }) + return current_user