Example #1
0
 def get(self, request):
     database_name = get_database_name(request.user)
     search_text = lower(request.GET["term"] or "")
     es = Elasticsearch(hosts=[{
         "host": ELASTIC_SEARCH_HOST,
         "port": ELASTIC_SEARCH_PORT
     }])
     search = Search(using=es, index=database_name, doc_type="reporter")
     search = search.extra(**{"size": "10"})
     resp = []
     if search_text:
         query_text_escaped = ElasticUtilsHelper().replace_special_chars(
             search_text)
         query_fields = [
             "name", "name_value", "name_exact", "short_code",
             "short_code_exact", "short_code_value"
         ]
         search = search.query("query_string",
                               query=query_text_escaped,
                               fields=query_fields)
         search_results = search.execute()
         resp = [{
             "id": result.short_code,
             "label": self.get_label(result)
         } for result in search_results.hits]
     return HttpResponse(json.dumps(resp))
 def get(self, request):
     search_text = lower(request.GET["term"] or "")
     database_name = get_database_name(request.user)
     query = elasticutils.S().es(urls=ELASTIC_SEARCH_URL, timeout=ELASTIC_SEARCH_TIMEOUT).indexes(database_name).doctypes("reporter") \
         .query(or_={'name__match': search_text, 'name_value': search_text, 'short_code__match': search_text,
                     'short_code_value': search_text}) \
         .values_dict()
     resp = [{"id": r["short_code"], "label": self.get_label(r)} for r in query[:min(query.count(), 50)]]
     return HttpResponse(json.dumps(resp))
Example #3
0
def subject_autocomplete(request, entity_type):
    search_text = lower(request.GET["term"] or "")
    database_name = get_database_name(request.user)
    dbm = get_database_manager(request.user)
    form_model = get_form_model_by_entity_type(dbm, [entity_type.lower()])
    subject_name_field = get_field_by_attribute_value(form_model, 'name', 'name')
    es_field_name_for_subject_name = es_questionnaire_field_name(subject_name_field.code, form_model.id)
    subject_short_code_field = get_field_by_attribute_value(form_model, 'name', 'short_code')
    es_field_name_for_short_code = es_questionnaire_field_name(subject_short_code_field.code, form_model.id)
    query = elasticutils.S().es(urls=ELASTIC_SEARCH_URL, timeout=ELASTIC_SEARCH_TIMEOUT).indexes(database_name).doctypes(lower(entity_type)) \
        .query(or_={es_field_name_for_subject_name + '__match': search_text,
                    es_field_name_for_subject_name + '_value': search_text,
                    es_field_name_for_short_code + '__match': search_text,
                    es_field_name_for_short_code + '_value': search_text}) \
        .values_dict()
    resp = [{"id": r[es_field_name_for_short_code], "label": r[es_field_name_for_subject_name]} for r in
            query[:min(query.count(), 50)]]
    return HttpResponse(json.dumps(resp))
def test_document_stores():
    document_stores = []
    for email in TEST_EMAILS:
        document_stores.append(get_database_name(User.objects.get(username=email)))
    return document_stores
Example #5
0
def _edit_questionnaire(request,
                        project_id,
                        excel_file=None,
                        excel_as_dict=None):
    manager = get_database_manager(request.user)
    questionnaire = Project.get(manager, project_id)
    try:
        xls_parser_response = _try_parse_xls(manager, request,
                                             questionnaire.name, excel_file)

        if isinstance(xls_parser_response, HttpResponse):
            return xls_parser_response

        doc = deepcopy(questionnaire._doc)
        doc.xform = MangroveService(
            request,
            questionnaire_code=questionnaire.form_code,
            xls_parser_response=xls_parser_response).xform_with_form_code
        new_questionnaire = Project.new_from_doc(manager, doc)
        QuestionnaireBuilder(new_questionnaire,
                             manager).update_questionnaire_with_questions(
                                 xls_parser_response.json_xform_data)
        xform_rules = get_all_rules()
        if excel_file is None:
            excel_file = _temp_file(request)
        else:
            excel_file.seek(0)
        questionnaire_wrapper = Questionnaire(excel_file)
        activity_log_detail = {}
        XFormEditor(
            Submission(manager, get_database_name(request.user), xform_rules),
            Validator(xform_rules),
            questionnaire_wrapper).edit(new_questionnaire, questionnaire,
                                        activity_log_detail)
        questionnaire = Project.get(manager, project_id)
        _save_questionnaire_as_dict_for_builder(questionnaire, excel_as_dict,
                                                excel_file)
        UserActivityLog().log(request,
                              action=EDITED_QUESTIONNAIRE,
                              project=questionnaire.name,
                              detail=json.dumps(activity_log_detail))

    except UnsupportedXformEditException as e:
        return HttpResponse(
            content_type='application/json',
            content=json.dumps({
                'success': False,
                'unsupported': True,
                'error_msg': [_("Unsupported edit operation")],
                "status": "error",
                'reason':
                "Unsupported edit operation",  # TODO: i18n translation
                'details': e.message
            }))
    except QuestionAlreadyExistsException as e:
        return HttpResponse(
            content_type='application/json',
            content=json.dumps({
                'success': False,
                'error_msg': [_(e.message)],
                "status": "error",
                'reason': "Save Failed",  # TODO: i18n translation
                'details': _(e.message)
            }))
    return HttpResponse(
        json.dumps({
            "success": True,
            "status": "success",
            'reason': 'Successfully updated'  # TODO: i18n translation
        }),
        content_type='application/json')
Example #6
0
 def __init__(self, form_model, project_name, user):
     self.form_model = form_model
     self.project_name = project_name
     self.db_name = get_database_name(user)
     self.user = user
Example #7
0
def get_database_manager(user):
    return get_db_manager(get_database_name(user))
def get_feed_database_name(user):
    from datawinners.main.utils import get_database_name
    database_name = get_database_name(user)
    return feeds_db_name(database_name)