Exemplo n.º 1
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
Exemplo n.º 2
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()
Exemplo n.º 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()
Exemplo n.º 4
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
Exemplo n.º 5
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"]])
Exemplo n.º 6
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']])
Exemplo n.º 7
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'])
Exemplo n.º 8
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=';')
Exemplo n.º 9
0
 def export_csv(self, resource, context, form):
     columns = ['name', 'customer_id', 'workflow_state', 'total_pre_vat',
                'total_vat', 'total_price', 'total_paid', 'ctime']
     header = [MSG(u'Order ref.'), MSG(u'Customer ref.'), MSG(u'State'),
               MSG(u'Total VAT not inc.'), MSG(u'VAT'),
               MSG(u'Total VAT inc.'), MSG(u'Total paid'), MSG(u'Date')]
     header = [x.gettext().encode('utf-8') for x in header]
     csv = CSVFile()
     csv.add_row(header)
     lines = []
     site_root = resource.get_site_root()
     since = datetime.combine(form['since'], time(0,0))
     orders = context.root.search(AndQuery(PhraseQuery('is_order', True),
         get_base_path_query(site_root.get_canonical_path()),
         RangeQuery('ctime', since, None)))
     for brain in orders.get_documents(sort_by='ctime'):
         item_resource = resource.get_resource(brain.abspath)
         item = brain, item_resource
         row = []
         for c in columns:
             value = self.get_csv_value(resource, context, item, c)
             if isinstance(value, unicode):
                 value = value.encode('utf-8')
             else:
                 value = str(value)
             row.append(value)
         csv.add_row(row)
     separator = ','
     context.set_content_type('text/comma-separated-values')
     context.set_content_disposition('attachment; filename="Orders.csv"')
     return csv.to_str(separator=separator)
Exemplo n.º 10
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'])
Exemplo n.º 11
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']])
Exemplo n.º 12
0
 def test_get_row(self):
     handler = CSVFile(string=TEST_DATA_2)
     self.assertEqual(handler.get_row(1), ['four', 'five', 'six'])
