===============================
 Exporting user defined fields
===============================

The user definded fields can be exported in the same way like other
fields.

Setup
=====

Create an addressbook:

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

Create some user defined fields:

>>> from icemac.addressbook.testing import create_field
>>> create_field(ab, 'icemac.addressbook.person.Person',
...     u'Bool', u'photo permission?')
>>> create_field(ab, 'icemac.addressbook.person.Person',
...     u'Datetime', u'last seen')
>>> create_field(ab, 'icemac.addressbook.address.PostalAddress',
...     u'Choice', u'state',
...     values=[u'Sachsen', u'Sachsen-Anhalt', u'Brandenburg'])
>>> create_field(ab, 'icemac.addressbook.address.PostalAddress',
...     u'Int', u'number of letters')
>>> create_field(ab, 'icemac.addressbook.address.PhoneNumber',
...     u'Decimal', u'cost per minute')
>>> create_field(ab, 'icemac.addressbook.address.PhoneNumber',
...     u'Text', u'mail box text')

Create some persons:

>>> from icemac.addressbook.testing import create
>>> import decimal
>>> import datetime
>>> liebig = create(
...     ab, ab, 'icemac.addressbook.person.Person', return_obj=True,
...     **{'last_name': u'Liebig', 'first_name': u'B.', 'Field-1': True,
...        'Field-2': datetime.datetime(2009, 11, 1, 14, 1)})
>>> create(ab, liebig, 'icemac.addressbook.address.PostalAddress',
...        set_as_default=True, **{'city': u'Testhausen',
...          'Field-3': u'Sachsen-Anhalt',  'Field-4': 3})
>>> create(ab, liebig, 'icemac.addressbook.address.PhoneNumber',
...        set_as_default=True, **{'number': u'01234-5678-90',
...          'Field-5': decimal.Decimal('1.43'),
...          'Field-6': 'I am not here, leave a message, beep.'})

>>> howitz = create(
...     ab, ab, 'icemac.addressbook.person.Person', return_obj=True,
...     **{'last_name': u'Howitz', 'Field-1': False})
>>> create(ab, howitz, 'icemac.addressbook.address.PostalAddress',
...        set_as_default=True, **{'city': u'Hettstedt',
...          'Field-3': u'Brandenburg'})

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

>>> create(ab, liebig, 'icemac.addressbook.address.PhoneNumber',
...        **{'Field-5': decimal.Decimal('1e2')})
>>> create(ab, liebig, 'icemac.addressbook.address.PhoneNumber',
...       **{'number': u'+49-3453-23434', 'Field-5': decimal.Decimal('28')})
>>> create(ab, howitz, 'icemac.addressbook.address.PostalAddress',
...        **{'zip': u'00001', 'Field-4': 123456})

Set the site so the user defined fields can be found:

>>> import zope.site.hooks
>>> old_site = zope.site.hooks.getSite()
>>> zope.site.hooks.setSite(ab)


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
>>> exporter = DefaultsExport()
>>> export = exporter.export(liebig, howitz)
>>> 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, 19)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'phone number',
 empty:'',
 empty:'',
 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'photo permission?',
 text:u'last seen',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'number of letters',
 text:u'state',
 text:u'number',
 text:u'cost per minute',
 text:u'mail box text',
 text:u'e-mail address',
 text:u'URL']
[text:u'B.',
 text:u'Liebig',
 empty:'',
 empty:'',
 empty:'',
 bool:1,
 xldate:40118.584027777775,
 empty:'',
 empty:'',
 text:u'Testhausen',
 empty:'',
 text:u'Germany',
 number:3.0,
 text:u'Sachsen-Anhalt',
 text:u'01234-5678-90',
 number:1.4299999999999999,
 text:u'I am not here, leave a message, beep.',
 empty:'',
 empty:'']
[empty:'',
 text:u'Howitz',
 empty:'',
 empty:'',
 empty:'',
 bool:0,
 empty:'',
 empty:'',
 empty:'',
 text:u'Hettstedt',
 empty:'',
 text:u'Germany',
 empty:'',
 text:u'Brandenburg',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'']


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
>>> exporter = CompleteExport()
>>> export = exporter.export(liebig, howitz)
>>> 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, 30)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'main postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'other postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'main phone number',
 empty:'',
 empty:'',
 text:u'other phone number',
 empty:'',
 empty:'',
 text:u'other phone number',
 empty:'',
 empty:'']
[text:u'first name',
 text:u'last name',
 text:u'birth date',
 text:u'keywords',
 text:u'notes',
 text:u'photo permission?',
 text:u'last seen',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'number of letters',
 text:u'state',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'number of letters',
 text:u'state',
 text:u'number',
 text:u'cost per minute',
 text:u'mail box text',
 text:u'number',
 text:u'cost per minute',
 text:u'mail box text',
 text:u'number',
 text:u'cost per minute',
 text:u'mail box text']
[text:u'B.',
 text:u'Liebig',
 empty:'',
 empty:'',
 empty:'',
 bool:1,
 xldate:40118.584027777775,
 empty:'',
 empty:'',
 text:u'Testhausen',
 empty:'',
 text:u'Germany',
 number:3.0,
 text:u'Sachsen-Anhalt',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'01234-5678-90',
 number:1.4299999999999999,
 text:u'I am not here, leave a message, beep.',
 empty:'',
 number:100.0,
 empty:'',
 text:u'+49-3453-23434',
 number:28.0,
 empty:'']
[empty:'',
 text:u'Howitz',
 empty:'',
 empty:'',
 empty:'',
 bool:0,
 empty:'',
 empty:'',
 empty:'',
 text:u'Hettstedt',
 empty:'',
 text:u'Germany',
 empty:'',
 text:u'Brandenburg',
 empty:'',
 empty:'',
 empty:'',
 text:u'00001',
 text:u'Germany',
 number:123456.0,
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'']


Tear down
=========

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