Exemplo n.º 1
0
 def test_can_write_different_values(self):
     writer = UnicodeCSVWriter(open_file=io.StringIO())
     s = 'ünįcodē'
     rows = [
         [s, s, 123, datetime.date.today()],
     ]
     writer.writerows(rows)
     self.assertRaises(TypeError, writer.writerows, [object()])
Exemplo n.º 2
0
 def test_can_write_different_values(self):
     writer = UnicodeCSVWriter(open_file=cStringIO())
     s = u'ünįcodē'
     class unicodeobj(object):
         def __str__(self):
             return s
         def __unicode__(self):
             return s
     rows = [[s, unicodeobj(), 123, datetime.date.today()], ]
     writer.writerows(rows)
     self.assertRaises(TypeError, writer.writerows, [object()])
Exemplo n.º 3
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 = u'ü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)
Exemplo n.º 4
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 = u'ü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)
Exemplo n.º 5
0
    def test_can_write_different_values(self):
        writer = UnicodeCSVWriter(open_file=cStringIO())
        s = u'ünįcodē'

        class unicodeobj(object):
            def __str__(self):
                return s

            def __unicode__(self):
                return s

        rows = [
            [s, unicodeobj(), 123, datetime.date.today()],
        ]
        writer.writerows(rows)
        self.assertRaises(TypeError, writer.writerows, [object()])
Exemplo n.º 6
0
    def download_applications(self, request, table):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s' % self.get_download_filename(request)
        writer = UnicodeCSVWriter(open_file=response)

        def format_csv_cell(str_in):
            if not str_in:
                return '–'
            return strip_tags(str_in).replace('\n', '').strip()

        # Loop through each row in the table, strip out any HTMl, and write it to a CSV excluding the last column (actions).
        for row_raw in table.as_values():
            row_values = tuple(format_csv_cell(value) for value in row_raw)
            writer.writerow(row_values[:-1])

        return response
Exemplo n.º 7
0
 def test_context_manager(self):
     tmp_file = NamedTemporaryFile()
     with UnicodeCSVWriter(filename=tmp_file.name) as writer:
         s = 'ünįcodē'
         rows = [
             [s, s, 123, datetime.date.today()],
         ]
         writer.writerows(rows)
Exemplo n.º 8
0
    def download_selected_invoices(self, request, invoices):
        response = http.HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s' \
            % self.get_download_filename(request)
        writer = UnicodeCSVWriter(open_file=response)

        meta_data = (('number', _('Invoice number')),
                     ('value', _('Invoice value')),
                     ('date', _('Date of purchase')),
                     ('order_number', _('Order number')),
                     ('num_items', _('Number of items')),
                     ('status', _('Invoice status')),
                     ('customer', _('Customer email address')),
                     ('person_name', _('Pay as juristic person')),
                     ('person_vatin', _('Juristic person VAT number')),
                     ('person_code', _('Juristic person code (e.g. KPP in Russia)')),
                     )
        columns = SortedDict()
        for k, v in meta_data:
            columns[k] = v

        writer.writerow(columns.values())
        for invoice in invoices:
            row = columns.copy()
            row['number'] = invoice.number
            row['value'] = invoice.total_incl_tax
            row['date'] = format_datetime(invoice.date_created, 'DATETIME_FORMAT')
            row['order_number'] = invoice.order_number
            row['num_items'] = invoice.order.num_items
            row['status'] = invoice.status
            row['customer'] = invoice.order.email
            if invoice.person:
                row['person_name'] = invoice.person.name
                row['person_vatin'] = invoice.person.vatin
                row['person_code'] = invoice.person.reason_code
            else:
                row['person_name'] = '<none>'
                row['person_vatin'] = '<none>'
                row['person_code'] = '<none>'
            writer.writerow(row.values())
        return response
Exemplo n.º 9
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)
Exemplo n.º 10
0
    def test_csv_write_output(self):
        tmp_file = NamedTemporaryFile(delete=False)
        with UnicodeCSVWriter(filename=tmp_file.name) as writer:
            s = u'ü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, u'ünįcodē,123,foo-bar')

        # Clean up
        os.unlink(tmp_file.name)
Exemplo n.º 11
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 = SortedDict()
        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
Exemplo n.º 12
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 = SortedDict()
        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)
        return response
Exemplo n.º 13
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)

        writer.writerow(self.CSV_COLUMNS.values())
        for order in orders:
            row_values = self.get_row_values(order)
            writer.writerow([row_values.get(column, "") for column in self.CSV_COLUMNS])
        return response
Exemplo n.º 14
0
    def download_selected_invoices(self, request, invoices):
        response = http.HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s' \
            % self.get_download_filename(request)
        writer = UnicodeCSVWriter(open_file=response)

        meta_data = (
            ('number', _('Invoice number')),
            ('value', _('Invoice value')),
            ('date', _('Date of purchase')),
            ('order_number', _('Order number')),
            ('num_items', _('Number of items')),
            ('status', _('Invoice status')),
            ('customer', _('Customer email address')),
            ('person_name', _('Pay as juristic person')),
            ('person_vatin', _('Juristic person VAT number')),
            ('person_code', _('Juristic person code (e.g. KPP in Russia)')),
        )
        columns = SortedDict()
        for k, v in meta_data:
            columns[k] = v

        writer.writerow(columns.values())
        for invoice in invoices:
            row = columns.copy()
            row['number'] = invoice.number
            row['value'] = invoice.total_incl_tax
            row['date'] = format_datetime(invoice.date_created,
                                          'DATETIME_FORMAT')
            row['order_number'] = invoice.order_number
            row['num_items'] = invoice.order.num_items
            row['status'] = invoice.status
            row['customer'] = invoice.order.email
            if invoice.person:
                row['person_name'] = invoice.person.name
                row['person_vatin'] = invoice.person.vatin
                row['person_code'] = invoice.person.reason_code
            else:
                row['person_name'] = '<none>'
                row['person_vatin'] = '<none>'
                row['person_code'] = '<none>'
            writer.writerow(row.values())
        return response
Exemplo n.º 15
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')),
                     ('total_tax', _('Tax 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['total_tax'] = order.total_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
Exemplo n.º 16
0
 def get_csv_writer(self, file_handle, **kwargs):
     return UnicodeCSVWriter(open_file=file_handle, **kwargs)
Exemplo n.º 17
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()])