예제 #1
0
 def test_should_import_subjects_with_form_code(self):
     file_name = "reporters.csv"
     request = Mock()
     request.GET = {'qqfile': file_name}
     request.raw_post_data = self.csv_subjects_data
     organization = Mock(spec=Organization)
     entity_type = "clinic"
     define_type(self.manager, [entity_type])
     self.create_form_for_entity_type()
     with patch("datawinners.utils.get_organization_from_manager"
                ) as get_organization_from_dbm_mock:
         get_organization_from_dbm_mock.return_value = Mock(
             return_value=organization)
         with patch(
                 "datawinners.utils.get_organization") as get_organization:
             mock = Mock(return_value=organization)
             mock.org_id = 'abc'
             get_organization.return_value = mock
             error_message, failure_imports, success_message, imported_entities = import_data(
                 request=request, manager=self.manager, form_code=FORM_CODE)
             self.assertEqual(
                 4,
                 get_entity_count_for_type(self.manager,
                                           entity_type=entity_type))
             self.assertEqual(4, len(imported_entities))
             self.assertEqual(entity_type, imported_entities["cli1"])
             self.assertEqual(entity_type, imported_entities["cli2"])
             self.assertEqual(entity_type, imported_entities["cli3"])
             self.assertEqual(entity_type, imported_entities["cli4"])
예제 #2
0
    def test_should_generate_short_code_and_import_data_senders_if_short_code_is_not_given(
            self):
        file_name = "reporters.csv"
        request = Mock()
        request.GET = {'qqfile': file_name}
        request.raw_post_data = self.csv_data_without_short_code
        organization = Mock(spec=Organization)
        with patch("datawinners.utils.get_organization_from_manager"
                   ) as get_organization_from_dbm_mock:
            with patch(
                    "datawinners.utils.get_organization") as get_organization:
                mock = Mock(return_value=organization)
                mock.org_id = 'abc'
                get_organization.return_value = mock
                get_organization_from_dbm_mock.return_value = Mock(
                    return_value=organization)

                error_message, failure_imports, success_message, imported_entities = import_data(
                    request=request,
                    manager=self.manager,
                    form_code='form_code')
                self.assertEqual(
                    4,
                    get_entity_count_for_type(self.manager,
                                              entity_type="reporter"))
                self.assertEqual(4, len(imported_entities))
                self.assertEqual('reporter', imported_entities["rep1"])
                self.assertEqual('reporter', imported_entities["rep2"])
                self.assertEqual('reporter', imported_entities["rep3"])
                self.assertEqual('reporter', imported_entities["rep4"])
예제 #3
0
    def post(self, request, *args, **kwargs):
        parser_dict = {
            '.xls': XlsDatasenderParser,
            '.xlsx': XlsxDataSenderParser
        }
        manager = get_database_manager(request.user)
        file_extension = os.path.splitext(request.GET["qqfile"])[1]
        parser = parser_dict.get(file_extension, None)
        is_update = request.GET.get('is_update') == 'true'
        error_message, failure_imports, success_message, successful_imports = import_module.import_data(
            request, manager, default_parser=parser, is_update=is_update)

        imported_data_senders = parse_successful_imports(successful_imports)
        self._convert_anonymous_submissions_to_registered(
            imported_data_senders, manager)

        self.update_activity_log(request, successful_imports)
        transaction.commit(
        )  #Mandatory for manually managed transaction blocks. Here it won't save anything

        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'successful_imports':
                imported_data_senders
            }))
예제 #4
0
 def test_should_import_data_senders(self):
     file_name = "reporters.csv"
     request = Mock()
     request.GET = {'qqfile' : file_name}
     request.raw_post_data = self.csv_data
     error_message, failure_imports, success, success_message = import_data(request=request,manager = self.dbm)
     self.assertTrue(success)
     self.assertEqual(4,get_entity_count_for_type(self.dbm,entity_type="reporter"))
예제 #5
0
 def test_should_import_data_senders(self):
     file_name = "reporters.csv"
     request = Mock()
     request.GET = {'qqfile': file_name}
     request.raw_post_data = self.csv_data
     error_message, failure_imports, success, success_message = import_data(
         request=request, manager=self.dbm)
     self.assertTrue(success)
     self.assertEqual(
         4, get_entity_count_for_type(self.dbm, entity_type="reporter"))
