Пример #1
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"]])
Пример #2
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
Пример #3
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
Пример #4
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']])
Пример #5
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']])
Пример #6
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']])
Пример #7
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)
Пример #8
0
 def test_num_of_lines(self):
     handler = CSVFile(string=TEST_DATA_2)
     rows = list(handler.get_rows())
     self.assertEqual(len(rows), 3)
Пример #9
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"]])
Пример #10
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']])
Пример #11
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
Пример #12
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']])
Пример #13
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)
Пример #14
0
 def test_num_of_lines(self):
     handler = CSVFile(string=TEST_DATA_2)
     rows = list(handler.get_rows())
     self.assertEqual(len(rows), 3)
Пример #15
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']])