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.cluster import ClusterController from jarr.controllers.article_clusterizer import Clusterizer 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 Clusterizer(article.user_id).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})
def test_delete(self): clu_ctrl = ClusterController() for cluster in clu_ctrl.read(): clu_ctrl.delete(cluster.id) self.assertEqual(0, ClusterController(2).read().count()) self.assertEqual(0, ArticleController(2).read().count())