def save(doc):
    eid = doc['_id']
    del doc['_id']
    del doc['_type']
    conn.index(
        doc, primary_index,
        'organisation', eid)
def index_alt_week(week, organisation_id, name, group_id, doc_meta=None):
    dt = week[0]['date']

    doc_id = '{oid}::{date}::{group_id}'.format(
        oid = organisation_id,
        date = dt.isoformat(),
        group_id = group_id
    )

    print(doc_id)

    data = {
        'days': week,
        'organisation': organisation_id,
        'week_start': dt,
        'meta' : doc_meta
        }

    if name:
        data['name_fi'] = name

    if group_id == "readingroom":
        data["group_fi"] = "Lukusali"
        data["group_en"] = "Reading Room"
        data["group_sv"] = "Läsrum"
    elif group_id == "selfservice":
        data["group_fi"] = "Itsepalvelu"
        data["group_en"] = "Self Service"
        data["group_sv"] = "Självservering"
    else:
        data["group_fi"] = ""
        data["group_en"] = ""
        data["group_sv"] = ""

    c = conn.index(data, hours_index, "week_alt", doc_id, bulk=True)
def do_linking_docs(index):
    f = pyes.ExistsFilter("services.model")
    q = pyes.FilteredQuery(pyes.MatchAllQuery(), f)

    docs = es_tools.get_across_multiple_pages(q, None, [index], ["organisation"])

    model_docs = {}
    linkupdatetime = datetime.datetime.now()

    for doc in docs:
        eid = doc.get("_id")
        updated = False

        for service in doc["services"]:
            if service["model"]:
                model = model_docs.get(service["model"])
                if not model:
                    fulldoc = es_tools.get_doc_from_index(index, "service", service["model"])
                    if fulldoc:
                        model = fulldoc.get("_source")
                        model_docs[service["model"]] = model
                    else:
                        print "deleted", eid, service

                if model:
                    if "links_updated" not in doc["meta"] or (
                        doc["meta"]["links_updated"] <= model["meta"]["modified"]
                    ):

                        service.update(model)
                        del service["meta"]
                        es_tools.clean_source(doc)
                        updated = True

        if updated:
            doc["meta"]["links_updated"] = linkupdatetime
            conn.index(doc, index, "organisation", eid)
def index_week(week, organisation_id, name=None, doc_meta=None):
    dt = week[0]['date']

    doc_id = '{oid}::{date}'.format(
        oid = organisation_id,
        date = dt.isoformat()
    )

    data = {
        'days': week,
        'organisation': organisation_id,
        'week_start': dt,
        'meta' : doc_meta
        }

    if name:
        data['name_fi'] = name

    c = conn.index(data, hours_index, "week", doc_id, bulk=True)