From 15943bb2b9828e3f1307b3dd5f727b384d58b465 Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Fri, 25 May 2012 22:13:50 -0700 Subject: [PATCH] Updating read counts and story intelligence status on training. --- apps/analyzer/models.py | 2 + apps/analyzer/views.py | 2 +- apps/reader/models.py | 2 +- apps/reader/views.py | 3 +- media/js/newsblur/common/assetmodel.js | 7 +- media/js/newsblur/models/stories.js | 3 +- media/js/newsblur/reader/reader.js | 76 ++++++++++--------- media/js/newsblur/reader/reader_classifier.js | 20 ++--- media/js/newsblur/views/story_title_view.js | 1 + media/js/newsblur/views/story_view.js | 7 +- 10 files changed, 68 insertions(+), 55 deletions(-) diff --git a/apps/analyzer/models.py b/apps/analyzer/models.py index 5ceaef978..b0e82a35e 100644 --- a/apps/analyzer/models.py +++ b/apps/analyzer/models.py @@ -126,6 +126,8 @@ def get_classifiers_for_user(user, feed_id=None, social_user_id=None, classifier params['feed_id__in'] = feed_id if social_user_id: params['social_user_id'] = int(social_user_id.replace('social:', '')) + elif feed_id: + params['social_user_id'] = 0 if classifier_feeds is None: classifier_feeds = list(MClassifierFeed.objects(**params)) diff --git a/apps/analyzer/views.py b/apps/analyzer/views.py index fb374e8da..c1d48ca4c 100644 --- a/apps/analyzer/views.py +++ b/apps/analyzer/views.py @@ -78,9 +78,9 @@ def save_classifier(request): if content_type == 'feed': if not post_content.startswith('social:'): classifier_dict['feed_id'] = post_content - print classifier_dict # try: classifier, created = ClassifierCls.objects.get_or_create(**classifier_dict) + print classifier_dict, classifier, created # except OperationError: # continue if score == 0: diff --git a/apps/reader/models.py b/apps/reader/models.py index ff25e5f2a..635f28990 100644 --- a/apps/reader/models.py +++ b/apps/reader/models.py @@ -301,7 +301,7 @@ class UserSubscription(models.Model): # if not silent: # logging.info(' ---> [%s] Format stories: %s' % (self.user, datetime.datetime.now() - now)) - classifier_feeds = list(MClassifierFeed.objects(user_id=self.user_id, feed_id=self.feed_id)) + classifier_feeds = list(MClassifierFeed.objects(user_id=self.user_id, feed_id=self.feed_id, social_user_id=0)) classifier_authors = list(MClassifierAuthor.objects(user_id=self.user_id, feed_id=self.feed_id)) classifier_titles = list(MClassifierTitle.objects(user_id=self.user_id, feed_id=self.feed_id)) classifier_tags = list(MClassifierTag.objects(user_id=self.user_id, feed_id=self.feed_id)) diff --git a/apps/reader/views.py b/apps/reader/views.py index fcc6c3073..408bc2080 100644 --- a/apps/reader/views.py +++ b/apps/reader/views.py @@ -388,7 +388,7 @@ def load_single_feed(request, feed_id): logging.user(request, "~BR~FK~SBRedis is unavailable for shared stories.") # Get intelligence classifier for user - classifier_feeds = list(MClassifierFeed.objects(user_id=user.pk, feed_id=feed_id)) + classifier_feeds = list(MClassifierFeed.objects(user_id=user.pk, feed_id=feed_id, social_user_id=0)) classifier_authors = list(MClassifierAuthor.objects(user_id=user.pk, feed_id=feed_id)) classifier_titles = list(MClassifierTitle.objects(user_id=user.pk, feed_id=feed_id)) classifier_tags = list(MClassifierTag.objects(user_id=user.pk, feed_id=feed_id)) @@ -900,7 +900,6 @@ def add_url(request): code = 0 url = request.POST['url'] auto_active = is_true(request.POST.get('auto_active', True)) - print auto_active if not url: code = -1 diff --git a/media/js/newsblur/common/assetmodel.js b/media/js/newsblur/common/assetmodel.js index 63e1699fc..2df9ff5b4 100644 --- a/media/js/newsblur/common/assetmodel.js +++ b/media/js/newsblur/common/assetmodel.js @@ -1132,7 +1132,12 @@ NEWSBLUR.AssetModel = Backbone.Router.extend({ recalculate_story_scores: function(feed_id) { this.stories.each(_.bind(function(story, i) { if (story.get('story_feed_id') != feed_id) return; - var intelligence = _.extend({}, this.defaults.classifiers); + var intelligence = { + author: 0, + feed: 0, + tags: 0, + title: 0 + }; _.each(this.classifiers[feed_id].titles, _.bind(function(classifier_score, classifier_title) { if (story.get('story_title', '').indexOf(classifier_title) != -1) { diff --git a/media/js/newsblur/models/stories.js b/media/js/newsblur/models/stories.js index f5a658deb..e5c8af5de 100644 --- a/media/js/newsblur/models/stories.js +++ b/media/js/newsblur/models/stories.js @@ -83,7 +83,7 @@ NEWSBLUR.Collections.Stories = Backbone.Collection.extend({ mark_unread: function(story, options) { options = options || {}; NEWSBLUR.assets.mark_story_as_unread(story.id, story.get('story_feed_id'), function(read) { - NEWSBLUR.reader.update_read_count(story.id, story.get('story_feed_id')); + NEWSBLUR.reader.update_read_count(story.id, story.get('story_feed_id'), {unread: true}); }); story.set('read_status', 0); }, @@ -150,6 +150,7 @@ NEWSBLUR.Collections.Stories = Backbone.Collection.extend({ if (selected_story.get('selected')) { this.deselect(selected_story); this.active_story = selected_story; + NEWSBLUR.reader.active_story = selected_story; if (!selected_story.get('read_status')) { this.mark_read(selected_story); } diff --git a/media/js/newsblur/reader/reader.js b/media/js/newsblur/reader/reader.js index 893a8fe23..08bf73b9a 100644 --- a/media/js/newsblur/reader/reader.js +++ b/media/js/newsblur/reader/reader.js @@ -1368,6 +1368,8 @@ this.active_feed = null; this.active_story = null; + + NEWSBLUR.assets.stories.reset(); }, open_feed: function(feed_id, options) { @@ -1397,6 +1399,8 @@ if (NEWSBLUR.app.story_unread_counter) { NEWSBLUR.app.story_unread_counter.remove(); } + + NEWSBLUR.app.story_titles.show_loading(); // this.show_stories_progress_bar(); this.iframe_scroll = null; this.set_correct_story_view_for_feed(feed.id); @@ -2598,41 +2602,41 @@ feed_id = feed_id || this.active_feed; this.model.recalculate_story_scores(feed_id); - - var replace_stories = _.bind(function($story, story_id) { - var story = this.model.get_story(story_id); - if (story.get('story_feed_id') != feed_id) return; - var score = this.compute_story_score(story); - $story.removeClass('NB-story-positive') - .removeClass('NB-story-neutral') - .removeClass('NB-story-negative'); - if (score > 0) { - $story.addClass('NB-story-positive'); - } else if (score == 0) { - $story.addClass('NB-story-neutral'); - } else if (score < 0) { - $story.addClass('NB-story-negative'); - } - NEWSBLUR.log(['story recalculation', story, story.get('read_status'), $story]); - // $story.toggleClass('read', !story.get('read_status')); - $('.NB-feed-story-tags', $story).replaceWith(this.make_story_feed_tags(story)); - $('.NB-feed-story-author', $story).replaceWith(this.make_story_feed_author(story)); - $('.NB-feed-story-title', $story).replaceWith(this.make_story_feed_title(story)); - - if (this.model.preference('new_window') == 1) { - $('a', $story).attr('target', '_blank'); - } - }, this); - - _.each(this.cache.feed_view_stories, _.bind(function($story, story_id) { - replace_stories($story, story_id); - })); - - $('.story', this.$s.$story_titles).each(function() { - var $story = $(this); - var story_id = $story.data('story_id'); - replace_stories($story, story_id); - }); + // + // var replace_stories = _.bind(function($story, story_id) { + // var story = this.model.get_story(story_id); + // if (story.get('story_feed_id') != feed_id) return; + // var score = this.compute_story_score(story); + // $story.removeClass('NB-story-positive') + // .removeClass('NB-story-neutral') + // .removeClass('NB-story-negative'); + // if (score > 0) { + // $story.addClass('NB-story-positive'); + // } else if (score == 0) { + // $story.addClass('NB-story-neutral'); + // } else if (score < 0) { + // $story.addClass('NB-story-negative'); + // } + // NEWSBLUR.log(['story recalculation', story, story.get('read_status'), $story]); + // // $story.toggleClass('read', !story.get('read_status')); + // $('.NB-feed-story-tags', $story).replaceWith(this.make_story_feed_tags(story)); + // $('.NB-feed-story-author', $story).replaceWith(this.make_story_feed_author(story)); + // $('.NB-feed-story-title', $story).replaceWith(this.make_story_feed_title(story)); + // + // if (this.model.preference('new_window') == 1) { + // $('a', $story).attr('target', '_blank'); + // } + // }, this); + // + // _.each(this.cache.feed_view_stories, _.bind(function($story, story_id) { + // replace_stories($story, story_id); + // })); + // + // $('.story', this.$s.$story_titles).each(function() { + // var $story = $(this); + // var story_id = $story.data('story_id'); + // replace_stories($story, story_id); + // }); }, // ================================= @@ -3173,9 +3177,9 @@ this.model.classifiers[feed_id][type+'s'][value] = score; this.model.save_classifier(data, _.bind(function(resp) { + this.model.recalculate_story_scores(feed_id); this.force_feeds_refresh(callback, true, feed_id); }, this)); - this.recalculate_story_scores(feed_id); }, show_correct_feed_in_feed_title_floater: function(story) { diff --git a/media/js/newsblur/reader/reader_classifier.js b/media/js/newsblur/reader/reader_classifier.js index 305948edf..e4a2a163b 100644 --- a/media/js/newsblur/reader/reader_classifier.js +++ b/media/js/newsblur/reader/reader_classifier.js @@ -424,32 +424,32 @@ var classifier_prototype = { // NEWSBLUR.log(['Make Story', story, feed]); // HTML entities decoding. - story.story_title = $('
').html(story.story_title).text(); + story_title = _.string.trim($('
').html(story.get('story_title')).text()); this.$modal = $.make('div', { className: 'NB-modal-classifiers NB-modal' }, [ $.make('h2', { className: 'NB-modal-title' }), (this.options['feed_loaded'] && $.make('form', { method: 'post' }, [ - (story.story_title && $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ + (story_title && $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ $.make('h5', 'Story Title'), $.make('div', { className: 'NB-fieldset-fields NB-classifiers' }, [ - $.make('input', { type: 'text', value: story.story_title, className: 'NB-classifier-title-highlight' }), + $.make('input', { type: 'text', value: story_title, className: 'NB-classifier-title-highlight' }), this.make_classifier('Highlight phrases to look for in future stories', '', 'title'), $.make('span', - this.make_user_titles(story.story_title) + this.make_user_titles(story_title) ) ]) ])), - (story.story_authors && $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ + (story.get('story_authors') && $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ $.make('h5', 'Story Author'), $.make('div', { className: 'NB-fieldset-fields NB-classifiers' }, - this.make_authors([story.story_authors]) + this.make_authors([story.get('story_authors')]) ) ])), - (story.story_tags.length && $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ + (story.get('story_tags').length && $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ $.make('h5', 'Story Categories & Tags'), $.make('div', { className: 'NB-classifier-tags NB-fieldset-fields NB-classifiers' }, - this.make_tags(story.story_tags) + this.make_tags(story.get('story_tags')) ) ])), $.make('div', { className: 'NB-modal-field NB-fieldset' }, [ @@ -851,7 +851,7 @@ var classifier_prototype = { serialize_classifier: function() { var data = {}; $('.NB-classifier', this.$modal).each(function() { - var value = $('.NB-classifier-input-like', this).val(); + var value = _.string.trim($('.NB-classifier-input-like', this).val()); if ($('.NB-classifier-input-like, .NB-classifier-input-dislike', this).is(':checked')) { var name = $('input:checked', this).attr('name'); if (!data[name]) data[name] = []; @@ -886,9 +886,9 @@ var classifier_prototype = { $save.addClass('NB-disabled').attr('disabled', true); this.update_opinions(); + NEWSBLUR.assets.recalculate_story_scores(feed_id); this.model.save_classifier(data, function() { if (!keep_modal_open) { - NEWSBLUR.reader.recalculate_story_scores(feed_id); NEWSBLUR.reader.force_feeds_refresh(null, true); // NEWSBLUR.reader.force_feed_refresh(); // NEWSBLUR.reader.open_feed(self.feed_id, true); diff --git a/media/js/newsblur/views/story_title_view.js b/media/js/newsblur/views/story_title_view.js index 3dd155a8d..c9bcf92e6 100644 --- a/media/js/newsblur/views/story_title_view.js +++ b/media/js/newsblur/views/story_title_view.js @@ -15,6 +15,7 @@ NEWSBLUR.Views.StoryTitleView = Backbone.View.extend({ this.model.bind('change:read_status', this.toggle_read_status, this); this.model.bind('change:selected', this.toggle_selected, this); this.model.bind('change:starred', this.toggle_starred, this); + this.model.bind('change:intelligence', this.render, this); this.model.story_title_view = this; }, diff --git a/media/js/newsblur/views/story_view.js b/media/js/newsblur/views/story_view.js index ee2dcec36..67379d530 100644 --- a/media/js/newsblur/views/story_view.js +++ b/media/js/newsblur/views/story_view.js @@ -25,6 +25,7 @@ NEWSBLUR.Views.StoryView = Backbone.View.extend({ this.model.bind('change:read_status', this.toggle_read_status, this); this.model.bind('change:selected', this.toggle_selected, this); this.model.bind('change:starred', this.toggle_starred, this); + this.model.bind('change:intelligence', this.render, this); // Binding directly instead of using event delegation. Need for speed. this.$el.bind('mouseenter', this.mouseenter); @@ -215,7 +216,7 @@ NEWSBLUR.Views.StoryView = Backbone.View.extend({ preserve_classifier_color: function(classifier_type, value, score) { var $tag; this.$('.NB-feed-story-'+classifier_type).each(function() { - if ($(this).text() == value) { + if (_.string.trim($(this).text()) == value) { $tag = $(this); return false; } @@ -330,9 +331,9 @@ NEWSBLUR.Views.StoryView = Backbone.View.extend({ save_classifier: function(e) { var $tag = $(e.currentTarget); var classifier_type = $tag.hasClass('NB-feed-story-author') ? 'author' : 'tag'; - var tag = $tag.text(); + var tag = _.string.trim($tag.text()); var score = $tag.hasClass('NB-score-1') ? -1 : $tag.hasClass('NB-score--1') ? 0 : 1; - NEWSBLUR.reader.save_classifier(classifier_type, tag, score, feed_id); + NEWSBLUR.reader.save_classifier(classifier_type, tag, score, this.model.get('story_feed_id')); this.preserve_classifier_color(classifier_type, tag, score); },