def create_profile(request): manager = get_database_manager(request) entity_list = get_all_entity_types(manager) entity_list = helper.remove_reporter(entity_list) project_summary = dict(name='New Project') if request.method == 'GET': form = ProjectProfile(entity_list=entity_list,initial={'activity_report':'yes'}) return render_to_response('project/profile.html', {'form': form, 'project': project_summary, 'edit':False}, context_instance=RequestContext(request)) form = ProjectProfile(data=request.POST, entity_list=entity_list) if form.is_valid(): entity_type=form.cleaned_data['entity_type'] project = Project(name=form.cleaned_data["name"], goals=form.cleaned_data["goals"], project_type=form.cleaned_data['project_type'], entity_type=entity_type, devices=form.cleaned_data['devices'], activity_report=form.cleaned_data['activity_report'], sender_group=form.cleaned_data['sender_group']) form_model = helper.create_questionnaire(post=form.cleaned_data, dbm=manager) try: pid = project.save(manager) qid = form_model.save() project.qid = qid pid = project.save(manager) except DataObjectAlreadyExists as e: messages.error(request, e.message) return render_to_response('project/profile.html', {'form': form, 'project': project_summary, 'edit':False}, context_instance=RequestContext(request)) return HttpResponseRedirect(reverse(subjects_wizard, args=[pid])) else: return render_to_response('project/profile.html', {'form': form, 'project': project_summary, 'edit':False}, context_instance=RequestContext(request))
def create_profile(request): manager = get_database_manager(request) entity_list = get_all_entity_types(manager) entity_list = helper.remove_reporter(entity_list) project_summary = dict(name='New Project') if request.method == 'GET': form = ProjectProfile(entity_list=entity_list, initial={'activity_report': 'yes'}) return render_to_response('project/profile.html', { 'form': form, 'project': project_summary, 'edit': False }, context_instance=RequestContext(request)) form = ProjectProfile(data=request.POST, entity_list=entity_list) if form.is_valid(): entity_type = form.cleaned_data['entity_type'] project = Project(name=form.cleaned_data["name"], goals=form.cleaned_data["goals"], project_type=form.cleaned_data['project_type'], entity_type=entity_type, devices=form.cleaned_data['devices'], activity_report=form.cleaned_data['activity_report'], sender_group=form.cleaned_data['sender_group']) form_model = helper.create_questionnaire(post=form.cleaned_data, dbm=manager) try: pid = project.save(manager) qid = form_model.save() project.qid = qid pid = project.save(manager) except DataObjectAlreadyExists as e: messages.error(request, e.message) return render_to_response('project/profile.html', { 'form': form, 'project': project_summary, 'edit': False }, context_instance=RequestContext(request)) return HttpResponseRedirect(reverse(subjects_wizard, args=[pid])) else: return render_to_response('project/profile.html', { 'form': form, 'project': project_summary, 'edit': False }, context_instance=RequestContext(request))
def create_clinic_projects(CLINIC_ENTITY_TYPE, manager): name_type = create_data_dict(manager, name='Name', slug='Name', primitive_type='string') # Entity id is a default type in the system. entity_id_type = get_datadict_type_by_slug(manager, slug='entity_id') age_type = create_data_dict(manager, name='Age Type', slug='age', primitive_type='integer') date_type = create_data_dict(manager, name='Report Date', slug='date', primitive_type='date') select_type = create_data_dict(manager, name='Choice Type', slug='choice', primitive_type='select') geo_code_type = create_data_dict(manager, name='GeoCode Type', slug='geo_code', primitive_type='geocode') question1 = TextField(label="entity_question", code="EID", name="What is associated entity?", language="eng", entity_question_flag=True, ddtype=entity_id_type, length=TextConstraint(min=1, max=12)) question2 = TextField(label="Name", code="NA", name="What is your name?", length=TextConstraint(min=1, max=10), defaultValue="some default value", language="eng", ddtype=name_type) question3 = IntegerField(label="Father age", code="FA", name="What is age of father?", range=NumericConstraint(min=18, max=100), ddtype=age_type) question4 = DateField(label="Report date", code="RD", name="What is reporting date?", date_format="dd.mm.yyyy", ddtype=date_type) question5 = SelectField(label="Blood Group", code="BG", name="What is your blood group?", options=[("O+", "a"), ("O-", "b"), ("AB", "c"), ("B+", "d")], single_select_flag=True, ddtype=select_type) question6 = SelectField(label="Symptoms", code="SY", name="What are symptoms?", options=[("Rapid weight loss", "a"), ("Dry cough", "b"), ("Pneumonia", "c"), ("Memory loss", "d"), ("Neurological disorders ", "e")], single_select_flag=False, ddtype=select_type) question7 = GeoCodeField(name="What is the GPS code for clinic", code="GPS", label="What is the GPS code for clinic?", language="eng", ddtype=geo_code_type) form_model = FormModel(manager, name="AIDS", label="Aids form_model", form_code="cli001", type='survey', fields=[ question1, question2, question3, question4, question5, question6, question7 ], entity_type=CLINIC_ENTITY_TYPE) try: qid = form_model.save() except DataObjectAlreadyExists as e: get_form_model_by_code(manager, "cli001").delete() qid = form_model.save() project = Project(name="Clinic Test Project", goals="This project is for automation", project_type="survey", entity_type=CLINIC_ENTITY_TYPE[-1], devices=["sms"], activity_report='no', sender_group="close") project.qid = qid project.state = ProjectState.ACTIVE try: project.save(manager) except Exception: pass form_model2 = FormModel(manager, name="AIDS", label="Aids form_model", form_code="cli002", type='survey', fields=[ question1, question2, question3, question4, question5, question6, question7 ], entity_type=CLINIC_ENTITY_TYPE) try: qid2 = form_model2.save() except DataObjectAlreadyExists as e: get_form_model_by_code(manager, "cli002").delete() qid2 = form_model2.save() project2 = Project(name="Clinic2 Test Project", goals="This project is for automation", project_type="survey", entity_type=CLINIC_ENTITY_TYPE[-1], devices=["sms"], activity_report='no', sender_group="close") project2.qid = qid2 project2.state = ProjectState.ACTIVE try: project2.save(manager) except Exception: pass
def create_project(request): manager = get_database_manager(request.user) entity_list = get_all_entity_types(manager) entity_list = helper.remove_reporter(entity_list) all_projects_name = get_all_project_names(get_database_manager(request.user)) name = generate_project_name(all_projects_name) project_summary = dict(name=name) ngo_admin = NGOUserProfile.objects.get(user=request.user) if request.method == 'GET': form = CreateProject(entity_list=entity_list, initial={'name': name}) activity_report_questions = json.dumps(helper.get_activity_report_questions(manager), default=field_to_json) subject_report_questions = json.dumps(helper.get_subject_report_questions(manager), default=field_to_json) return render_to_response('project/create_project.html', {'form': form, "activity_report_questions": repr(activity_report_questions), 'subject_report_questions': repr(subject_report_questions), 'preview_links': get_preview_and_instruction_links(), 'existing_questions': repr(activity_report_questions), 'project': project_summary, 'questionnaire_code': helper.generate_questionnaire_code(manager), 'is_edit': 'false', 'post_url': reverse(create_project)}, context_instance=RequestContext(request)) if request.method == 'POST': project_info = json.loads(request.POST['profile_form']) form = CreateProject(entity_list, data=project_info) if form.is_valid(): project = Project(name=form.cleaned_data['name'], goals=form.cleaned_data['goals'], project_type='survey', entity_type=form.cleaned_data['entity_type'], activity_report=form.cleaned_data['activity_report'], state=request.POST['project_state'], devices=[u'sms', u'web', u'smartPhone'], language=form.cleaned_data['language']) if ngo_admin.reporter_id is not None: project.data_senders.append(ngo_admin.reporter_id) try: questionnaire = create_questionnaire(post=request.POST, manager=manager, entity_type=form.cleaned_data['entity_type'], name=form.cleaned_data['name'], language=form.cleaned_data['language']) except (QuestionCodeAlreadyExistsException, QuestionAlreadyExistsException, EntityQuestionAlreadyExistsException) as ex: return HttpResponse( json.dumps({'success': False, 'error_message': _(ex.message), 'error_in_project_section': False})) try: project.qid = questionnaire.save() except DataObjectAlreadyExists: return HttpResponse(json.dumps( {'success': False, 'error_message': "Questionnaire with this code already exists", 'error_in_project_section': False})) try: project.save(manager) UserActivityLog().log(request, action=CREATED_PROJECT, project=project.name, detail=project.name) except DataObjectAlreadyExists as ex: questionnaire.delete() message = _("%s with %s = %s already exists.") % (_(ex.data[2]), _(ex.data[0]), "'%s'" % project.name) return HttpResponse( json.dumps({'success': False, 'error_message': message, 'error_in_project_section': True})) return HttpResponse(json.dumps({'success': True, 'project_id': project.id}))
def create_clinic_projects(CLINIC_ENTITY_TYPE, manager): name_type = create_data_dict(manager, name='Name', slug='Name', primitive_type='string') # Entity id is a default type in the system. entity_id_type = get_datadict_type_by_slug(manager, slug='entity_id') age_type = create_data_dict(manager, name='Age Type', slug='age', primitive_type='integer') date_type = create_data_dict(manager, name='Report Date', slug='date', primitive_type='date') select_type = create_data_dict(manager, name='Choice Type', slug='choice', primitive_type='select') geo_code_type = create_data_dict(manager, name='GeoCode Type', slug='geo_code', primitive_type='geocode') question1 = TextField(label="entity_question", code="EID", name="What is associated entity?", language="eng", entity_question_flag=True, ddtype=entity_id_type, length=TextConstraint(min=1, max=12)) question2 = TextField(label="Name", code="NA", name="What is your name?", length=TextConstraint(min=1, max=10), defaultValue="some default value", language="eng", ddtype=name_type) question3 = IntegerField(label="Father age", code="FA", name="What is age of father?", range=NumericConstraint(min=18, max=100), ddtype=age_type) question4 = DateField(label="Report date", code="RD", name="What is reporting date?", date_format="dd.mm.yyyy", ddtype=date_type) question5 = SelectField(label="Blood Group", code="BG", name="What is your blood group?", options=[("O+", "a"), ("O-", "b"), ("AB", "c"), ("B+", "d")], single_select_flag=True, ddtype=select_type) question6 = SelectField(label="Symptoms", code="SY", name="What are symptoms?", options=[("Rapid weight loss", "a"), ("Dry cough", "b"), ("Pneumonia", "c"), ("Memory loss", "d"), ("Neurological disorders ", "e")], single_select_flag=False, ddtype=select_type) question7 = GeoCodeField(name="What is the GPS code for clinic", code="GPS", label="What is the GPS code for clinic?", language="eng", ddtype=geo_code_type) form_model = FormModel(manager, name="AIDS", label="Aids form_model", form_code="cli001", type='survey', fields=[question1, question2, question3, question4, question5, question6, question7], entity_type=CLINIC_ENTITY_TYPE ) try: qid = form_model.save() except DataObjectAlreadyExists as e: get_form_model_by_code(manager, "cli001").delete() qid = form_model.save() project = Project(name="Clinic Test Project", goals="This project is for automation", project_type="survey", entity_type=CLINIC_ENTITY_TYPE[-1], devices=["sms"], activity_report='no',sender_group="close") project.qid = qid project.state = ProjectState.ACTIVE try: project.save(manager) except Exception: pass form_model2 = FormModel(manager, name="AIDS", label="Aids form_model", form_code="cli002", type='survey', fields=[question1, question2, question3, question4, question5, question6, question7], entity_type=CLINIC_ENTITY_TYPE) try: qid2 = form_model2.save() except DataObjectAlreadyExists as e: get_form_model_by_code(manager, "cli002").delete() qid2 = form_model2.save() project2 = Project(name="Clinic2 Test Project", goals="This project is for automation", project_type="survey", entity_type=CLINIC_ENTITY_TYPE[-1], devices=["sms"], activity_report='no', sender_group="close") project2.qid = qid2 project2.state = ProjectState.ACTIVE try: project2.save(manager) except Exception: pass