def update_submission_search_for_subject_edition(entity_doc, dbm):
    from datawinners.search.submission_query import SubmissionQueryBuilder

    entity_type = entity_doc.entity_type
    projects = []
    for row in dbm.load_all_rows_in_view('projects_by_subject_type',
                                         key=entity_type[0],
                                         include_docs=True):
        projects.append(
            Project.new_from_doc(dbm, ProjectDocument.wrap(row['doc'])))
    for project in projects:
        entity_field_code = None
        for field in project.entity_questions:
            if [field.unique_id_type] == entity_type:
                entity_field_code = field.code

        if entity_field_code:
            unique_id_field_name = es_field_name(entity_field_code, project.id)

            fields_mapping = {
                unique_id_field_name: entity_doc.data['name']['value']
            }
            args = {
                es_unique_id_code_field_name(unique_id_field_name):
                entity_doc.short_code
            }

            survey_response_filtered_query = SubmissionQueryBuilder(
                project).query_all(dbm.database_name, project.id, **args)

            for survey_response in survey_response_filtered_query.all():
                SubmissionIndexUpdateHandler(
                    dbm.database_name,
                    project.id).update_field_in_submission_index(
                        survey_response._id, fields_mapping)
def create_submission_index(dbm, row):
    form_model = Project.new_from_doc(dbm, ProjectDocument.wrap(row["value"]))
    form_code = form_model.form_code
    start_key = [form_code]
    end_key = [form_code, {}]
    rows = dbm.database.iterview("surveyresponse/surveyresponse",
                                 1000,
                                 reduce=False,
                                 include_docs=False,
                                 startkey=start_key,
                                 endkey=end_key)
    es = get_elasticsearch_handle(timeout=600)

    survey_response_docs = []
    for row in rows:
        survey_response = SurveyResponseDocument._wrap_row(row)
        search_dict = _meta_fields(survey_response, dbm)
        _update_with_form_model_fields(dbm, survey_response, search_dict,
                                       form_model)
        search_dict.update({'id': survey_response.id})
        survey_response_docs.append(search_dict)

    if survey_response_docs:
        es.bulk_index(dbm.database_name, form_model.id, survey_response_docs)
Beispiel #3
0
def update_datasender_for_project_change(project_doc, dbm, old_project=None):
    if not old_project or project_doc.name != old_project.name or project_doc.void != old_project.void:
        project = Project.new_from_doc(dbm, project_doc)
        for entity_doc in project.get_associated_datasenders(dbm):
            update_datasender_index(entity_doc, dbm)