Exemplo n.º 13
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"')
Exemplo n.º 14
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()
Exemplo n.º 15
0
    def action(self, resource, context, form):
        filename, mimetype, body = form['file']

        # Clean up f*ing Google export with no quote
        body = cleanup_gmail_csv(body)

        csv = CSVFile()
        csv.load_state_from_string(body)
        rows = csv.get_rows()

        # Decode header
        header = rows.next()

        root = context.root
        language = context.site_root.get_default_language()
        companies = resource.get_resource('companies')
        contacts = resource.get_resource('contacts')
        abspath = str(resource.get_canonical_path())
        base_path_query = get_base_path_query(abspath)

        contacts_added = {}
        contacts_updated = []
        companies_added = {}

        for row in rows:
            # Find company
            company_title = find_value_by_column(header, row, GMAIL_COMPANY)
            if company_title:
                company = None
                # FIXME the catalog should do this
                key = company_title.lower().translate(transmap)
                if key in companies_added:
                    # Already added
                    company = companies_added[key]
                else:
                    # Search existing company
                    query = AndQuery(base_path_query,
                                PhraseQuery('format', 'company'),
                                TextQuery('title', company_title))
                    results = root.search(query)
                    for document in results.get_documents():
                        # Found
                        company = root.get_resource(document.abspath)
                        break
                    else:
                        # Creating company
                        company = companies.add_company(
                                title={language: company_title})
                        companies_added[key] = company
            else:
                company = thingy(name=None)
            # Find contact by name and company if available
            contact = None
            firstname = find_value_by_column(header, row, GMAIL_FIRST_NAME)
            lastname = find_value_by_column(header, row, GMAIL_LAST_NAME)
            email = find_value_by_column(header, row, GMAIL_EMAIL)
            key = (firstname, lastname, email, company_title)
            if key in contacts_added:
                # Already added
                contact = contacts_added[key]
            else:
                # Search existing contact
                query = AndQuery(base_path_query,
                            PhraseQuery('format', 'contact'))
                if firstname:
                    query.append(TextQuery('crm_p_firstname', firstname))
                if lastname:
                    query.append(TextQuery('crm_p_lastname', lastname))
                if email:
                    query.append(TextQuery('crm_p_email', email))
                if company.name is not None:
                    query.append(PhraseQuery('crm_p_company', company.name))
                results = root.search(query)
                for document in results.get_documents():
                    # Found
                    contact = root.get_resource(document.abspath)
                    contacts_updated.append(contact)
                    break
                else:
                    # Creating contact
                    contact = contacts.add_contact(
                            crm_p_firstname=firstname,
                            crm_p_lastname=lastname,
                            crm_p_email=email,
                            crm_p_company=company.name,
                            crm_p_status='lead')
                    contacts_added[key] = contact
            # Update contact
            for title, name in import_columns.iteritems():
                if title in (GMAIL_FIRST_NAME, GMAIL_LAST_NAME, GMAIL_EMAIL,
                        GMAIL_COMPANY):
                    continue
                value = find_value_by_column(header, row, title)
                if value is not None:
                    datatype = contact.get_property_datatype(name)
                    if issubclass(datatype, String):
                        value = value.encode('utf8')
                    contact.set_property(name, value)

        message = []
        pattern = u'<a href="{0}">{1}</a>'
        if contacts_added:
            n = len(contacts_added)
            contacts_added = u", ".join(pattern.format(
                context.get_link(contact),
                XMLContent.encode(contact.get_title()))
                for contact in contacts_added.itervalues())
            message.append(MSG_CONTACTS_ADDED(n=n, added=contacts_added))
        if contacts_updated:
            n = len(contacts_updated)
            contacts_updated = u", ".join(pattern.format(
                context.get_link(contact),
                XMLContent.encode(contact.get_title()))
                for contact in contacts_updated)
            message.append(MSG_CONTACTS_UPDATED(n=n,
                updated=contacts_updated))
        if not message:
            message = ERR_NO_CONTACT_FOUND
        context.message = message
Exemplo n.º 16
0
 def test_del_rows(self):
     handler = CSVFile(string=TEST_DATA_2)
     handler.del_rows([0, 1])
     self.assertRaises(IndexError, handler.get_row, 0)
Exemplo n.º 17
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']])
Exemplo n.º 18
0
 def __init__(cls, columns, name):
     cls.columns = columns
     cls.csv = CSVFile()
Exemplo n.º 19
0
 def test_num_of_lines(self):
     handler = CSVFile(string=TEST_DATA_2)
     rows = list(handler.get_rows())
     self.assertEqual(len(rows), 3)
Exemplo n.º 20
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"]])
Exemplo n.º 21
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)
Exemplo n.º 22
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"')
Exemplo n.º 23
0
 def test_del_rows(self):
     handler = CSVFile(string=TEST_DATA_2)
     handler.del_rows([0, 1])
     self.assertRaises(IndexError, handler.get_row, 0)
Exemplo n.º 24
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'])
Exemplo n.º 25
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']])
Exemplo n.º 26
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)
Exemplo n.º 27
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'])
Exemplo n.º 28
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']])
Exemplo n.º 29
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)
Exemplo n.º 30
0
 def test_get_row(self):
     handler = CSVFile(string=TEST_DATA_2)
     self.assertEqual(handler.get_row(1), ['four', 'five', 'six'])
Exemplo n.º 31
0
 def test_num_of_lines(self):
     handler = CSVFile(string=TEST_DATA_2)
     rows = list(handler.get_rows())
     self.assertEqual(len(rows), 3)
Exemplo n.º 32
0
 def test_bad_syntax_csv_file(self):
     load_state = CSVFile().load_state_from_string
     self.assertRaises(ValueError, load_state, TEST_SYNTAX_ERROR)
Exemplo n.º 33
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']])
Exemplo n.º 34
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)