def add_linkdata_view(request, doctype, exdoctype, linkfield):
    """
    Function adds given given exdoc_id's to given target field and
    sets given linkfield with the modeldocs eid

    TODO: reliable way to find where to put the eid of linked doc in form
          reasonable way to loop the model docs with their id's

    building the form is ever so slightly different when one wants to
    update it with some values and replace with others (linkfield and modeldoc)
    """
    #append_data = conn.get(settings.INDEX_NAME, doctype, exdoc_id)
    exdoc_ids = request.GET.getlist('exdoc_id')
    if not exdoc_ids:
        raise Http404
    append_key = request.GET.get('target')
    if not append_key:
        raise Http404
    template = None
    if doctype == 'organisation':
        template = env.get_template('organisation_form.html')

    eid = request.POST.get('doc_id')
    data, doc, baseform, template = get_basics(request, doctype, template=template)
    example_data = searcher.get_docs(exdoctype, exdoc_ids)
    #if is_compoundfield(doc, append_key):
    #    append_data = example_data
    #else:
    #    append_data = create_exampledoc(doc, append_key, example_data[0])
    #TODO: plz fix.. doclist and eid list are in same order, but not pretty
    for eid_num, modeldoc in enumerate(example_data):
        #TODO: plz fix.. linkfield might not be at first level of subdoc
        modeldoc[linkfield] = exdoc_ids[eid_num]
    context = add_linkdata_doc(doc, baseform, data, append_key, example_data)
    if not context:
        raise Http404

    path = request.path_info + '?doc_id={0}'.format(eid)

    context.update({
        'type' : doctype,
        'doc' : json.dumps(doc),
        'user': request.user,
        'doc_id' : eid,
        'title': data.get('name_fi'),
        "description" : doc.get("description").capitalize(),
        'reload_path': '/add/{dt}/?doc_id={eid}'.format(dt=doctype, eid=eid),
        'languages' : supported_languages(),
        'tab_ids'     : prepare_tabs(context['form']),
        'skip_js': True,
        'request': request
    })
    s = template_render(template, context)
    sr = re.sub(r'\s+', ' ', s)
    return HttpResponse(sr)
def add_exampledata_view(request, doctype, exdoctype, append_key):
    #append_data = conn.get(settings.INDEX_NAME, doctype, exdoc_id)
    exdoc_ids = request.GET.getlist('exdoc_id')

    if not exdoc_ids:
        raise Http404

    template = None

    eid = request.POST.get('doc_id')

    if doctype == 'organisation':
        template = env.get_template('organisation_form.html')
    data, doc, baseform, template = get_basics(request, doctype, template=template)
    example_data = searcher.get_docs(exdoctype, exdoc_ids)
    if is_compoundfield(doc, append_key):
        append_data = example_data
    else:
        append_data = create_exampledoc(doc, append_key, example_data[0])
    context = add_fielddata_doc(doc, baseform, data, append_key, append_data)
    if not context:
        raise Http404

    path = request.path_info + '?doc_id={0}'.format(eid)

    context.update({
        'type' : doctype,
        'doc' : json.dumps(doc),
        'user': request.user,
        'doc_id' : eid,
        'title': data.get('name_fi'),
        "description" : doc.get("description").capitalize(),
        'reload_path': '/add/{dt}/?doc_id={eid}'.format(dt=doctype,
                                                                    eid=eid),
        'languages' : supported_languages(),
        'tab_ids'     : prepare_tabs(context['form']),
        'skip_js': True,
        'request': request})
    s = template_render(template, context)
    return HttpResponse(s)