예제 #6
0
def registered_datasenders(request, project_id):
    manager = get_database_manager(request.user)
    questionnaire = Project.get(manager, project_id)
    project_links = get_project_link(questionnaire)
    dashboard_page = settings.HOME_PAGE + "?deleted=true"
    if questionnaire.is_void():
        return HttpResponseRedirect(dashboard_page)
    if request.method == 'GET':
        in_trial_mode = _in_trial_mode(request)
        user_rep_id_name_dict = rep_id_name_dict_of_users(manager)
        return render_to_response(
            'project/registered_datasenders.html', {
                'project': questionnaire,
                'project_links': project_links,
                'questionnaire_code': questionnaire.form_code,
                'current_language': translation.get_language(),
                'is_quota_reached': is_quota_reached(request),
                'in_trial_mode': in_trial_mode,
                'user_dict': json.dumps(user_rep_id_name_dict)
            },
            context_instance=RequestContext(request))
    if request.method == 'POST':
        error_message, failure_imports, success_message, successful_imports = import_module.import_data(
            request, manager, default_parser=XlsDatasenderParser)
        imported_data_senders = parse_successful_imports(successful_imports)
        imported_datasenders_ids = [
            imported_data_sender["id"]
            for imported_data_sender in imported_data_senders
        ]
        _add_imported_datasenders_to_project(imported_datasenders_ids, manager,
                                             questionnaire)

        if len(imported_datasenders_ids):
            UserActivityLog().log(request,
                                  action=IMPORTED_DATA_SENDERS,
                                  detail=json.dumps(
                                      dict({
                                          "Unique ID":
                                          "[%s]" %
                                          ", ".join(imported_datasenders_ids)
                                      })),
                                  project=questionnaire.name)
        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'successful_imports':
                imported_data_senders
            }))
예제 #7
0
def all_datasenders(request):
    manager = get_database_manager(request)
    if request.method == 'POST':
        error_message, failure_imports, success, success_message = import_module.import_data(request, manager)
        all_data_senders = import_module.load_all_subjects_of_type(request)

        return HttpResponse(json.dumps({'success': success, 'message': success_message, 'error_message': error_message,
                                        'failure_imports': failure_imports, 'all_data': all_data_senders}))
    all_data_senders = import_module.load_all_subjects_of_type(request)
    return render_to_response('entity/all_datasenders.html', {'all_data': all_data_senders},
                              context_instance=RequestContext(request))
예제 #8
0
def import_subjects_from_project_wizard(request):
    manager = get_database_manager(request)
    error_message, failure_imports, success, success_message = import_module.import_data(
        request, manager)
    return HttpResponse(
        json.dumps({
            'success': success,
            'message': success_message,
            'error_message': error_message,
            'failure_imports': failure_imports
        }))
예제 #9
0
 def test_should_import_subjects_with_wrong_form_code(self):
     file_name = "reporters.csv"
     request = Mock()
     request.GET = {'qqfile': file_name}
     request.raw_post_data = self.csv_subjects_data_with_error_form_code
     organization = Mock(spec=Organization)
     entity_type = "clinic"
     define_type(self.manager, [entity_type])
     self.create_form_for_entity_type()
     with patch("datawinners.utils.get_organization_from_manager") as get_organization_from_dbm_mock:
         get_organization_from_dbm_mock.return_value = Mock(return_value=organization)
         with patch("datawinners.entity.import_data.get_organization") as get_organization:
             mock = Mock( return_value=organization )
             mock.org_id = 'abc'
             get_organization.return_value = mock
             error_message, failure_imports, success_message, imported_entities = import_data(request=request, manager = self.manager, form_code=FORM_CODE )
             self.assertEqual(0, len(imported_entities))
             self.assertEqual('The file you are uploading is not a list of [clinic]. Please check and upload again.', error_message)
예제 #10
0
def all_subjects(request, form_code=None):
    manager = get_database_manager(request.user)
    if request.method == 'POST':
        error_message, failure_imports, success_message, imported_entities = import_module.import_data(
            request,
            manager,
            default_parser=XlsOrderedParser,
            form_code=form_code)
        if len(imported_entities) != 0:
            detail_dict = dict()
            for short_code, entity_type in imported_entities.items():
                entity_type = entity_type.capitalize()
                if detail_dict.get(entity_type) is not None:
                    detail_dict.get(entity_type).append(short_code)
                else:
                    detail_dict.update({entity_type: [short_code]})
            for key, detail in detail_dict.items():
                detail_dict.update({key: "[%s]" % ", ".join(detail)})
            UserActivityLog().log(request,
                                  action=IMPORTED_SUBJECTS,
                                  detail=json.dumps(detail_dict))
        subjects_data = import_module.load_all_subjects(manager)
        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'all_data':
                subjects_data,
                'imported':
                imported_entities.keys()
            }))
    subjects_data = import_module.load_all_subjects(manager)
    return render_to_response('entity/all_subjects.html', {
        'all_data': subjects_data,
        'current_language': translation.get_language(),
        'edit_url': '/entity/subject/edit/'
    },
                              context_instance=RequestContext(request))
