=============================================
 Entity and field sort order in person forms
=============================================

The person add and edit form reflect the sort order defined for entities and
fields.

Set up
======

We need an address book:

>>> from icemac.addressbook.testing import create_addressbook
>>> from z3c.etestbrowser.wsgi import ExtendedTestBrowser as Browser
>>> from pprint import pprint
>>> import zope.site.hooks
>>> ab = create_addressbook()
>>> old_site = zope.site.hooks.getSite()

And a browser to access it:

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

And a helper function for the test:

>>> def pprint_form_fields(browser):
...     pprint([x.name.rsplit('.', 1)[1]
...             for x in browser.getForm().mech_form.controls])

Add form
========

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

Using the default sort order, the fieldsets are ordered the following way:

>>> browser.getLink('person').click()
>>> pprint_form_fields(browser)
['first_name',
 'last_name',
 'birth_date',
 'keywords:list',
 'keywords-empty-marker',
 'notes',
 'address_prefix',
 'street',
 'city',
 'zip',
 'country:list',
 'country-empty-marker',
 'number',
 'email',
 'url',
 'add',
 'cancel']

User definded entity sort order
-------------------------------

The sort order of the entities can be changed in the entities master data:

>>> from icemac.addressbook.testing import get_messages
>>> browser.getLink('Master data').click()
>>> browser.getLink('Entities').click()

When moving the postal address two positions up, the sort order in the person
add form changes accordingly:

>>> browser.getLink('up', index=2).click()
>>> get_messages(browser)
['Moved postal address up.']
>>> browser.getLink('up', index=1).click()
>>> get_messages(browser)
['Moved postal address up.']
>>> browser.getLink('Person list').click()
>>> browser.getLink('person').click()
>>> pprint_form_fields(browser)
['address_prefix',
 'street',
 'city',
 'zip',
 'country:list',
 'country-empty-marker',
 'first_name',
 'last_name',
 'birth_date',
 'keywords:list',
 'keywords-empty-marker',
 'notes',
 'number',
 'email',
 'url',
 'add',
 'cancel']

User definded field sort order
------------------------------

The sort order of fields of an entity can be changed on the entity itself:
When switching the first name and the last name of the person the person add
form changes accordingly:

>>> from icemac.addressbook.interfaces import IPerson, IEntity
>>> person_entity = IEntity(IPerson)
>>> zope.site.hooks.setSite(ab)
>>> person_entity.setFieldOrder(('last_name', 'first_name'))

>>> browser.getLink('Person list').click()
>>> browser.getLink('person').click()
>>> pprint_form_fields(browser)
['address_prefix',
 'street',
 'city',
 'zip',
 'country:list',
 'country-empty-marker',
 'last_name',
 'first_name',
 'birth_date',
 'keywords:list',
 'keywords-empty-marker',
 'notes',
 'number',
 'email',
 'url',
 'add',
 'cancel']


Edit form
=========

Set up
------

We need a separate address book to test the default sort order and a person
(and some data in it) to use the edit form:

>>> from icemac.addressbook.testing import create_full_person, create_file
>>> ab2 = create_addressbook('ab2')
>>> person = create_full_person(ab2, ab2, u'Tester')
>>> create_file(ab2, person, return_obj=False)
>>> browser.open('http://localhost/++skin++AddressBook/ab2')


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

Using the default sort order, the fieldsets are ordered the following way:


>>> browser.getLink('Tester').click()
>>> pprint_form_fields(browser)
['first_name',
 'last_name',
 'birth_date',
 'keywords:list',
 'keywords-empty-marker',
 'notes',
 'default_postal_address:list',
 'default_postal_address-empty-marker',
 'default_phone_number:list',
 'default_phone_number-empty-marker',
 'default_email_address:list',
 'default_email_address-empty-marker',
 'default_home_page_address:list',
 'default_home_page_address-empty-marker',
 'address_prefix',
 'street',
 'city',
 'zip',
 'country:list',
 'country-empty-marker',
 'number',
 'email',
 'url',
 'mimeType',
 'name',
 'data',
 'apply',
 'cancel',
 'clone_person',
 'delete_person',
 'delete_entry']

