def get_cohorts_for_article(article_id): """ Gets all the cohorts for this article """ article = Article.find_by_id(article_id) return json.dumps(CohortArticleMap.get_cohorts_for_article(article))
def add_article_to_cohort(): """ Gets all the articles of this teacher """ cohort = Cohort.find(request.form.get("cohort_id")) if not has_permission_for_cohort(cohort.id): flask.abort(401) article = Article.find_by_id(request.form.get("article_id")) if not CohortArticleMap.find(cohort.id, article.id): new_mapping = CohortArticleMap(cohort, article) db.session.add(new_mapping) db.session.commit() return "OK"
def delete_article_from_cohort(): """ Gets all the articles of this teacher """ cohort = Cohort.find(request.form.get("cohort_id")) if not has_permission_for_cohort(cohort.id): flask.abort(401) article = Article.find_by_id(request.form.get("article_id")) mapping = CohortArticleMap.find(cohort.id, article.id) if mapping: db.session.delete(mapping) db.session.commit() return "OK" else: return make_error(401, "That article does not belong to the cohort!")
def more_like_this_article(user, count, article_id): """ Given a article ID find more articles like that one via Elasticsearchs "more_like_this" method """ article = Article.find_by_id(article_id) query_body = build_more_like_this_query(count, article.content, article.language) es = Elasticsearch(ES_CONN_STRING) res = es.search(index=ES_ZINDEX, body=query_body) # execute search hit_list = res["hits"].get("hits") # TODO need to make sure either that the searched on article is always a part of the list \ # or that it is never there. # it could be used to show on website; you searched on X, here is what we found related to X final_article_mix = _to_articles_from_ES_hits(hit_list) return [ UserArticle.user_article_info(user, article) for article in final_article_mix ]
def _to_articles_from_ES_hits(hits): articles = [] for hit in hits: articles.append(Article.find_by_id(hit.get("_id"))) return articles