def export_sms_details_to_excel(modeladmin, request, query_set): list = [] textSearchFilter = get_text_search_filter(request.GET, SMSAdmin.search_fields) adminPanelFilter = get_admin_panel_filter(request.GET) filteredSms = SMS.objects.all().filter( Q(**adminPanelFilter) & (textSearchFilter)) for sms in filteredSms: delivered_date_time = ExcelDate( datetime.combine(sms.delivered_at, datetime.min.time()), 'dd.mm.yyyy') if sms.delivered_at else None creation_date_time = ExcelDate( datetime.combine(sms.created_at, datetime.min.time()), 'dd.mm.yyyy') if sms.created_at else None list.append([ sms.organization_id, sms.status, creation_date_time, delivered_date_time, sms.msg_from, sms.msg_to, sms.msg_type, sms.message ]) headers = [ 'Organisation Id', 'Status', 'Creation Date', 'Delivery Date', 'Message from Number', 'Message to Number', 'Message Type', 'Content' ] response = export_to_new_excel(headers, list, 'sms_list') return response
def export_message_tracker_details_to_excel(modeladmin, request, query_set): headers = ["Organization Name", "Organization Id","Type", "Month", "Total Incoming Submissions (In total)", "Total Incoming Submissions", "Total SMS (incoming and outgoing)", "Outgoing Charged SMS: Total", "Outgoing SMS: Auto Reply", "Outgoing Charged SMS: Auto Reply", "Outgoing SMS: Reminders", "Outgoing Charged SMS: Reminders", "Outgoing SMS: Send Message", "Outgoing Charged SMS: Send Message", "Outgoing SMS: API", "Outgoing Charged SMS: API", "SMS Submissions", "SP Submissions", "Web Submissions", "SMS Subject Registration"] list = [] textSearchFilter = get_text_search_filter(request.GET,MessageTrackerAdmin.search_fields) adminPanelFilter = get_admin_panel_filter(request.GET) filteredSms = MessageTracker.objects.all().filter(Q(**adminPanelFilter) & (textSearchFilter)) for messageTracker in filteredSms: sms_tracker_month = ExcelDate(datetime.datetime.combine(messageTracker.month, datetime.datetime.min.time()), 'dd.mm.yyyy') if messageTracker.month else None list.append([modeladmin.organization_name(messageTracker), modeladmin.organization_id(messageTracker), modeladmin.type(messageTracker), sms_tracker_month, messageTracker.total_incoming_in_total(), messageTracker.total_monthly_incoming_messages(), messageTracker.total_messages(), messageTracker.outgoing_message_count(), messageTracker.outgoing_sms_count, messageTracker.outgoing_sms_charged_count, messageTracker.sent_reminders_count, messageTracker.sent_reminders_charged_count, messageTracker.send_message_count, messageTracker.send_message_charged_count, messageTracker.sms_api_usage_count, messageTracker.sms_api_usage_charged_count, modeladmin.sms_submission(messageTracker), messageTracker.incoming_sp_count, messageTracker.incoming_web_count, messageTracker.sms_registration_count]) response = export_to_new_excel(headers, list, 'tracker_list') return response
def _format_row(self, row): result = [] for field_code in self.columns.keys(): if self.columns[field_code].get("type") == "date" or field_code == "date": date_format = self.columns[field_code].get("format") py_date_format = DateField.DATE_DICTIONARY.get(date_format) or SUBMISSION_DATE_FORMAT_FOR_SUBMISSION try: col_val = ExcelDate(datetime.strptime(row[field_code], py_date_format), date_format or "submission_date") except Exception: col_val = row.get(field_code) or "" result.append(col_val) elif self.columns[field_code].get("type") == GEODCODE_FIELD_CODE: col_val = self._split_gps(row.get(field_code)) result.extend(col_val) elif self.columns[field_code].get("type") == 'select': value = row.get(field_code) col_val = ", ".join(value) if value else "" result.append(col_val) elif self.columns[field_code].get("type") == 'integer': col_val_parsed = try_parse(float, row.get(field_code)) result.append(col_val_parsed) else: col_val = row.get(field_code) result.append(col_val) return result
def test_use_default_format_when_provided_format_not_found(self): expected_date_string = 'Feb. 24, 2013, 12:45 PM' input_date = datetime.strptime(expected_date_string, '%b. %d, %Y, %H:%M %p') actual_date_string = ExcelDate(input_date, 'submission_date').date_as_string() self.assertEqual(expected_date_string, actual_date_string)
def test_should_give_back_excel_date_field_for_submission_date_type_question(self): columns = {'date': {'type': 'date', 'label':'what is submission date', 'format': "submission_date"}} date_1 = 'Dec. 09, 2013, 10:48 AM' submission_list = [{"_source": {'date': date_1}}] headers, values = SubmissionFormatter(columns, ('+', 0, 0)).format_tabular_data(submission_list) self.assertEquals(headers, ['what is submission date']) result_date = datetime.datetime.strptime(date_1, SUBMISSION_DATE_FORMAT_FOR_SUBMISSION) self.assertEquals(values, [[ExcelDate(result_date, "submission_date")]])
def test_should_give_back_excel_date_field_for_date_type_question(self): columns = {'form_id_q1': {'type': 'date', 'label':'what is date', 'format': 'dd.mm.yyyy'}} date_1 = '09.12.2013' submission_list = [{"_source": {'form_id_q1': date_1}}] headers, values = SubmissionFormatter(columns, ('+', 0, 0)).format_tabular_data(submission_list) self.assertEquals(headers, ['what is date']) result_date = datetime.datetime.strptime(date_1, '%d.%m.%Y') self.assertEquals(values, [[ExcelDate(result_date, 'dd.mm.yyyy')]])
def _format_date_field(self, field, field_code, result, row, columns): date_format = columns[field_code].get("format") date_value_str = row[field_code] try: if field_code == 'date': date_value = self._convert_to_localized_date_time(date_value_str) else: date_value = datetime.strptime(date_value_str, DateField.DATE_DICTIONARY.get(date_format)) col_val = ExcelDate(date_value, date_format or "submission_date") except ValueError: col_val = field or "" result.append(col_val)