User defined entity sort order
------------------------------

The sort order of the entities can be changed in the entities master data:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Entities').click()

When moving the file object one position up and the person one position
down, the sort order in the person edit form changes accordingly:

>>> from icemac.addressbook.testing import get_messages
>>> browser.getLink('up', index=6).click()
>>> get_messages(browser)
['Moved file up.']
>>> browser.getLink('down', index=1).click()
>>> get_messages(browser)
['Moved person down.']
>>> browser.getLink('Person list').click()
>>> browser.getLink('Tester').click()
>>> pprint_form_fields(browser)
['default_postal_address:list',
 'default_postal_address-empty-marker',
 'default_phone_number:list',
 'default_phone_number-empty-marker',
 'default_email_address:list',
 'default_email_address-empty-marker',
 'default_home_page_address:list',
 'default_home_page_address-empty-marker',
 'first_name',
 'last_name',
 'birth_date',
 'keywords:list',
 'keywords-empty-marker',
 'notes',
 'address_prefix',
 'street',
 'city',
 'zip',
 'country:list',
 'country-empty-marker',
 'number',
 'email',
 'mimeType',
 'name',
 'data',
 'url',
 'apply',
 'cancel',
 'clone_person',
 'delete_person',
 'delete_entry']

User defined field sort order
-----------------------------

The sort order of fields of an entity can be changed on the entity itself:
When switching the address prefix and the street name of the postal address
the person edit form changes accordingly:

>>> from icemac.addressbook.interfaces import IPostalAddress, IEntity
>>> zope.site.hooks.setSite(ab2)
>>> person_entity = IEntity(IPostalAddress)
>>> person_entity.setFieldOrder(('street', 'address_prefix'))

>>> browser.getLink('Person list').click()
>>> browser.getLink('Tester').click()
>>> pprint_form_fields(browser)
['default_postal_address:list',
 'default_postal_address-empty-marker',
 'default_phone_number:list',
 'default_phone_number-empty-marker',
 'default_email_address:list',
 'default_email_address-empty-marker',
 'default_home_page_address:list',
 'default_home_page_address-empty-marker',
 'first_name',
 'last_name',
 'birth_date',
 'keywords:list',
 'keywords-empty-marker',
 'notes',
 'street',
 'address_prefix',
 'city',
 'zip',
 'country:list',
 'country-empty-marker',
 'number',
 'email',
 'mimeType',
 'name',
 'data',
 'url',
 'apply',
 'cancel',
 'clone_person',
 'delete_person',
 'delete_entry']



Edit form - main addresses and numbers
======================================

The `main addresses and numbers` sub form of the edit form also reflects
changes in the sort order.


Set up
------

We need a new address book to test the default sort order and a person to
use the edit form:

>>> ab3 = create_addressbook('ab3')
>>> person = create_full_person(ab3, ab3, u'Utzer')
>>> browser.open('http://localhost/++skin++AddressBook/ab3')


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

Using the default sort order, the fieldsets are ordered the following way:

>>> browser.getLink('Utzer').click()
>>> pprint_form_fields(browser)
[...
 'default_postal_address:list',
 'default_postal_address-empty-marker',
 'default_phone_number:list',
 'default_phone_number-empty-marker',
 'default_email_address:list',
 'default_email_address-empty-marker',
 'default_home_page_address:list',
 'default_home_page_address-empty-marker',
 ...

User defined sort order
-----------------------

The sort order can be changed in the entities master data:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Entities').click()

When moving the e-mail address one position up, the sort order in the `main
addresses and numbers` sub form changes accordingly:

>>> browser.getLink('up', index=4).click()
>>> get_messages(browser)
['Moved e-mail address up.']
>>> browser.getLink('Person list').click()
>>> browser.getLink('Utzer').click()
>>> pprint_form_fields(browser)
[...
 'default_postal_address:list',
 'default_postal_address-empty-marker',
 'default_email_address:list',
 'default_email_address-empty-marker',
 'default_phone_number:list',
 'default_phone_number-empty-marker',
 'default_home_page_address:list',
 'default_home_page_address-empty-marker',
 ...

Tear down
=========

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

