Пример #1
0
    def test_set_state_in_memory_resource(self):
        handler = CSVFile(string=TEST_DATA_2)
        handler.add_row(['a', 'b', 'c'])
        data = handler.to_str()

        handler2 = CSVFile(string=data)
        self.assertEqual(handler2.get_row(3), ['a', 'b', 'c'])
Пример #2
0
    def test_set_state_in_file_resource(self):
        handler = CSVFile('tests/test.csv')
        handler.add_row(['d1', 'e1', 'f1'])
        handler.save_state()

        handler2 = CSVFile('tests/test.csv')
        self.assertEqual(handler2.get_row(3), ['d1', 'e1', 'f1'])
        handler2.del_row(3)
        handler2.save_state()

        handler = CSVFile('tests/test.csv')
        self.assertEqual(handler.get_nrows(), 3)
Пример #3
0
 def GET(self, resource, context):
     search = context.root.search(format='user')
     columns = [
         'email', 'lastname', 'firstname', 'gender', 'ctime', 'last_time',
         'phone1', 'phone2'
     ]
     csv = CSVFile()
     from shop.user import ShopUser
     dynamic_schema = ShopUser.get_dynamic_schema()
     print dynamic_schema
     for brain in search.get_documents():
         user = context.root.get_resource(brain.abspath)
         row = []
         # Name
         row.append(user.name)
         row.append(str(user.get_property('is_enabled')))
         row.append(user.get_property('user_group'))
         # Base informations
         for column in columns:
             value = user.get_property(column)
             try:
                 row.append(value.encode('utf-8'))
             except:
                 pass
         csv.add_row(row)
     context.set_content_type('text/csv')
     context.set_content_disposition('attachment; filename=export.csv')
     return csv.to_str()
Пример #4
0
 def test_unicode(self):
     data = '"Martin von Löwis","Marc André Lemburg","Guido van Rossum"\n'
     handler = CSVFile(string=data)
     rows = list(handler.get_rows())
     self.assertEqual(
         rows,
         [["Martin von Löwis", "Marc André Lemburg", "Guido van Rossum"]])
Пример #5
0
 def GET(self, resource, context):
     encoding = 'cp1252'
     # FIXME
     #if not resource.is_ready():
     #    msg = MSG(u"Your form is not finished yet.")
     #    return context.come_back(msg, goto='/')
     # construct the csv
     csv = CSVFile()
     csv.add_row(["Chapitre du formulaire", "rubrique", "valeur"])
     schema = resource.get_schema()
     handler = resource.get_value('data')
     for name, datatype in sorted(schema.iteritems()):
         if name in ('ctime', 'mtime'):
             continue
         value = handler.get_value(name, schema)
         data = force_encode(value, datatype, encoding)
         if type(data) is not str:
             raise ValueError, str(type(datatype))
         csv.add_row([datatype.pages[0], name, data])
     # Return as CSV
     context.set_content_type('text/comma-separated-values')
     context.set_content_disposition('attachment',
                                     filename="%s.csv" % (resource.name))
     # Ok
     return csv.to_str(separator=';')
Пример #6
0
 def action_import_ods(self, resource, context, form):
     # Check if lpod is install ?
     if lpod_is_install is False:
         msg = ERROR(u'Please install LPOD')
         return context.come_back(msg)
     # Get models
     root = context.root
     shop = get_shop(resource)
     models = shop.get_resource('products-models').get_resources()
     # Open ODF file
     filename, mimetype, body = form['file']
     f = StringIO(body)
     document = odf_get_document(f)
     for table in document.get_body().get_tables():
         model_name = table.get_name()
         csv = CSVFile(string=table.to_csv())
         for row in csv.get_rows():
             reference = row[0]
             declination_name = row[1]
             stock = row[-3]
             price1 = row[-2]
             price2 = row[-1]
             product_brains = root.search(
                 reference=reference).get_documents()
             if len(product_brains) > 1:
                 print 'Reference %s %s' % (reference, len(product_brains))
                 continue
             product_brain = product_brains[0]
             product = root.get_resource(product_brain.abspath)
             declination = product.get_resource(declination_name)
             # Set change
             declination.set_property('stock-quantity', int(stock))
     context.message = MSG(u'Import has been done')
     return
Пример #7
0
 def test_load_state_without_schema(self):
     handler = CSVFile()
     handler.columns = ['name', 'url', 'number', 'date']
     handler.load_state_from_string(TEST_DATA_1)
     rows = list(handler.get_rows())
     self.assertEqual(
         rows, [["python", 'http://python.org/', '52343', '2003-10-23'],
                ["ruby", 'http://ruby-lang.org/', '42352', '2001-03-28']])
Пример #8
0
 def test_load_state_without_schema_and_columns(self):
     handler = CSVFile(string=TEST_DATA_1)
     rows = list(handler.get_rows())
     self.assertEqual(
         rows, [["python", 'http://python.org/', '52343', '2003-10-23'],
                ["ruby", 'http://ruby-lang.org/', '42352', '2001-03-28']])
Пример #9
0
 def test_del_rows(self):
     handler = CSVFile(string=TEST_DATA_2)
     handler.del_rows([0, 1])
     self.assertRaises(IndexError, handler.get_row, 0)
Пример #10
0
 def test_num_of_lines(self):
     handler = CSVFile(string=TEST_DATA_2)
     rows = list(handler.get_rows())
     self.assertEqual(len(rows), 3)
Пример #11
0
 def test_num_of_lines_with_last_new_line(self):
     data = TEST_DATA_2 + '\r\n'
     handler = CSVFile(string=data)
     rows = list(handler.get_rows())
     self.assertEqual(len(rows), 3)
Пример #12
0
 def test_bad_syntax_csv_file(self):
     load_state = CSVFile().load_state_from_string
     self.assertRaises(ValueError, load_state, TEST_SYNTAX_ERROR)
Пример #13
0
 def __init__(cls, columns, name):
     cls.columns = columns
     cls.csv = CSVFile()
Пример #14
0
 def test_to_str_without_schema(self):
     handler = CSVFile(string=TEST_DATA_1)
     self.assertEqual(
         handler.to_str(),
         u'"python","http://python.org/","52343","2003-10-23"\n'
         u'"ruby","http://ruby-lang.org/","42352","2001-03-28"')
Пример #15
0
 def test_get_row(self):
     handler = CSVFile(string=TEST_DATA_2)
     self.assertEqual(handler.get_row(1), ['four', 'five', 'six'])
Пример #16
0
 def to_csv(self):
     csv = CSVFile()
     for values in self.iter_values():
         row = (value.encode('utf_8') for value in values)
         csv.add_row(row)
     return csv.to_str()
Пример #17
0
 def test_add_row(self):
     handler = CSVFile(string=TEST_DATA_2)
     handler.add_row(['a', 'b', 'c'])
     self.assertEqual(handler.get_row(3), ['a', 'b', 'c'])
Пример #18
0
 def test_get_rows(self):
     handler = CSVFile(string=TEST_DATA_2)
     rows = list(handler.get_rows([0, 1]))
     self.assertEqual(rows,
                      [['one', 'two', 'three'], ['four', 'five', 'six']])