def get_docs(doctype, docids):
    """
    Multiget for ES
    """
    docs = []
    for eid in docids:
        doc = conn.get(primary_index, doctype, eid)
        doc = elastic.es_tools.convert_to_legacy_format(doc)
        docs.append(doc["_source"])
    return docs
def get_parent_chain(doc, data, max_depth = 4):
    """
    Get all the organisation's parents and parents' parents etc.
    performance ?
    todo: remove and use es enchanced version
    """
    meta = doc.get('meta', {})
    parent_field_key = meta.get('parent_reference', None)

    if not parent_field_key:
        return None
    if max_depth == 0 or doc == None:
        return None

    from views import doc_presentation

    query_term = doc.get('fields').get(parent_field_key).get('field').get(
        'query')
    if query_term[0] == '_type':
        doctype = query_term[1]

    parent_id = data.get(parent_field_key, None)

    if parent_id:
        try:
            parent = conn.get(primary_index, doctype, parent_id)
            parent = es_tools.convert_to_legacy_format(parent)
        except:
            return None

        name =  doc_presentation(parent) # todo: language
        result = [(parent_id, name)]

        deeper = get_parent_chain(get_basedoc(doctype), parent['_source'],
                                  max_depth - 1)
        if deeper:
            result.extend(deeper)

        return result
    else:
        return None
Exemplo n.º 3
0
def hours_view(request, querytype):
    eid = qfilter = None

    if not 'time' in request.GET or (
        not 'doc_id' in request.GET and not 'filter' in request.GET):
        return HttpResponseBadRequest(
            "Required parameter: doc_id or filter and time.")

    if 'doc_id' in request.GET:
        eid = request.GET['doc_id']

    elif 'filter' in request.GET:
        qfilter = request.GET.getlist('filter')

    time = request.GET['time'] # todo: parse time

    if time == 'now':
        today = datetime.datetime.now()

    else:
        today = parse_time(time)
        if today == None:
            return HttpResponseBadRequest("Time format should be YYYY-MM-DD.")

    if querytype != 'week':
        # only weekly query implemented for now
        return HttpResponseBadRequest("Query type must be 'week'")

    js_docs = []

    if eid:
        # single document by id
        try:
            js_doc = conn.get(primary_index, 'organisation', eid)
            js_doc = es_tools.convert_to_legacy_format(js_doc)
            js_doc['_source']['_id'] = js_doc['_id']
            js_doc['_source']['_type'] = js_doc['_type']
            js_docs.append(js_doc['_source'])

        except ElasticSearchException as e:
            if e.result['exists'] == False:
                raise Http404, "Elastic Search can't find requested document."
            else:
                return HttpResponseServerError(
                    "Elastic Search: error executing query.")

    elif qfilter:
        # multiple documents by search filter
        strings = parse_filters(qfilter)
        string_queries = [pyes.query.StringQuery(value, default_field = field)
                          for field,value in strings]
        if len(string_queries) == 1:
            query = string_queries[0]
        else:
            query = pyes.query.BoolQuery(must = string_queries)

        try:
            results = conn.search(query, size = 1000,
                                  indexes = [primary_index],
                                  doc_types = ['organisation'])
        except ElasticSearchException as e:
                return HttpResponseServerError(
                    "Elastic search: error executing query.")

        for d in results['hits']['hits']:
            d['_source']['_id'] = d['_id']
            d['_source']['_type'] = d['_type']
            js_docs.append(d['_source'])

    my_results = []

    for doc in js_docs:
        periods  = doc.get ('period', '')
        lib_name = doc.get ('name_fi', '')
        lib_id   = doc.get ('_id')

        relevant_periods = find_periods_this_week(periods, today)
        aggregated_weekdays = aggregate_weekdays(relevant_periods, today)

        my_results.append({'id': lib_id,
                           'name_fi' : lib_name,
                           'hours' : aggregated_weekdays})

    if my_results:
        if len(my_results) == 0:
            raise Http404, "Couldn't find documents."

        if eid:
            # the contract is that id-based match returns only 1 doc
            return HttpResponse(json.dumps(my_results[0]))

        else:
            # filtering=searching returns an array
            return HttpResponse(json.dumps(my_results))

    else:
        raise Http404, "Couldn't find documents."