예제 #11
0
def import_subjects_from_project_wizard(request):
    manager = get_database_manager(request.user)
    project_id = request.GET.get('project_id')
    error_message, failure_imports, success_message, imported_entities = import_module.import_data(
        request, manager)
    if project_id is not None:
        _associate_data_senders_to_project(imported_entities, manager,
                                           project_id)
    return HttpResponse(
        json.dumps({
            'success':
            error_message is None and is_empty(failure_imports),
            'message':
            success_message,
            'error_message':
            error_message,
            'failure_imports':
            failure_imports
        }))
예제 #12
0
def all_datasenders(request):
    manager = get_database_manager(request)
    if request.method == 'POST':
        error_message, failure_imports, success, success_message = import_module.import_data(
            request, manager)
        all_data_senders = import_module.load_all_subjects_of_type(request)

        return HttpResponse(
            json.dumps({
                'success': success,
                'message': success_message,
                'error_message': error_message,
                'failure_imports': failure_imports,
                'all_data': all_data_senders
            }))
    all_data_senders = import_module.load_all_subjects_of_type(request)
    return render_to_response('entity/all_datasenders.html',
                              {'all_data': all_data_senders},
                              context_instance=RequestContext(request))
예제 #13
0
 def test_should_import_data_senders(self):
     file_name = "reporters.csv"
     request = Mock()
     request.GET = {'qqfile': file_name}
     request.raw_post_data = self.csv_data
     organization = Mock(spec=Organization)
     with patch("datawinners.utils.get_organization_from_manager") as get_organization_from_dbm_mock:
         get_organization_from_dbm_mock.return_value = Mock(return_value=organization)
         with patch("datawinners.entity.import_data.get_organization") as get_organization:
             mock = Mock( return_value=organization )
             mock.org_id = 'abc'
             get_organization.return_value = mock
             error_message, failure_imports, success_message, imported_entities = import_data(request=request,
                                                                                          manager=self.manager)
             self.assertEqual(4, get_entity_count_for_type(self.manager, entity_type="reporter"))
             self.assertEqual(4, len(imported_entities))
             self.assertEqual('reporter', imported_entities["r1"])
             self.assertEqual('reporter', imported_entities["r2"])
             self.assertEqual('reporter', imported_entities["r3"])
             self.assertEqual('reporter', imported_entities["r4"])
예제 #14
0
    def post(self, request, *args, **kwargs):
        manager = get_database_manager(request.user)
        error_message, failure_imports, success_message, successful_imports = import_module.import_data(
            request, manager, default_parser=XlsDatasenderParser)

        imported_data_senders = parse_successful_imports(successful_imports)
        self.update_activity_log(request, successful_imports)

        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'successful_imports':
                imported_data_senders
            }))
예제 #15
0
    def post(self, request, *args, **kwargs):
        parser_dict = {'.xls': XlsDatasenderParser, '.xlsx': XlsxDataSenderParser}
        manager = get_database_manager(request.user)
        file_extension = os.path.splitext(request.GET["qqfile"])[1]
        parser = parser_dict.get(file_extension, None)
        error_message, failure_imports, success_message, successful_imports = import_module.import_data(
            request, manager, default_parser=parser)

        imported_data_senders = parse_successful_imports(successful_imports)
        self._convert_anonymous_submissions_to_registered(imported_data_senders, manager)

        self.update_activity_log(request, successful_imports)

        return HttpResponse(json.dumps(
            {
                'success': error_message is None and is_empty(failure_imports),
                'message': success_message,
                'error_message': error_message,
                'failure_imports': failure_imports,
                'successful_imports': imported_data_senders
            }))
