========
 Export
========

Search results can be exported in different formats.

Set up
======

Visitors are allowed to search and export, so we log in as a visitor:

>>> from icemac.addressbook.testing import Browser
>>> browser = Browser()
>>> browser.login('visitor')
>>> browser.open('http://localhost/++skin++AddressBook/ab')

Export
======

There is a link in the global navigation which leads to the searches,
we choose the keyword search as it has export abilities:

>>> browser.getLink('Search').click()
>>> browser.getLink('Keyword search').click()

Before searching no export button is displayed, only the search button
is shown:

>>> from icemac.addressbook.testing import get_submit_control_names
>>> get_submit_control_names(browser)
['form.buttons.search']

If there where no search results, there is no export button, too:

>>> browser.getControl('keywords').displayValue = ['work']
>>> browser.getControl('Search').click()
>>> get_submit_control_names(browser)
['form.buttons.search']

After performing the search a table containing the results is
displayed, the checkboxes of the rows are pre-selected:

>>> browser.getControl('keywords').displayValue = ['church']
>>> browser.getControl('Search').click()
>>> (get_submit_control_names(form=browser.getForm(index=0)),
...  get_submit_control_names(form=browser.getForm(index=1)))
(['form.buttons.search'], ['form.buttons.apply'])

>>> print browser.contents
<!DOCTYPE ...
<table>
  <thead>
    <tr>
      <th></th>
      <th>last name</th>
      <th>first name</th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-even-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-2" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-2">Koch</a></td>
      <td></td>
    </tr>
    <tr class="table-odd-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-4" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-4">Liebig</a></td>
      <td></td>
    </tr>
    <tr class="table-even-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-3" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-3">Velleuer</a></td>
       <td></td>
    </tr>
  </tbody>
</table>...

Choosing the export button returns an XLS document, all selected
persons are exported:

>>> import pprint
>>> import xlrd
>>> browser.getControl(name='form.buttons.apply').click()
>>> browser.headers['Content-Type']
'application/vnd.ms-excel'
>>> browser.headers['Content-Disposition']
'attachment; filename=addressbook_export.xls'
>>> xls_workbook = xlrd.open_workbook(file_contents=browser.contents)
>>> xls_workbook.sheet_names()
[u'Address book - Export']
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(5, 13)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'phone number',
 text:u'e-mail address',
 text:u'home page address']
[text:u'first name',
 text:u'last name',
 text:u'birth date',
 text:u'keywords',
 text:u'notes',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'number',
 text:u'e-mail address',
 text:u'URL']
[empty:'',
 text:u'Koch',
 xldate:19017.0,
 text:u'family, church',
 text:u'father-in-law',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'']
[empty:'',
 text:u'Liebig',
 empty:'',
 text:u'church',
 text:u'family',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'']
[empty:'',
 text:u'Velleuer',
 empty:'',
 text:u'family, church',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'']

When the user chooses only some persons for export only these persons
are exported:

>>> browser.goBack()
>>> browser.getControl(name='persons:list').getControl(value="Person-2").click()
>>> browser.getControl(name='form.buttons.apply').click()
>>> xls_workbook = xlrd.open_workbook(file_contents=browser.contents)
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(4, 13)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'phone number',
 text:u'e-mail address',
 text:u'home page address']
[text:u'first name',
 text:u'last name',
 text:u'birth date',
 text:u'keywords',
 text:u'notes',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'number',
 text:u'e-mail address',
 text:u'URL']
[empty:'',
 text:u'Liebig',
 empty:'',
 text:u'church',
 text:u'family',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'']
[empty:'',
 text:u'Velleuer',
 empty:'',
 text:u'family, church',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'']

When the user chooses no person for export a neary empty sheet gets
exported. As all persons are selected by default, it is necessary to
deselect them. We also test here another exporter:

>>> browser.goBack()
>>> browser.getControl(name='persons:list').getControl(value="Person-2").click()
>>> browser.getControl(name='persons:list').getControl(value="Person-3").click()
>>> browser.getControl(name='persons:list').getControl(value="Person-4").click()
>>> browser.getControl('XLS export complete').click()
>>> browser.getControl(name='form.buttons.apply').click()
>>> xls_workbook = xlrd.open_workbook(file_contents=browser.contents)
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(2, 5)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person', empty:'', empty:'', empty:'', empty:'']
[text:u'first name',
 text:u'last name',
 text:u'birth date',
 text:u'keywords',
 text:u'notes']
