def clean_links(a): """ Remove any existing links to `a` in the db :param a: :return: """ ls = RefTopicLinkSet({'toTopic': a}) if ls.count() > 0: ls.delete() ls = IntraTopicLinkSet({"$or": [{"fromTopic": a}, {"toTopic": a}]}) if ls.count() > 0: ls.delete()
def update_sheet_topic_links(sheet_id, new_topics, old_topics): """ Adds and removes sheet topic links per differences in old and new topics list. Only adds link for public sheets. """ topic_diff = topic_list_diff(old_topics, new_topics) for removed in topic_diff["removed"]: #print("removing {}".format(removed[1])) RefTopicLinkSet( { "class": "refTopic", "toTopic": removed[1], "expandedRefs": "Sheet {}".format(sheet_id), "linkType": "about", "is_sheet": True, "dataSource": "sefaria-users" }, hint="expandedRefs_1").delete() status = db.sheets.find_one({ "id": sheet_id }, { "status": 1 }).get("status", "unlisted") if status != "public": return for added in topic_diff["added"]: #print("adding {}".format(added[1])) attrs = { "class": "refTopic", "toTopic": added[1], "ref": "Sheet {}".format(sheet_id), "expandedRefs": ["Sheet {}".format(sheet_id)], "linkType": "about", "is_sheet": True, "dataSource": "sefaria-users" } tl = RefTopicLink(attrs) try: tl.save() except DuplicateRecordError: pass