예제 #16
0
def registered_datasenders(request, project_id):
    manager = get_database_manager(request.user)
    questionnaire = Project.get(manager, project_id)
    project_links = get_project_link(questionnaire)
    dashboard_page = settings.HOME_PAGE + "?deleted=true"
    if questionnaire.is_void():
        return HttpResponseRedirect(dashboard_page)
    if request.method == 'GET':
        in_trial_mode = _in_trial_mode(request)
        is_open_survey_allowed = _is_pro_sms(
            request) and not questionnaire.xform
        is_open_survey = 'open' if questionnaire.is_open_survey else 'restricted'
        user_rep_id_name_dict = rep_id_name_dict_of_users(manager)
        return render_to_response(
            'project/registered_datasenders.html', {
                'project': questionnaire,
                'project_links': project_links,
                'questionnaire_code': questionnaire.form_code,
                'current_language': translation.get_language(),
                'is_quota_reached': is_quota_reached(request),
                'in_trial_mode': in_trial_mode,
                'is_open_survey_allowed': is_open_survey_allowed,
                'is_open_survey': is_open_survey,
                'user_dict': json.dumps(user_rep_id_name_dict)
            },
            context_instance=RequestContext(request))
    if request.method == 'POST':
        error_message, failure_imports, success_message, successful_imports = import_module.import_data(
            request,
            manager,
            default_parser=XlsDatasenderParser,
            is_datasender=True)
        imported_data_senders = parse_successful_imports(successful_imports)

        reporter_id_email_map = dict([
            (imported_datasender['id'], imported_datasender['email'])
            for imported_datasender in imported_data_senders
        ])
        org_id = request.user.get_profile().org_id
        create_web_users(org_id, reporter_id_email_map, request.LANGUAGE_CODE)

        imported_datasenders_ids = [
            imported_data_sender["id"]
            for imported_data_sender in imported_data_senders
        ]
        _add_imported_datasenders_to_project(imported_datasenders_ids, manager,
                                             questionnaire)
        convert_open_submissions_to_registered_submissions.delay(
            manager.database_name, imported_datasenders_ids)

        if len(imported_datasenders_ids):
            UserActivityLog().log(request,
                                  action=IMPORTED_DATA_SENDERS,
                                  detail=json.dumps(
                                      dict({
                                          "Unique ID":
                                          "[%s]" %
                                          ", ".join(imported_datasenders_ids)
                                      })),
                                  project=questionnaire.name)
        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'successful_imports':
                imported_data_senders
            }))
예제 #17
0
def import_subjects_from_project_wizard(request, form_code):
    manager = get_database_manager(request.user)
    error_message, failure_imports, success_message, short_code_subject_details_dict = import_module.import_data(
        request, manager, default_parser=XlsOrderedParser, form_code=form_code)
    subject_details = {}

    if len(short_code_subject_details_dict) != 0:
        detail_dict = dict()
        form_model = get_form_model_by_code(manager, form_code)
        entity_type = form_model.entity_type[0]
        short_codes = []
        for short_code in short_code_subject_details_dict.keys():
            short_codes.append(short_code)

        subject_details = _format_imported_subjects_datetime_field_to_str(
            form_model, short_code_subject_details_dict)
        detail_dict.update({entity_type: "[%s]" % ", ".join(short_codes)})
        UserActivityLog().log(request,
                              action=IMPORTED_IDENTIFICATION_NUMBER,
                              detail=json.dumps(detail_dict))

    return HttpResponse(
        json.dumps({
            'success':
            error_message is None and is_empty(failure_imports),
            'message':
            success_message,
            'error_message':
            error_message,
            'failure_imports':
            failure_imports,
            'successful_imports':
            subject_details,
        }))
 def post(self, request, *args, **kwargs):
     manager = get_database_manager(request.user)
     error_message, failure_imports, success_message, imported_datasenders, successful_imports = import_module.import_data(
         request,
         manager,
         default_parser=XlsDatasenderParser)
     imported_data_senders = _parse_successful_imports(successful_imports)
     imported_datasenders_ids = [imported_data_sender["id"] for imported_data_sender in imported_data_senders]
     if len(imported_datasenders_ids):
         UserActivityLog().log(request, action=IMPORTED_DATA_SENDERS,
                               detail=json.dumps(
                                   dict({"Unique ID": "[%s]" % ", ".join(imported_datasenders_ids)})))
     org_id = request.user.get_profile().org_id
     _add_imported_datasenders_to_trail_account(imported_data_senders, org_id)
     return HttpResponse(json.dumps(
         {
             'success': error_message is None and is_empty(failure_imports),
             'message': success_message,
             'error_message': error_message,
             'failure_imports': failure_imports,
             'successful_imports': imported_data_senders
         }))
