=======================
 Exporting person data
=======================

The addressbook can export the data of persons using its export
plug-ins.

Setup
=====

At first we have to create an addressbook, some keywords and some
persons with data:

>>> from icemac.addressbook.testing import (
...     create_addressbook, create_person, create_postal_address,
...     create_phone_number, create_email_address, create_home_page_address,
...     create_keyword)
>>> import zope.site.hooks
>>> import gocept.country.db
>>> import datetime
>>> addressbook = create_addressbook()

>>> church = create_keyword(addressbook, u'church')
>>> family = create_keyword(addressbook, u'family')

>>> liebig = create_person(
...     addressbook, addressbook, u'Liebig', first_name=u'B.',
...     birth_date=datetime.date(1980, 4, 21))
>>> old_site = zope.site.hooks.getSite()
>>> zope.site.hooks.setSite(addressbook)
>>> liebig.keywords = set([church, family])
>>> create_postal_address(
...     addressbook, liebig, zip=u'06333', city=u'Testhausen',
...     street=u'A-Street 1a')
<icemac.addressbook.address.PostalAddress object at 0x...>
>>> create_email_address(addressbook, liebig, email=u'b.liebig@example.com')
<icemac.addressbook.address.EMailAddress object at 0x...>
>>> create_phone_number(addressbook, liebig, number=u'01234-5678-90')
<icemac.addressbook.address.PhoneNumber object at 0x...>
>>> create_home_page_address(
...    addressbook, liebig, url='http://www.example.org')
<icemac.addressbook.address.HomePageAddress object at 0x...>

>>> howitz = create_person(
...     addressbook, addressbook, u'Howitz', first_name=u'Michael')
>>> create_postal_address(
...     addressbook, howitz, zip=u'06333', city=u'Hettstedt',
...     street=u'Eine-Straße 99x')
<icemac.addressbook.address.PostalAddress object at 0x...>

Let's create some additional phone numbers and postal addresses which
do not show up in defaults export but in complete export:

>>> create_phone_number(
...     addressbook, liebig, number=u'017612345678',
...     set_as_default=False, return_obj=False)
>>> create_phone_number(
...     addressbook, liebig, number=u'+49-3453-23434',
...     set_as_default=False, return_obj=False)
>>> create_postal_address(
...     addressbook, howitz, city=u'Beispieldorf', zip=u'00001',
...     set_as_default=False, return_obj=False)


XLS main adresses and numbers export
====================================

This exporter exports personal data, and the *main* adresses and
numbers as an XLS file:

>>> from icemac.addressbook.export.xls.simple import DefaultsExport
>>> import pprint
>>> import xlrd
>>> export = DefaultsExport([liebig, howitz]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> 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)
(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']
[text:u'B.',
 text:u'Liebig',
 xldate:29332.0,
 text:u'church, family',
 empty:'',
 empty:'',
 text:u'A-Street 1a',
 text:u'Testhausen',
 text:u'06333',
 text:u'Germany',
 text:u'01234-5678-90',
 text:u'b.liebig@example.com',
 text:u'http://www.example.org']
[text:u'Michael',
 text:u'Howitz',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Eine-Stra\xc3\x9fe 99x',
 text:u'Hettstedt',
 text:u'06333',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'']

Also works with nothing for export:

>>> export = DefaultsExport([]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> 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)
(2, 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']


XLS complete export
===================

This exporter exports personal data, and *all* addresses and numbers
as an XLS file:

>>> from icemac.addressbook.export.xls.simple import CompleteExport
>>> export = CompleteExport([liebig, howitz]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> 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)
(4, 20)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'main postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'other postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'main phone number',
 text:u'other phone number',
 text:u'other phone number',
 text:u'main e-mail address',
 text:u'main 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'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'number',
 text:u'number',
 text:u'number',
 text:u'e-mail address',
 text:u'URL']
[text:u'B.',
 text:u'Liebig',
 xldate:29332.0,
 text:u'church, family',
 empty:'',
 empty:'',
 text:u'A-Street 1a',
 text:u'Testhausen',
 text:u'06333',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'01234-5678-90',
 text:u'017612345678',
 text:u'+49-3453-23434',
 text:u'b.liebig@example.com',
 text:u'http://www.example.org']
[text:u'Michael',
 text:u'Howitz',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Eine-Stra\xc3\x9fe 99x',
 text:u'Hettstedt',
 text:u'06333',
 text:u'Germany',
 empty:'',
 empty:'',
 text:u'Beispieldorf',
 text:u'00001',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'']

Also works with nothing for export:

>>> export = CompleteExport([]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> 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)
(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']

Tear down
=========

>>> zope.site.hooks.setSite(old_site)
