================================
 Importing data having keywords
================================

Keywords are komma separated values inside a column. Not yet existing
keywords get created. Existing ones get used.

Set up
======

Create an addressbook:

>>> from icemac.addressbook.testing import create_addressbook
>>> from zope.testbrowser.testing import Browser
>>> ab = create_addressbook()

Create a browser:

>>> browser = Browser()
>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
>>> browser.open('http://localhost/++skin++AddressBook/ab')

Create a keyword first to show it gets used:

>>> from icemac.addressbook.testing import create_keyword
>>> create_keyword(ab, u'family', return_obj=False)

Create an import file:

>>> from StringIO import StringIO
>>> file_data = StringIO()
>>> file_data.write('last_name,xkeywords\n'
...                 'Lahn,"family, friends"\n'
...                 'Sieg," friends , company"\n'
...                 'Pech,\n')
>>> file_data.seek(0)


Importing data rows having keywords
===================================

The user has to upload a file and start the import:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Import data').click()
>>> browser.getLink('file').click()
>>> browser.getControl('file').add_file(file_data, 'text/plain', 'keyword.csv')
>>> browser.getControl('Add').click()
>>> browser.getLink('Import').click()
>>> browser.url
'http://localhost/++skin++AddressBook/ab/++attribute++importer/File/@@import'

The user chooses the reader as usual and maps the fields:

>>> browser.getControl('Next').click()
>>> print browser.url
http://localhost/++skin++AddressBook/ab/++attribute++importer/File/import/map
>>> browser.getControl('last name').displayValue = [
...     'last_name (Lahn, Sieg, Pech)']
>>> browser.getControl('keywords').displayValue = [
...     'xkeywords (family, friends, friends , company)']
>>> browser.getControl('Next').click()

The review step shows the result:

>>> print browser.url
http://localhost/.../ab/++attribute++importer/File/import/review
>>> print browser.contents
<!DOCTYPE ...
<td>family, friends</td>...
<td>friends, company</td>...

The import needs to be completed to view the results:

>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/.../ab/++attribute++importer/File/import/complete'
>>> browser.getControl('Complete').click()
>>> browser.url
'http://localhost/++skin++AddressBook/ab/++attribute++importer'

The new keywords have been created:

>>> from icemac.addressbook.interfaces import IKeywords
>>> import zope.component
>>> keywords = zope.component.getUtility(IKeywords, context=ab)
>>> [x.title for x in keywords.get_keywords()]
[u'company', u'family', u'friends']

They have been associated to the persons:

>>> browser.getLink('Person list').click()
>>> browser.getLink('Lahn').click()
>>> browser.getControl('keywords').displayValue
['family', 'friends']
>>> browser.getControl('Cancel').click()
>>> browser.getLink('Sieg').click()
>>> browser.getControl('keywords').displayValue
['company', 'friends']
>>> browser.getControl('Cancel').click()
>>> browser.getLink('Pech').click()
>>> browser.getControl('keywords').displayValue
[]


Do not keep data
================

The review step allows to discard the imported data, newly created
keywords get also deleted, but exisiting ones are kept.

Create an import file:

>>> from StringIO import StringIO
>>> file_data = StringIO()
>>> file_data.write('last_name,xkeywords\n'
...                 'Abc,"ccc, company"\n')
>>> file_data.seek(0)


The import is as usual:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Import data').click()
>>> browser.getLink('file').click()
>>> browser.getControl('file').add_file(file_data, 'text/plain', 'abc.csv')
>>> browser.getControl('Add').click()
>>> browser.getLink('Import').click()
>>> browser.url
'http://localhost/++skin++AddressBook/ab/++attribute++importer/File-2/@@import'

The user chooses the reader as usual and maps the fields:

>>> browser.getControl('Next').click()
>>> print browser.url
http://localhost/++skin++AddressBook/ab/++attribute++importer/File-2/import/map
>>> browser.getControl('last name').displayValue = ['last_name (Abc)']
>>> browser.getControl('keywords').displayValue = ['xkeywords (ccc, company)']
>>> browser.getControl('Next').click()

The review step shows the result and allows to discard the imported data

>>> print browser.url
http://localhost/.../ab/++attribute++importer/File-2/import/review
>>> print browser.contents
<!DOCTYPE ...
<td>company, ccc</td>...
>>> browser.getControl('Keep imported data?').displayValue = ['no']

Completing the import deletes the imported data and keywords:

>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/.../ab/++attribute++importer/File-2/import/complete'
>>> browser.getControl('Complete').click()
>>> browser.url
'http://localhost/++skin++AddressBook/ab/++attribute++importer'

No new keywords have been created:

>>> keywords = zope.component.getUtility(IKeywords, context=ab)
>>> [x.title for x in keywords.get_keywords()]
[u'company', u'family', u'friends']

No new persons have been created:

>>> from icemac.addressbook.interfaces import ITitle
>>> [ITitle(x) for x in ab.values()]
[u'Lahn', u'Sieg', u'Pech']
