Пример #1
0
 def test_can_write_different_values(self):
     writer = UnicodeCSVWriter(open_file=io.StringIO())
     s = 'ünįcodē'
     rows = [
         [s, unicodeobj(s), 123,
          datetime.date.today()],
     ]
     writer.writerows(rows)
     self.assertRaises(TypeError, writer.writerows, [object()])
Пример #2
0
    def test_bom_write_with_open_file(self):
        csv_file = NamedTemporaryFile(delete=False)
        with open(csv_file.name, 'w') as open_file:
            writer = UnicodeCSVWriter(open_file=open_file, encoding="utf-8")
            s = 'ünįcodē'
            row = [s, 123, datetime.date.today()]
            writer.writerows([row])

        with open(csv_file.name, 'rb') as read_file:
            self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))

        # Clean up
        os.unlink(csv_file.name)
Пример #3
0
 def test_context_manager(self):
     tmp_file = NamedTemporaryFile()
     with UnicodeCSVWriter(filename=tmp_file.name) as writer:
         s = 'ünįcodē'
         rows = [
             [s, unicodeobj(s), 123,
              datetime.date.today()],
         ]
         writer.writerows(rows)
Пример #4
0
    def test_bom_not_written_for_other_encodings(self):
        csv_file = NamedTemporaryFile(delete=False)
        with UnicodeCSVWriter(filename=csv_file.name,
                              encoding="ascii") as writer:
            s = 'boring ascii'
            row = [s, 123, datetime.date.today()]
            writer.writerows([row])

        with open(csv_file.name, 'rb') as read_file:
            self.assertFalse(read_file.read().startswith(codecs.BOM_UTF8))

        # Clean up
        os.unlink(csv_file.name)
Пример #5
0
    def test_csv_write_output(self):
        tmp_file = NamedTemporaryFile(delete=False)
        with UnicodeCSVWriter(filename=tmp_file.name) as writer:
            s = 'ünįcodē'
            row = [s, 123, 'foo-bar']
            writer.writerows([row])

        with open(tmp_file.name, 'r') as read_file:
            content = smart_text(read_file.read(), encoding='utf-8').strip()
            self.assertEqual(content, 'ünįcodē,123,foo-bar')

        # Clean up
        os.unlink(tmp_file.name)
Пример #6
0
    def download_selected_orders(self, request, orders):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s' \
            % self.get_download_filename(request)
        writer = UnicodeCSVWriter(open_file=response)

        meta_data = (('number', _('Order number')),
                     ('value', _('Order value')),
                     ('date', _('Date of purchase')),
                     ('num_items', _('Number of items')),
                     ('status', _('Order status')),
                     ('customer', _('Customer email address')),
                     ('shipping_address_name', _('Deliver to name')),
                     ('billing_address_name', _('Bill to name')),
                     )
        columns = OrderedDict()
        for k, v in meta_data:
            columns[k] = v

        writer.writerow(columns.values())
        for order in orders:
            row = columns.copy()
            row['number'] = order.number
            row['value'] = order.total_incl_tax
            row['date'] = format_datetime(order.date_placed, 'DATETIME_FORMAT')
            row['num_items'] = order.num_items
            row['status'] = order.status
            row['customer'] = order.email
            if order.shipping_address:
                row['shipping_address_name'] = order.shipping_address.name
            else:
                row['shipping_address_name'] = ''
            if order.billing_address:
                row['billing_address_name'] = order.billing_address.name
            else:
                row['billing_address_name'] = ''
            writer.writerow(row.values())
        return response
Пример #7
0
 def get_csv_writer(self, file_handle, **kwargs):
     return UnicodeCSVWriter(open_file=file_handle, **kwargs)