========================================
 Entity and field sort order in exports
========================================

The export reflects the sort order defined for entities and fields.

Set up
======

We need an address book which must be the current site:

>>> from icemac.addressbook.testing import create_addressbook
>>> ab = create_addressbook()
>>> import zope.site.hooks
>>> old_site = zope.site.hooks.getSite()
>>> zope.site.hooks.setSite(ab)


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

Default sort order
------------------

Using the default sort order, in the main addresses and numbers export the
columns are ordered the following way:

>>> from icemac.addressbook.export.xls.simple import DefaultsExport
>>> import pprint
>>> import xlrd
>>> export = DefaultsExport([]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=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']


The sort order can be changed using the entity order utility. When moving
the phone numer one position up, the sort order in the export changes
accordingly:

>>> import zope.component
>>> from icemac.addressbook.interfaces import IEntityOrder, IEntity, IPhoneNumber
>>> entity_order = zope.component.getUtility(IEntityOrder)
>>> phone_number_entity = IEntity(IPhoneNumber)
>>> entity_order.up(phone_number_entity, 1)

>>> export = DefaultsExport([]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> for rx in range(work_sheet_0.nrows):
...     pprint.pprint(work_sheet_0.row(rx))
[text:u'person',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'phone number',
 text:u'postal address',
 empty:'',
 empty:'',
 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'number',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'e-mail address',
 text:u'URL']


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

Set up
------

We need a new address book which must be the current site:

>>> from icemac.addressbook.testing import create_addressbook
>>> ab2 = create_addressbook(name="ab2")
>>> zope.site.hooks.setSite(ab2)

And a person wich some data:

>>> from icemac.addressbook.testing import (
...     create_full_person, create_phone_number)
>>> person = create_full_person(ab2, ab2, u'Tester')
>>> create_phone_number(ab2, person, set_as_default=False, return_obj=False)

Default sort order
------------------

Using the default sort order, in the main addresses and numbers export the
columns are ordered the following way:

>>> from icemac.addressbook.export.xls.simple import CompleteExport
>>> export = CompleteExport([person]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(3, 14)
>>> 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'main 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'number',
 text:u'number',
 text:u'e-mail address',
 text:u'URL']
[empty:'',
 text:u'Tester',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Germany',
 empty:'',
 empty:'',
 empty:'',
 empty:'']


The sort order can be changed using the entity order utility. When moving
the phone numer one position up, the sort order in the export changes
accordingly:

>>> import zope.component
>>> from icemac.addressbook.interfaces import IEntityOrder, IEntity, IPhoneNumber
>>> entity_order = zope.component.getUtility(IEntityOrder)
>>> phone_number_entity = IEntity(IPhoneNumber)
>>> entity_order.up(phone_number_entity, 1)

>>> export = CompleteExport([person]).export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> 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 phone number',
 text:u'other phone number',
 text:u'main postal address',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 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'number',
 text:u'number',
 text:u'address prefix',
 text:u'street',
 text:u'city',
 text:u'zip',
 text:u'country',
 text:u'e-mail address',
 text:u'URL']
[empty:'',
 text:u'Tester',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 empty:'',
 text:u'Germany',
 empty:'',
 empty:'']


Tear down
=========

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

