def restore(doctype, curdoc, resdoc, user):
    """
    Makes a version from curdoc and saves resdoc
    with the curdoc's id.
    """
    eid = curdoc["_id"]
    del curdoc["_id"]
    del curdoc["_type"]
    del resdoc["_id"]
    del resdoc["_type"]
    make_version(primary_index, version_index,
                     doctype, user.username, eid=eid, data=curdoc)
    conn.index(resdoc, primary_index, doctype, eid)
def es_import(doctype, index=primary_index, path=".",
              docs=None):
    """
    Imports documents from JSON file into ES.
    """

    if not docs:
        docs = json.load(open("%s/export_%s.json" % (path, doctype),'r'),
            cls=OrderedJSONDecoder)

    for doc in docs:
        eid = doc["_id"]
        del doc["_id"]
        del doc["_type"]
        conn.index(doc, index, doctype, eid)
def make_version(original_index, version_index,
                 doctype, username, eid=None, data=None):
    """
    Saves original document to versions index and sets original link
    in metadata to point to original document's id.
    """
    if eid:
        data = get_doc_from_index(original_index, doctype, eid)
        if data:
            data = data["_source"]
            if '_id' in data:
                del data['_id']
            if '_type' in data:
                del data['_type']
    else:
        return None
    data["meta"]["original"] = eid
    msg = conn.index(data, version_index, doctype)
def save(data, meta, user, eid=None, meta_field_data = None, preserved_metadata=None):
    add_metadata(data, user, meta_field_data, preserved_metadata = preserved_metadata)

    if meta.get("document_type") == "organisation":
        collect_periods(data)

    try:
        hooks.do_operations('beforesave', data, doctype=meta['document_type'])
        doc_id = data.get('id', eid)

        msg = conn.index(
            data, primary_index, meta['document_type'],
            doc_id)

        hooks.do_operations('aftersave', data, metadata={
                'id': doc_id, 'doc_meta': data.get('meta')},
                            doctype=meta['document_type'])

        return msg

    except ElasticSearchException as e:
        return {'ok': False}
def undelete_version(doctype, version_id):
    """
    Restores a document that was deleted.
    doc_id is the id of the original (deleted) document
    and version_id is the id of the version to
    be restored.
    """
    doc = get_source(
            get_doc_from_index(
                version_index,
                doctype,
                version_id))

    clean_source(doc)
    doc_id = doc['meta']['deleted']
    del doc['meta']['deleted']
    result = conn.index(doc, primary_index, doctype, doc_id)
    if result.get('ok', False):
        conn.delete(version_index, doctype, version_id)
        return True
    else:
        return False
def chown_docs(update_ids, groupname, doctype, docs, index):
    for doc in docs:
        if doc["_id"] in update_ids:
            doc.setdefault("meta", {})["group"] = groupname
            conn.index(doc, index, doctype, doc["_id"])
    return _("%d documents were changed") % len(update_ids)