def registered_datasenders(request, project_id):
    manager = get_database_manager(request.user)
    project, project_links = _get_project_and_project_link(manager, project_id)
    if request.method == 'GET':
        in_trial_mode = _in_trial_mode(request)
        user_rep_id_name_dict = rep_id_name_dict_of_users(manager)
        return render_to_response(
            'project/registered_datasenders.html', {
                'project': project,
                'project_links': project_links,
                'current_language': translation.get_language(),
                'is_quota_reached': is_quota_reached(request),
                'in_trial_mode': in_trial_mode,
                'user_dict': json.dumps(user_rep_id_name_dict)
            },
            context_instance=RequestContext(request))
    if request.method == 'POST':
        error_message, failure_imports, success_message, imported_entities, successful_imports = import_module.import_data(
            request, manager, default_parser=XlsDatasenderParser)
        imported_data_senders = _parse_successful_imports(successful_imports)
        imported_datasenders_ids = [
            imported_data_sender["id"]
            for imported_data_sender in imported_data_senders
        ]
        _add_imported_datasenders_to_project(imported_datasenders_ids, manager,
                                             project)

        if len(imported_datasenders_ids):
            UserActivityLog().log(request,
                                  action=IMPORTED_DATA_SENDERS,
                                  detail=json.dumps(
                                      dict({
                                          "Unique ID":
                                          "[%s]" %
                                          ", ".join(imported_datasenders_ids)
                                      })),
                                  project=project.name)
        org_id = request.user.get_profile().org_id
        _add_imported_datasenders_to_trail_account(imported_data_senders,
                                                   org_id)
        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'successful_imports':
                imported_data_senders
            }))
예제 #20
0
def import_subjects_from_project_wizard(request, form_code):
    manager = get_database_manager(request.user)
    error_message, failure_imports, success_message, imported_entities, successful_imports = import_module.import_data(
        request, manager, default_parser=XlsOrderedParser, form_code=form_code)
    if len(imported_entities) != 0:
        detail_dict = dict()
        for short_code, entity_type in imported_entities.items():
            entity_type = entity_type.capitalize()
            if detail_dict.get(entity_type) is not None:
                detail_dict.get(entity_type).append(short_code)
            else:
                detail_dict.update({entity_type: [short_code]})
        for key, detail in detail_dict.items():
            detail_dict.update({key: "[%s]" % ", ".join(detail)})
        UserActivityLog().log(request,
                              action=IMPORTED_SUBJECTS,
                              detail=json.dumps(detail_dict))

    subjects_data = import_module.load_all_subjects(manager)

    return HttpResponse(
        json.dumps({
            'success':
            error_message is None and is_empty(failure_imports),
            'message':
            success_message,
            'error_message':
            error_message,
            'failure_imports':
            failure_imports,
            'all_data':
            subjects_data,
            'imported':
            imported_entities.keys()
        }))
예제 #21
0
def all_datasenders(request):
    manager = get_database_manager(request.user)
    projects = get_all_projects(manager)
    fields, old_labels, codes = get_entity_type_fields(manager)
    in_trial_mode = utils.get_organization(request).in_trial_mode
    labels = []
    for label in old_labels:
        if label != "What is the mobile number associated with the subject?":
            labels.append(_(label.replace('subject', 'Data Sender')))
        else:
            labels.append(_("What is the Data Sender's mobile number?"))
    grant_web_access = False
    if request.method == 'GET' and int(request.GET.get('web', '0')):
        grant_web_access = True
    if request.method == 'POST':
        error_message, failure_imports, success_message, imported_datasenders = import_module.import_data(
            request, manager, default_parser=XlsDatasenderParser)
        if len(imported_datasenders.keys()):
            UserActivityLog().log(
                request,
                action=IMPORTED_DATA_SENDERS,
                detail=json.dumps(
                    dict({
                        "Unique ID":
                        "[%s]" % ", ".join(imported_datasenders.keys())
                    })))
        all_data_senders = _get_all_datasenders(manager, projects,
                                                request.user)
        mobile_number_index = fields.index('mobile_number')
        add_imported_data_sender_to_trial_organization(
            request,
            imported_datasenders,
            all_data_senders=all_data_senders,
            index=mobile_number_index)

        return HttpResponse(
            json.dumps({
                'success':
                error_message is None and is_empty(failure_imports),
                'message':
                success_message,
                'error_message':
                error_message,
                'failure_imports':
                failure_imports,
                'all_data':
                all_data_senders,
                'imported_datasenders':
                imported_datasenders
            }))

    all_data_senders = _get_all_datasenders(manager, projects, request.user)
    return render_to_response('entity/all_datasenders.html', {
        'all_data': all_data_senders,
        'projects': projects,
        'grant_web_access': grant_web_access,
        "labels": labels,
        'current_language': translation.get_language(),
        'in_trial_mode': in_trial_mode
    },
                              context_instance=RequestContext(request))
예제 #22
0
def import_subjects_from_project_wizard(request):
    manager = get_database_manager(request)
    error_message, failure_imports, success, success_message = import_module.import_data(request, manager)
    return HttpResponse(json.dumps({'success': success, 'message': success_message, 'error_message': error_message,
                                    'failure_imports': failure_imports}))