From e4df27462cd833c8f7ced2badc75af431c2134eb Mon Sep 17 00:00:00 2001 From: Eric Barbour Date: Aug 10 2016 19:35:20 +0000 Subject: Make stream code more readable, improve save --- diff --git a/hubs/app.py b/hubs/app.py index 67cad13..ec76fdb 100755 --- a/hubs/app.py +++ b/hubs/app.py @@ -457,7 +457,7 @@ def widget_source(name): from hubs.widgets import registry base = '/hubs/' fname = '' - + try: fname = base + registry[name].__file__.split(base, 1)[1] except KeyError: @@ -522,6 +522,31 @@ def login_required(function): return decorated_function +def apply_html(actions): + # Smash multiple messages into single ones + actions = fedmsg.meta.conglomerate(actions, lexers=True, **fedmsg_config) + + # Apply some html markup to the subtitles for extra fanciness. + for match in actions: + match['markup'] = hubs.widgets.feed.apply_markup(match) + for idx, constituent in match['msg_ids'].items(): + constituent['markup'] = hubs.widgets.feed.apply_markup(constituent) + if constituent['long_form'] != constituent['subtitle']: + if constituent.get('lexer'): + constituent['long_form'] = pygments.highlight( + constituent['long_form'], + constituent['lexer'], + pygments.formatters.HtmlFormatter(style='monokai'), + ) + else: + markup = u"
{long_form}
".format(**constituent) + constituent['long_form'] = markup + # And tack on a unique identifier for each top level entry. + match['dom_id'] = six.text_type(uuid.uuid4()) + + return actions + + @app.route('//stream') @app.route('//stream/') @login_required @@ -529,7 +554,7 @@ def stream(name): saved = hubs.models.SavedNotification.by_username(session, name) saved = [n.__json__() for n in saved] - actions_rule = { + preference = { 'filters': [{ 'created_on': 'Thu, 15 Jan 2015 16:07:43 GMT', 'id': 20506, @@ -560,56 +585,37 @@ def stream(name): }] }] } - actions_rule = hubs.widgets.feed.rehydrate_preference(actions_rule) + preference = hubs.widgets.feed.rehydrate_preference(preference) messages = [] - matches = [] + actions = [] delta = datetime.timedelta(days=365) end = datetime.datetime.utcnow() - fmn_hinting = fmn.lib.hinting.gather_hinting(fedmsg_config, actions_rule['filters'][0]['rules'], paths) - total, pages, rows = datanommer.models.Message.grep( - start=end - delta, - end=end, - rows_per_page=100, - page=1, - order='desc', - **fmn_hinting - ) - messages.extend(rows) + for fmn_filter in preference['filters']: + fmn_hinting = fmn.lib.hinting.gather_hinting(fedmsg_config, fmn_filter['rules'], paths) + total, pages, rows = datanommer.models.Message.grep( + start=end - delta, + end=end, + rows_per_page=100, + page=1, + order='desc', + **fmn_hinting + ) + messages.extend(rows) for message in messages: original = message.__json__() if name in fedmsg.meta.msg2usernames(original, **fedmsg_config): - matches.append(original) - - # Smash multiple messages into single ones - matches = fedmsg.meta.conglomerate(matches, lexers=True, **fedmsg_config) + actions.append(original) - # Apply some html markup to the subtitles for extra fanciness. - for match in matches: - match['markup'] = hubs.widgets.feed.apply_markup(match) - for idx, constituent in match['msg_ids'].items(): - constituent['markup'] = hubs.widgets.feed.apply_markup(constituent) - if constituent['long_form'] != constituent['subtitle']: - if constituent.get('lexer'): - constituent['long_form'] = pygments.highlight( - constituent['long_form'], - constituent['lexer'], - pygments.formatters.HtmlFormatter(style='monokai'), - ) - else: - markup = u"
{long_form}
".format(**constituent) - constituent['long_form'] = markup - - # And tack on a unique identifier for each top level entry. - match['dom_id'] = six.text_type(uuid.uuid4()) + actions = apply_html(actions) - matches = json.dumps(matches, cls=hubs.widgets.feed.PythonObjectEncoder) + actions = json.dumps(actions, cls=hubs.widgets.feed.PythonObjectEncoder) return flask.render_template( 'stream.html', saved=json.dumps(saved), - actions=matches + actions=actions ) @@ -629,13 +635,15 @@ def notifications(user): markup = data['markup'] link = data['link'] icon = data['secondary_icon'] + dom_id = data['dom_id'] except: return str(flask.request.data) notification = hubs.models.SavedNotification( username=user, markup=markup, link=link, - secondary_icon=icon + secondary_icon=icon, + dom_id=dom_id ) session.add(notification) session.commit() diff --git a/hubs/models.py b/hubs/models.py index 03217c1..48ed913 100755 --- a/hubs/models.py +++ b/hubs/models.py @@ -487,23 +487,26 @@ class SavedNotification(BASE): __tablename__ = 'savednotifications' user = sa.Column(sa.Text, sa.ForeignKey('users.username')) + created = sa.Column(sa.DateTime, default=datetime.datetime.utcnow) + dom_id = sa.Column(sa.Text) idx = sa.Column(sa.Integer, primary_key=True) - markup = sa.Column(sa.Text) link = sa.Column(sa.Text) + markup = sa.Column(sa.Text) secondary_icon = sa.Column(sa.Text) - created = sa.Column(sa.DateTime, default=datetime.datetime.utcnow) - def __init__(self, username=None, markup='', link='', secondary_icon=''): + def __init__(self, username=None, markup='', link='', secondary_icon='', dom_id=''): self.user = username self.markup = markup self.link = link self.secondary_icon = secondary_icon + self.dom_id = dom_id def __json__(self): return { 'created': str(self.created), 'date_time': str(self.created), + 'dom_id': self.dom_id, 'idx': self.idx, 'link': bleach.linkify(self.link), 'markup': bleach.linkify(self.markup), diff --git a/hubs/static/client/app/components/Panel.jsx b/hubs/static/client/app/components/Panel.jsx index 1651554..a30eab1 100644 --- a/hubs/static/client/app/components/Panel.jsx +++ b/hubs/static/client/app/components/Panel.jsx @@ -1,4 +1,4 @@ -import React from 'react'; + import React from 'react'; import $ from 'jquery'; import Icon from './Icon.jsx'; @@ -9,6 +9,7 @@ const Panel = function (props) { link: props.match.link, markup: props.match.markup, secondary_icon: props.match.secondary_icon, + dom_id: props.match.dom_id, }; function onClick(data) { @@ -18,25 +19,22 @@ const Panel = function (props) { data: JSON.stringify(data), contentType: 'application/json', }).done(resp => { - const id = '#save' + resp.notification.idx; + const id = "#save" + props.match.dom_id; const $saveBtn = $(id); - $saveBtn.removeClass('btn-default') - .addClass('disabled btn-success') - .text('Saved'); - console.log(resp); + $saveBtn.removeClass('btn-default').addClass('btn-success').text('SAVED') }); } - const button = props.match.saved - ? null - : (); - + let saveBtn; + if (!props.match.saved) { + saveBtn = (); + } return (
- {button} + {saveBtn}
);