Browsing the WAeUP portal
*************************

Here we visit all parts of a WAeUP portal using a browser.

:Test-Layer: functional

University
==========

We can watch universities in the browser.

To make sure, we can 'watch' pages, we first have to initialize out
test browser::

  >>> from zope.testbrowser.testing import Browser
  >>> browser = Browser()
  >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
  >>> browser.handleErrors = False

We create an university object and put into the ZODB root::

  >>> root = getRootFolder()
  >>> list(root)
  []

  >>> from waeup.app import University
  >>> u = University()
  >>> root['myuniversity'] = u
  >>> list(root)
  [u'myuniversity']


Let's get the default view of a university::

  >>> browser.open('http://localhost/myuniversity')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Welcome to the...
  ...

We can also get an edit view of a university::

  >>> browser.open('http://localhost/myuniversity/manage')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<form action="http://localhost/myuniversity/manage"
  ...

The edit form contains the default value for the university name::

  >>> 'Sample University' in browser.contents
  True

We can export a university as XML::

  >>> browser.open('http://localhost/myuniversity/export.xml')
  >>> print browser.contents
  <?xml version="1.0" encoding="utf-8" ?>
  <pickle>
  ...
  </pickle>

  >>> print browser.headers
  Status: 200 Ok
  Content-Length: ...
  Content-Type: text/xml; charset=UTF-8
  X-Powered-By: Zope (www.zope.org), Python (www.python.org)


Faculties
=========

Faculties are stored in a special container of `IUniversity`
instances. The container is called ``faculties`` and provides an
add-form to add new faculties::

  >>> browser.open('http://localhost/myuniversity/faculties/add')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<span class="required">*</span><span>Name of Faculty</span>
  ...

We fill in a new name for our new faculty::

  >>> ctrl = browser.getControl(name='form.title')
  >>> ctrl.value = 'TestFac'

Furthermore we add a prefix and a code (kind of abbreviation):

  >>> browser.getControl(name='form.code').value = 'TF'

Finally we click on 'Add Faculty' to add the new thing::

  >>> browser.getControl('Add faculty').click()

We can view a faculty by browsing a URL like this::

  >>> browser.open('http://localhost/myuniversity/faculties/TF')

Afterwards, the faculty should be visible:

  >>> browser.open('http://localhost/myuniversity/faculties')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<h2>Registered faculties:</h2>
  ...<a href="http://localhost/myuniversity/faculties/TF">TestFac</a>
  ...


Data Center
===========

The data center helps us uploading files for later import or similar.

  >>> browser.open('http://localhost/myuniversity')
  >>> browser.getLink('Data Center').click()

Setting the file path
---------------------

A datacenter stores files in a path in filesystem. By default this is
a directory in the sources:

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  ...
  Storage path: <span>/.../waeup/files</span>
  ...

Going to 'Settings` we can change the path:

  >>> browser.getLink('Settings').click()
  >>> pathsetting = browser.getControl(name='newpath')

We create a directory and set it as our upload path:

  >>> import os
  >>> cwd = os.getcwd()
  >>> uploadpath = os.path.join(cwd, 'testfiles')
  >>> os.mkdir(uploadpath)
  >>> pathsetting.value = uploadpath

And submit the form:

  >>> browser.getControl(name='save').click()

We clean up the set directory path, as there might be some files be
copied some files from installation:

  >>> files = os.listdir(uploadpath)
  >>> for filename in files:
  ...   os.unlink(os.path.join(uploadpath, filename))

The new upload directory is now empty:

  >>> os.listdir(uploadpath)
  []

Now we can upload files. Most interesting files might be CSV files,
that can be imported lateron. We create a CSV file containing faculty
descriptions:

  >>> open('faculties.csv', 'wb').write(
  ... """code,title,title_prefix
  ... FA,Arts,Faculty
  ... FS,Sciences,Faculty
  ... """)

Now we can upload this file. To do this, we first go to the upload
page:

  >>> browser.getLink('Upload data').click()

and enter the appropriate data in the form:

  >>> filewidget = browser.getControl(name='uploadfile:file')
  >>> filewidget
  <Control name='uploadfile:file' type='file'>
 
A sidenote for developers: by marking the filewidget with the
``:file`` extension, we tell Zope to handle this field as a file
widget.

  >>> import cStringIO
  >>> filecontents = cStringIO.StringIO(
  ...   open('faculties.csv', 'rb').read())
  >>> filewidget.add_file(filecontents, 'text/plain', 'myfaculties.csv')
  
  >>> browser.getControl(name='SUBMIT').click()

The file was indeed uploaded:

  >>> os.listdir(uploadpath)
  ['myfaculties.csv']

Clean up:

  >>> import os
  >>> import shutil
  >>> shutil.rmtree(uploadpath)
