Exemplo n.º 1
0
def modify_text(user, oref, vtitle, lang, text, vsource=None, **kwargs):
    """
    Updates a chunk of text, identified by oref, versionTitle, and lang, and records history.
    :param user:
    :param oref:
    :param vtitle:
    :param lang:
    :param text:
    :param vsource:
    :return:
    """
    chunk = model.TextChunk(oref, lang, vtitle)
    if getattr(chunk.version(), "status", "") == "locked" and not is_user_staff(user):
        raise InputError("This text has been locked against further edits.")
    action = kwargs.get("type") or "edit" if chunk.text else "add"
    old_text = chunk.text
    chunk.text = text
    if vsource:
        chunk.versionSource = vsource  # todo: log this change
    if chunk.save():
        model.log_text(user, action, oref, lang, vtitle, old_text, text, **kwargs)
        if USE_VARNISH:
            invalidate_ref(oref, lang=lang, version=vtitle, purge=True)
            if oref.next_section_ref():
                invalidate_ref(oref.next_section_ref(), lang=lang, version=vtitle, purge=True)
            if oref.prev_section_ref():
                invalidate_ref(oref.prev_section_ref(), lang=lang, version=vtitle, purge=True)
        if not kwargs.get("skip_links", None):
            from sefaria.helper.link import add_commentary_links, add_links_from_text, rebuild_commentary_links
            # Commentaries generate links to their base text automatically
            if oref.type == "Commentary":
                rebuild_commentary_links(oref.normal(), user, **kwargs)
            # scan text for links to auto add
            add_links_from_text(oref.normal(), lang, chunk.text, chunk.full_version._id, user, **kwargs)

            if USE_VARNISH:
                for linkref in {r.section_ref() for r in oref.linkset().refs_from(oref)}:
                    invalidate_ref(linkref)

    return chunk
Exemplo n.º 2
0
 def test_rebuild_commentary_links(self):
     rebuild_commentary_links("Rashi on Menachot", 1)
     rebuild_commentary_links("Rashi on Exodus", 1)