def create_type(request): success = False form = EntityTypeForm(request.POST) if form.is_valid(): entity_name = form.cleaned_data["entity_type_regex"] entity_name = [entity_name.strip().lower()] try: manager = get_database_manager(request.user) define_type(manager, entity_name) create_registration_form(manager, entity_name) message = _("Entity definition successful") success = True UserActivityLog().log(request, action=ADDED_SUBJECT_TYPE, detail=entity_name[0].capitalize()) except EntityTypeAlreadyDefined: if request.POST["referer"] == 'project': message = _( "%s already registered as a subject type. Please select %s from the drop down menu." ) % (entity_name[0], entity_name[0]) else: message = _("%s already registered as a subject type.") % ( entity_name[0], ) else: message = form.errors['entity_type_regex'] return HttpResponse(json.dumps({ 'success': success, 'message': _(message) }))
def create_type(request): success = False form = EntityTypeForm(request.POST) if form.is_valid(): entity_name = form.cleaned_data["entity_type_regex"] entity_name = [entity_name.strip().lower()] try: manager = get_database_manager(request.user) if entity_type_already_defined(manager, entity_name): raise EntityTypeAlreadyDefined(u"Type: %s is already defined" % u'.'.join(entity_name)) create_registration_form(manager, entity_name) define_type(manager, entity_name) message = _("Entity definition successful") success = True UserActivityLog().log(request, action=ADDED_IDENTIFICATION_NUMBER_TYPE, project=entity_name[0].capitalize()) except EntityTypeAlreadyDefined: message = _("%s already exists.") % (entity_name[0].capitalize(), ) else: message = form.errors['entity_type_regex'] return HttpResponse(json.dumps({ 'success': success, 'message': _(message) }))
def create_entity_types(manager, entity_types): for entity_type in entity_types: try: define_type(manager, entity_type) activate(DEFAULT_LANGUAGE) create_registration_form(manager, entity_type) except EntityTypeAlreadyDefined: pass
def test_should_save_created_registration_form_for_given_entity_name(self): with patch("datawinners.entity.helper._generate_form_code" ) as form_code_generator_mock: with patch("datawinners.entity.helper._create_registration_form" ) as registration_form_creator_mock: database_manager = Mock(name="dbm_mock") form_model_mock = Mock(name="form_model_mock") form_code_generator_mock.return_value = "form_code" registration_form_creator_mock.return_value = form_model_mock create_registration_form(database_manager, "entityname") form_model_mock.save.assert_called_with()
def _create_entities(self): self.entity_type = ['clinic'] define_type(self.manager, self.entity_type) create_registration_form(self.manager, self.entity_type) self.entity_type = ['waterpoint'] define_type(self.manager, self.entity_type) create_registration_form(self.manager, self.entity_type) define_type(self.manager, ['reporter']) self.name_type = DataDictType(self.manager, name='Name', slug='name', primitive_type='string') self.telephone_number_type = DataDictType(self.manager, name='telephone_number', slug='telephone_number', primitive_type='string') rep1 = create_entity(self.manager, ['reporter'], 'rep1') rep1.add_data(data=[(MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type)], submission=dict(submission_id="2"))
def test_should_generate_form_code_with_prefix_as_first_3_non_space_characters_of_entity_name( self): with patch("datawinners.entity.helper._generate_form_code" ) as form_code_generator_mock: with patch("datawinners.entity.helper._create_registration_form" ) as registration_form_creator_mock: database_manager = Mock(name="dbm_mock") form_model_mock = Mock(name="form_model_mock") form_code_generator_mock.return_value = "form_code" registration_form_creator_mock.return_value = form_model_mock create_registration_form(database_manager, "na me") form_code_generator_mock.assert_called_with( database_manager, "nam")
def migrate_01(managers, map_fun_raw_form_model_docs): failed_managers = [] for manager in managers: try: print manager.database print manager print 'running initializer' initializer.run(manager) print 'syncing form models' form_models = manager.database.query(map_fun_raw_form_model_docs) for form_model in form_models: document = form_model.key language = document['metadata']['activeLanguages'][0] if document['form_code'] == 'reg': document['metadata']['activeLanguages'] = ["en"] document['is_registration_model'] = True document["validators"] = [ {"cls": "mandatory"}, {"cls": "mobile_number_mandatory_for_reporter"}, {"cls": "at_least_one_location_field_must_be_answered_validator"} ] else: document['is_registration_model'] = False for index, json_field in enumerate(document['json_fields']): json_field['label'] = json_field['name'] document['json_fields'][index] = json_field document["validators"] = [{"cls": "mandatory"}] manager.database.save(document) print 'syncing entity reg forms' entity_types = get_all_entity_types(manager) # django.core.management.base forces the locale to en-us. translation.activate('en') for entity_type in entity_types: if entity_type != ['reporter']: create_registration_form(manager, entity_type) print "done for %s"%(manager,) except Exception as e: failed_managers.append((manager,e.message)) print 'failed managers if any' for manager,exception_mesage in failed_managers: print " %s failed. the reason : %s" %(manager,exception_mesage)
def test_should_return_created_registration_form(self): with patch("datawinners.entity.helper._generate_form_code" ) as form_code_generator_mock: with patch("datawinners.entity.helper._create_registration_form" ) as registration_form_creator_mock: database_manager = Mock(name="dbm_mock") form_model_mock = Mock(name="form_model_mock") form_code_generator_mock.return_value = "form_code" registration_form_creator_mock.return_value = form_model_mock actual_form_model = create_registration_form( database_manager, "entitytypename") self.assertEqual(actual_form_model, form_model_mock, "form_model not returned after creation")