def test_list_active_questionnaires(self): questionnaires = Questionnaires() list_survey = self.server.list_surveys(self.session_key, None) self.server.release_session_key(self.session_key) list_active_survey = [] for survey in list_survey: survey_has_token = questionnaires.survey_has_token_table(survey['sid']) if survey['active'] == "Y" and survey_has_token is True: list_active_survey.append(survey) self.assertEqual(questionnaires.find_all_active_questionnaires(), list_active_survey) questionnaires.release_session_key()
def survey_create(request, template_name="survey/survey_register.html"): survey_form = SurveyForm(request.POST or None, initial={ 'title': 'title', 'is_initial_evaluation': False }) surveys = Questionnaires() limesurvey_available = check_limesurvey_access(request, surveys) questionnaires_list = [] if limesurvey_available: questionnaires_list = surveys.find_all_active_questionnaires() surveys.release_session_key() if questionnaires_list: # removing surveys already registered used_surveys = Survey.objects.all() for used_survey in used_surveys: for questionnaire in questionnaires_list: if used_survey.lime_survey_id == questionnaire['sid']: questionnaires_list.remove(questionnaire) break else: messages.warning(request, _('No questionnaire found.')) if request.method == "POST": if request.POST['action'] == "save": if survey_form.is_valid(): survey_added = survey_form.save(commit=False) survey, created = Survey.objects.get_or_create( lime_survey_id=request.POST['questionnaire_selected'], is_initial_evaluation=survey_added.is_initial_evaluation) if created: messages.success(request, _('Questionnaire created successfully.')) redirect_url = reverse("survey_list") return HttpResponseRedirect(redirect_url) context = { "survey_form": survey_form, "creating": True, "editing": True, "questionnaires_list": questionnaires_list, 'limesurvey_available': limesurvey_available } return render(request, template_name, context)
def test_list_active_questionnaires(self): questionnaires = Questionnaires() list_survey = self.server.list_surveys(self.session_key, None) self.server.release_session_key(self.session_key) list_active_survey = [] for survey in list_survey: survey_has_token = questionnaires.survey_has_token_table( survey['sid']) if survey['active'] == "Y" and survey_has_token is True: list_active_survey.append(survey) self.assertEqual(questionnaires.find_all_active_questionnaires(), list_active_survey) questionnaires.release_session_key()
def survey_create(request, template_name="survey/survey_register.html"): survey_form = SurveyForm(request.POST or None, initial={'title': 'title', 'is_initial_evaluation': False}) surveys = Questionnaires() limesurvey_available = check_limesurvey_access(request, surveys) questionnaires_list = [] if request.method == "POST": if request.POST['action'] == "save": if survey_form.is_valid(): survey_added = survey_form.save(commit=False) survey, created = Survey.objects.get_or_create( lime_survey_id=request.POST['questionnaire_selected'], is_initial_evaluation=survey_added.is_initial_evaluation) surveys.release_session_key() if created: messages.success(request, _('Questionnaire created successfully.')) redirect_url = reverse("survey_list") return HttpResponseRedirect(redirect_url) if limesurvey_available: questionnaires_list = surveys.find_all_active_questionnaires() if questionnaires_list: # removing surveys already registered used_surveys = Survey.objects.all() for used_survey in used_surveys: for questionnaire in questionnaires_list: if used_survey.lime_survey_id == questionnaire['sid']: questionnaires_list.remove(questionnaire) break else: messages.warning(request, _('No questionnaire found.')) surveys.release_session_key() context = { "survey_form": survey_form, "creating": True, "editing": True, "questionnaires_list": questionnaires_list, 'limesurvey_available': limesurvey_available } return render(request, template_name, context)
def test_delete_participant_to_a_survey(self): """ Remove survey participant test testa a insercao de participante em um questionario """ surveys = Questionnaires() list_active_surveys = surveys.find_all_active_questionnaires() self.assertNotEqual(list_active_surveys, None) survey = list_active_surveys[0] sid = int(survey['sid']) # list_participants = self.server.list_participants(self.session_key, sid) # participant_data = {'email': '*****@*****.**', 'lastname': 'junqueira', 'firstname': 'juca'} participant_data_result = surveys.add_participant(sid) # verificar se info retornada eh a mesma # self.assertEqual(participant_data_result[0]['email'], participant_data['email']) # self.assertEqual(participant_data_result[0]['lastname'], participant_data['lastname']) # self.assertEqual(participant_data_result[0]['firstname'], participant_data['firstname']) self.assertNotEqual(participant_data_result, None) # list_participants_new = self.server.list_participants(self.session_key, sid) # self.assertEqual(len(list_participants_new), len(list_participants) + 1) # token_id = participant_data_result[0]['tid'] token_id = participant_data_result['tid'] # tokens_to_delete = [token_id] # remover participante do questionario result = surveys.delete_participant(sid, token_id) self.assertEqual(result[str(token_id)], 'Deleted') surveys.release_session_key()
def test_delete_participant_to_a_survey(self): """ Remove survey participant test testa a insercao de participante em um questionario """ surveys = Questionnaires() list_active_surveys = surveys.find_all_active_questionnaires() self.assertNotEqual(list_active_surveys, None) survey = list_active_surveys[0] sid = int(survey['sid']) # list_participants = self.server.list_participants(self.session_key, sid) # participant_data = {'email': '*****@*****.**', 'lastname': 'junqueira', 'firstname': 'juca'} participant_data_result = surveys.add_participant(sid) # verificar se info retornada eh a mesma # self.assertEqual(participant_data_result[0]['email'], participant_data['email']) # self.assertEqual(participant_data_result[0]['lastname'], participant_data['lastname']) # self.assertEqual(participant_data_result[0]['firstname'], participant_data['firstname']) self.assertNotEqual(participant_data_result, None) # list_participants_new = self.server.list_participants(self.session_key, sid) # self.assertEqual(len(list_participants_new), len(list_participants) + 1) # token_id = participant_data_result[0]['tid'] token_id = participant_data_result['tid'] # tokens_to_delete = [token_id] # remover participante do questionario result = surveys.delete_participant(sid, token_id) self.assertEqual(result[str(token_id)], 'Deleted') surveys.release_session_key()
def export_view(request, template_name="export/export_data.html"): export_form = ExportForm(request.POST or None, initial={'title': 'title', 'responses': ['short'], 'headings': 'code'}) # , 'per_participant': False, # 'per_questinnaire': False}) # export_form.per_participant = False # export_form.per_questionnaire = True # context = {} # test with pagination # a = [{"b": "2", "c": "3"}, {"d": "7", "e": "8"}] # b = [1, 2, 3, 4, 5] # c = [7, 9, (4, 3, 2)] # # contact_list = [a, b, c] # # paginator = Paginator(contact_list, 1) # Show 1 info per page # # page = request.GET.get('page') # try: # contacts = paginator.page(page) # except PageNotAnInteger: # # If page is not an integer, deliver first page. # page = 1 # contacts = paginator.page(1) # except EmptyPage: # # If page is out of range (e.g. 9999), deliver last page of results. # page = paginator.num_pages # contacts = paginator.page(paginator.num_pages) # page = 1 # # if page == 1: selected_ev_quest = [] selected_participant = [] selected_diagnosis = [] if request.method == "POST": questionnaires_selected_list = request.POST.getlist('to[]') questionnaires_list = [] # fields = {} previous_questionnaire_id = 0 output_list = [] for questionnaire in questionnaires_selected_list: sid, title, field, header = questionnaire.split("*") sid = int(sid) # transform to integer # # if sid not in fields: # fields[sid] = [] # fields[sid].append(field) if sid != previous_questionnaire_id: if previous_questionnaire_id != 0: output_list = [] questionnaires_list.append([sid, title, output_list]) previous_questionnaire_id = sid output_list.append((field, header)) # get participants list participant_selected_list = request.POST.getlist('patient_selected') participants_list = [] for participant in participant_selected_list: participants_list.append(participant.split("*")) # get diagnosis list diagnosis_selected_list = request.POST.getlist('diagnosis_selected') diagnosis_list = [] for diagnosis in diagnosis_selected_list: diagnosis_list.append(diagnosis.split("*")) selected_data_available = (len(questionnaires_selected_list) or len(participant_selected_list) or len(diagnosis_selected_list)) if selected_data_available: if export_form.is_valid(): print("valid data") per_participant = export_form.cleaned_data['per_participant'] per_questionnaire = export_form.cleaned_data['per_questionnaire'] heading_type = export_form.cleaned_data['headings'] responses_type = export_form.cleaned_data['responses'] questionnaires_list = update_questionnaire_list(questionnaires_list, heading_type, request.LANGUAGE_CODE) # insert participation_code update_participants_list(participants_list, heading_type) update_diagnosis_list(diagnosis_list, heading_type) # output_filename = # "/Users/sueli/PycharmProjects/nes/patientregistrationsystem/qdc/export/json_export_output2.json" # MEDIA_ROOT/export/username_id/export_id # input_export_file = create_initial_directory(request.user) export_instance = create_export_instance(request.user) input_export_file = path.join(EXPORT_DIRECTORY, path.join(str(request.user.id), path.join(str(export_instance.id), str(JSON_FILENAME)))) # copy data to .../media/export/<user_id>/<export_id>/ input_filename = path.join(settings.MEDIA_ROOT, input_export_file) create_directory(settings.MEDIA_ROOT, path.split(input_export_file)[0]) build_complete_export_structure(per_participant, per_questionnaire, participants_list, diagnosis_list, questionnaires_list, responses_type, heading_type, input_filename, request.LANGUAGE_CODE) complete_filename = export_create(request, export_instance.id, input_filename) if complete_filename: messages.success(request, _("Export was finished correctly")) # return file to the user # error_message = "a" # return_response = complete_filename # # redirect_url = reverse("export_result", args=(return_response, error_message)) # return HttpResponseRedirect(redirect_url ) print("antes do fim: httpResponse") zip_file = open(complete_filename, 'rb') response = HttpResponse(zip_file, content_type='application/zip') response['Content-Disposition'] = 'attachment; filename="export.zip"' response['Content-Length'] = path.getsize(complete_filename) return response else: messages.error(request, _("Export data was not generated.")) else: for questionnaire in questionnaires_list: for field in questionnaire[2]: # get output_list selected_ev_quest.append((questionnaire[0], field[0])) for participant in participants_list: selected_participant.append(participant[0]) for diagnosis in diagnosis_list: selected_diagnosis.append(diagnosis[0]) else: messages.error(request, _("No data was select. Export data was not generated.")) # else: # page 1 - list of questionnaires surveys = Questionnaires() limesurvey_available = check_limesurvey_access(request, surveys) questionnaires_list = [] if limesurvey_available: questionnaires_list = surveys.find_all_active_questionnaires() surveys.release_session_key() questionnaires_list_final = [] # removing surveys that are not entrance evaluation # entrance_evaluation_questionnaires = QuestionnaireResponse.objects.all() entrance_evaluation_questionnaire_ids_list = set(QuestionnaireResponse.objects.values_list('survey', flat=True)) # ev_questionnaire_ids_list = entrance_evaluation_questionnaires.values_list("survey") surveys_with_ev_list = Survey.objects.filter(id__in=entrance_evaluation_questionnaire_ids_list) for survey in surveys_with_ev_list: for questionnaire in questionnaires_list: if survey.lime_survey_id == questionnaire['sid']: questionnaires_list_final.append(questionnaire) break # page 2 fields # entrance evaluation questionnarie fields # if len(language_code) > 2: # language_code = "{}-{}".format(language_code[:2],language_code[-2:].upper()) questionnaires_fields_list = get_questionnaire_fields(questionnaires_list_final, request.LANGUAGE_CODE) if len(selected_ev_quest): questionnaire_ids, field_id = zip(*selected_ev_quest) else: questionnaire_ids = () for questionnaire in questionnaires_fields_list: questionnaire["selected_counter"] = questionnaire_ids.count(questionnaire["sid"]) for output_list in questionnaire["output_list"]: if (questionnaire["sid"], output_list["field"]) in selected_ev_quest: output_list["selected"] = True # for field in questionnaires_fields_list: # for questionnaire in questionnaires_list_final: # if field["sid"] == questionnaire['sid']: # field["title"] = questionnaire["surveyls_title"] # break context = { "limesurvey_available": limesurvey_available, "export_form": export_form, # "questionnaires_list": questionnaires_list_final, # "contacts": contacts, "patient_fields": patient_fields, "diagnosis_fields": diagnosis_fields, "questionnaires_fields_list": questionnaires_fields_list, "selected_ev_quest": selected_ev_quest, "selected_participant": selected_participant, "selected_diagnosis": selected_diagnosis, } # elif page == 2: return render(request, template_name, context)
def survey_create(request, template_name="survey/survey_register.html"): survey_form = SurveyForm(request.POST or None, initial={'title': 'title', 'is_initial_evaluation': False}) surveys = Questionnaires() limesurvey_available = check_limesurvey_access(request, surveys) questionnaires_list = [] if limesurvey_available: questionnaires_list = surveys.find_all_active_questionnaires() if questionnaires_list: # removing surveys already registered used_surveys = Survey.objects.all() for used_survey in used_surveys: for questionnaire in questionnaires_list: if used_survey.lime_survey_id == questionnaire['sid']: questionnaires_list.remove(questionnaire) break else: messages.warning(request, _('No questionnaire found.')) if request.method == "POST": if request.POST['action'] == "save": if survey_form.is_valid(): survey_added = survey_form.save(commit=False) survey, created = Survey.objects.get_or_create( lime_survey_id=request.POST['questionnaire_selected'], is_initial_evaluation=survey_added.is_initial_evaluation) has_file_upload_question = is_type_of_question_in_survey( surveys=surveys, survey=survey, type="|" ) surveys.release_session_key() if created: if has_file_upload_question: messages.warning(request, _('NES can\'t retrieve files from \"file upload\" ' 'questions from LimeSurvey.') + ' ' + _('See \"Best Pratices and Recommendations\" at ' 'https://nes.rtfd.io for more details.')) messages.success(request, _('Questionnaire created successfully.')) redirect_url = reverse("survey_list") return HttpResponseRedirect(redirect_url) surveys.release_session_key() context = { "survey_form": survey_form, "creating": True, "editing": True, "questionnaires_list": questionnaires_list, 'limesurvey_available': limesurvey_available } return render(request, template_name, context)