def remove_from_cluster(self, article): """Removes article with id == article_id from the cluster it belongs to If it's the only article of the cluster will delete the cluster Return True if the article is deleted at the end or not """ from jarr.controllers import ClusterController if not article.cluster_id: return clu_ctrl = ClusterController(self.user_id) cluster = clu_ctrl.read(id=article.cluster_id).first() if not cluster: return try: new_art = next(new_art for new_art in cluster.articles if new_art.id != article.id) except StopIteration: # only on article in cluster, deleting cluster clu_ctrl.delete(cluster.id, delete_articles=False) else: if cluster.main_article_id == article.id: cluster.main_article_id = None clu_ctrl.enrich_cluster(cluster, new_art, cluster.read, cluster.liked, force_article_as_main=True) self.update({'id': article.id}, { 'cluster_id': None, 'cluster_reason': None, 'cluster_score': None, 'cluster_tfidf_with': None, 'cluster_tfidf_neighbor_size': None }) return
def test_cluster_enabled(self): ccontr = ClusterController() cluster = ccontr.read().first() feed = FeedController(cluster.user_id).read( category_id__ne=None, id__nin=[art.feed_id for art in cluster.articles]).first() category = feed.category # clustering works when all is true update_on_all_objs(articles=cluster.articles, feeds=[feed], cluster_enabled=True) article = self.create_article_from(cluster, feed) self.assertInCluster(article, cluster) # disabling on user desactivate all clustering by default update_on_all_objs(articles=cluster.articles, feeds=[feed], cluster_enabled=None) UserController().update({'id': cluster.user_id}, {'cluster_enabled': False}) article = self.create_article_from(cluster, feed) self.assertNotInCluster(article, cluster) # disabling on article's feed prevents from clustering update_on_all_objs(articles=cluster.articles, feeds=[feed], cluster_enabled=True) FeedController().update({'id': feed.id}, {'cluster_enabled': False}) article = self.create_article_from(cluster, feed) self.assertNotInCluster(article, cluster) # disabling on feed from cluster's articles prevents from clustering update_on_all_objs(articles=cluster.articles, feeds=[feed], cluster_enabled=True) FeedController().update( {'id__in': [a.feed_id for a in cluster.articles]}, {'cluster_enabled': False}) article = self.create_article_from(cluster, feed) self.assertNotInCluster(article, cluster) # disabling on article's category prevents from clustering CategoryController(cluster.user_id).update({'id': category.id}, {'cluster_enabled': False}) article = self.create_article_from(cluster, feed) self.assertNotInCluster(article, cluster) update_on_all_objs(articles=cluster.articles, feeds=[feed], cluster_enabled=True) article = self.create_article_from(cluster, feed) self.assertInCluster(article, cluster)