def migrate_to_groups(doc):
    """
    (2012-08-06)
    Decides the document group from it's municipality.
    """
    mapping = {
        "helsinki": "helsingin_kaupunginkirjasto",
        "vantaa": "vantaan_kaupunginkirjasto",
        "vanda": "vantaan_kaupunginkirjasto",
        "kauniainen": "kauniaisten_kaupunginkirjasto",
        "espoo": "espoon_kaupunginkirjasto",
    }

    municipality = None
    pdoc = doc

    while not municipality:
        municipality = pdoc.get("contact", {}).get("street_address", {}).get("municipality_fi")

        pid = pdoc.get("parent_organisation")
        if not pid:
            break
        pdoc = get_doc_from_index(primary_index, "organisation", pid).get("_source")
        if not pdoc:
            break

    if isinstance(municipality, basestring):
        group = mapping.get(municipality.strip().lower())
        if "meta" in doc and group:
            doc["meta"]["group"] = group
    def __call__(self, field, **kwargs):
        kwargs.setdefault('id', field.id)
        kwargs.setdefault('type', self.input_type)
        kwargs['class'] += u" linkfield"
        if 'value' not in kwargs:
            kwargs['value'] = field._value()
        if not kwargs['value']:
            msg = _('Not chosen')
        else:
            doc = get_doc_from_index(
                primary_index,
                self.field_data["field"]["linkdocument"], kwargs["value"])

            if not doc:
                del kwargs['value']
                msg = _('Not chosen (the target was removed )')
            else:
                msg = get_source(doc).get("name_fi")

        return HTMLString(u'<input %s><span class="label">%s</span>' %
                          (html_params(name=field.name, **kwargs), msg))
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)