def import_template(request, form_code): manager = get_database_manager(request.user) form_model = get_form_model_by_code(manager, form_code) if form_model.is_entity_registration_form(): form_fields = form_model.form_fields headers = get_subject_headers(form_fields) field_codes = _field_codes(form_fields) sheet_name = request.GET["filename"] else: form_fields = get_submission_form_fields_for_user(form_model, request) field_codes = _field_codes(form_fields) headers = get_submission_headers(form_fields, form_model) sheet_name = "Import_Submissions" filename = unquote(request.GET["filename"]) workbook_response_factory = WorkBookResponseFactory(form_code, filename, sheet_name) return workbook_response_factory.create_workbook_response([headers], field_codes)
def export_subject(request): manager = get_database_manager(request.user) query_text = request.POST.get('query_text', '') subject_type = request.POST.get('subject_type', '').lower() subject_list = SubjectQuery().query(request.user, subject_type, query_text) form_model = get_form_model_by_entity_type(manager, [subject_type.lower()]) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="%s.xls"' % ( subject_type, ) field_codes = form_model.field_codes() labels = get_subject_headers(form_model.form_fields) raw_data = [labels] for subject in subject_list: raw_data.append(subject) wb = get_excel_sheet(raw_data, subject_type) add_codes_sheet(wb, form_model.form_code, field_codes) wb.save(response) return response
def test_should_get_header_information_for_subject_excel(self): fields = [{"name": "first name", "code": 'q1', "label": 'What is your name', "type": "text"}, {"name": "age", "code": 'q2', "label": 'What is your age', "type": "integer", "constraints": [ [ "range", { "max": "15", "min": "12" } ] ]}, {"name": "reporting date", "code": 'q3', "label": 'What is the reporting date', "date_format": "dd.mm.yyyy", "type": "date"}, {"name": "eid", "code": 'eid', "label": 'What is the subject id', "type": "short_code"}, {"name": "location", "code": 'q4', "label": 'What is the location', "type": "list"}, {"name": "choices", "code": 'q5', "label": 'Your choices', "type": "select"}] form_model = MagicMock() form_model.form_fields = fields headers = get_subject_headers(form_model) headers_text = self._get_header_component(headers, 0) self.assertEqual( ["What is your name", "What is your age", "What is the reporting date", "What is the subject id", 'What is the location', "Your choices"], headers_text) header_instructions = self._get_header_component(headers, 1) self.assertEqual( ["\n\nAnswer must be a word", "\n\nEnter a number between 12-15.", "\n\nAnswer must be a date in the following format: day.month.year", "\n\nAssign a unique ID for each Identification Number.", '\n\nEnter name of the location.', "\n\nEnter 1 or more answers from the list."], header_instructions) header_examples = self._get_header_component(headers, 2) self.assertEqual( ["\n\n", "\n\n", "\n\nExample: 25.12.2011", "\n\nLeave this column blank if you want DataWinners to assign an ID for you.", '\n\nExample: Nairobi', "\n\nExample: a or ab"], header_examples)
def export_subject(request): manager = get_database_manager(request.user) query_text = request.POST.get('query_text', '') subject_type = request.POST.get('subject_type', '').lower() subject_list = SubjectQuery().query(request.user, subject_type, query_text) form_model = get_form_model_by_entity_type(manager, [subject_type.lower()]) response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="%s.xls"' % ( subject_type, ) field_codes = form_model.field_codes() field_codes.insert(0, form_model.form_code) labels = get_subject_headers(form_model.form_fields) raw_data = [] headers = OrderedDict([(subject_type, labels), ("codes", field_codes)]) for subject in subject_list: raw_data.append(subject) return export_to_new_excel(headers, {subject_type: raw_data}, subject_type, hide_codes_sheet=True)