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>
  ...<td><a href="http://localhost/myuniversity/faculties/TF">TF</a></td><td>Faculty of TestFac</td>
  ...

We can 'visit' each faculty by clicking on the appropriate link:

  >>> browser.getLink('TF').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Name of Faculty...
  ...TestFac...
  ...


Modifying faculties
-------------------

A faculty can directly be reached by its code:

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

We can change the settings for a faculty by clicking on the
provided 'settings' link:

  >>> browser.getLink('Settings').click()

Let's set a new title and save the form:

  >>> browser.getControl(name='form.title').value = "My test faculty"
  >>> browser.getControl(name='form.actions.save').click()

Clicking 'Save' we will stay on the settings form. So we can change
the department again. This time we will return to the overview page
afterwards:

  >>> browser.getControl(name='form.title').value = "My renamed faculty"
  >>> ctrl = browser.getControl("Save and return")
  >>> ctrl.click()

If we go to the settings page and click ``Cancel`` nothing will be
changed:

  >>> browser.getLink('Settings').click()
  >>> browser.getControl(name='form.title').value = "Blah"
  >>> browser.getControl('Cancel').click()

Our faculty was indeed renamed to ``My renamed faculty`` and not to
``Blah``:

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


Departments
===========

Adding departments
------------------

Departments are stored in :class:`IFaculty` instances with their code
as key. Faculties therefore are also department containers. Faculties
provides an add-form to add new departments:

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

We fill in a new name for our new department:

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

Furthermore we add a code (kind of abbreviation):

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

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

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

If we try to register a department under the same code twice we will
get an error:

  >>> browser.open('http://localhost/myuniversity/faculties/TF/add')
  >>> ctrl = browser.getControl(name='form.title')
  >>> ctrl.value = 'Another TestDept with same code'
  >>> browser.getControl(name='form.code').value = 'TD'
  >>> browser.getControl('Add department').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<div>The name chosen already exists in the database</div>
  ...

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

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

Afterwards, the department should be visible:

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


Modifying departments
---------------------

We can change the settings for a department by clicking on the
provided 'settings' link:

  >>> browser.open('http://localhost/myuniversity/faculties/TF/TD')
  >>> browser.getLink('Settings').click()

Let's set a new title and save the form:

  >>> browser.getControl(name='form.title').value = "My test dept"
  >>> browser.getControl(name='form.actions.save').click()

Clicking 'Save' we will stay on the settings form. So we can change
the department again. This time we will return to the overview page
afterwards:

  >>> browser.getControl(name='form.title').value = "My renamed dept"
  >>> ctrl = browser.getControl("Save and return")
  >>> ctrl.click()

If we go to the settings page and click ``Cancel`` nothing will be
changed:

  >>> browser.getLink('Settings').click()
  >>> browser.getControl(name='form.title').value = "Blah"
  >>> browser.getControl('Cancel').click()

Our department was indeed renamed to ``My renamed dept`` and not to
``Blah``:

  >>> browser.open('http://localhost/myuniversity/faculties/TF')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<h2>Registered Departments:</h2>
  ...<td><a href="http://localhost/myuniversity/faculties/TF/TD">TD</a></td><td>Department of My renamed dept</td>
  ...


Courses
=======

Once we have a department, we can add courses.

Adding courses
--------------

Courses are stored in :class:`ICourseContainer` instances with their
code as key. CourseContainers are normally availabe as `course`
attribute of :class:`waeup.university.department.Department`
instances.

To ease the life of users we do not require to browse the
coursecontainers (which have a rather flat user interface), but
provide adding of courses in department views.

Each department provides a ``Add course`` action button near top.

Departments provide an add-form to add new courses:

  >>> dept_url = 'http://localhost/myuniversity/faculties/TF/TD'
  >>> browser.open(dept_url + '/addcourse')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<span class="required">*</span><span>Title of course</span>
  ...

We fill in a name for our new course:

  >>> ctrl = browser.getControl(name='form.title')
  >>> ctrl.value = 'My Course 1'

Furthermore we add a code (kind of abbreviation):

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

This course will take place in the the first semester, so we set the
`semester` value to 1:

  >>> ctrl = browser.getControl(name='form.semester')
  >>> ctrl.options
  ['0', '1', '2', '3']

  >>> ctrl.displayOptions
  ['N/A', 'First Semester', 'Second Semester', 'Combined']

  >>> ctrl.value = ['1']

Finally, we create the course:

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

If we try to register a course under the same code twice we will
get an error:

  >>> browser.open(dept_url + '/addcourse')
  >>> ctrl = browser.getControl(name='form.title')
  >>> ctrl.value = 'Another course with same code'
  >>> browser.getControl(name='form.code').value = 'COURSE1'
  >>> browser.getControl('Add course').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<div>The code chosen already exists in the database</div>
  ...

Our course will be linked under the code on the department page:

  >>> browser.open(dept_url)
  >>> browser.getLink('COURSE1').click()
  >>> browser.url
  'http://localhost/myuniversity/faculties/TF/TD/courses/COURSE1'

Before we really add a course we can cancel the action and will be
redirected to the department page:

  >>> browser.open(dept_url + '/addcourse')
  >>> browser.getControl('Cancel').click()
  >>> browser.url
  'http://localhost/myuniversity/faculties/TF/TD'


Modifying courses
-----------------

We can change the settings for a course by clicking on the provided
'Edit settings' link:

  >>> browser.open(dept_url + '/courses/COURSE1')
  >>> browser.getLink('Edit settings').click()

When modifying a course, we cannot change the code any more:

  >>> browser.getControl(name='form.code')
  Traceback (most recent call last):
  ...
  LookupError: name 'form.code'

Let's set a new title and save the form:

  >>> browser.getControl(name='form.title').value = "My test course"
  >>> browser.getControl(name='form.actions.save').click()

Clicking 'Save' we will stay on the settings form. So we can change
the course again. This time we will return to the overview page
afterwards:

  >>> browser.getControl(name='form.title').value = "My renamed course" 
  >>> ctrl = browser.getControl("Save and return")
  >>> ctrl.click()

If we go to the settings page and click ``Cancel`` nothing will be
changed:

  >>> browser.getLink('Edit settings').click()
  >>> browser.getControl(name='form.title').value = "Blah"
  >>> browser.getControl('Cancel').click()

Our course was indeed renamed to ``My renamed course`` and not to
``Blah``:

  >>> browser.open('http://localhost/myuniversity/faculties/TF/TD')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<div name="thecoursetable">
  ...<td>My renamed course</td>...
  ...


Deleting courses
----------------

We can delete courses by browsing the containing department and
clicking on the appropriate 'Delete' button. As we have only one
course, there is only one 'Delete' button yet:

  >>> browser.open('http://localhost/myuniversity/faculties/TF/TD')
  >>> 'My renamed course' in browser.contents
  True

  >>> browser.getControl('Delete').click()

  >>> browser.open('http://localhost/myuniversity/faculties/TF/TD')
  >>> 'My renamed course' in browser.contents
  False


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)
  []


Uploading files
---------------

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']

We create and upload also a CSV file containing departments:

  >>> open('departments.csv', 'wb').write(
  ... """code,title,title_prefix,faculty_code
  ... LIT,Literature,Department,FA
  ... SOC,Sociology,Department,FA
  ... PHY,Physics,Department,FS
  ... INF,Informatics,Department,FS
  ... MAT,Math,Department,FS
  ... """)

  >>> browser.open('http://localhost/myuniversity/datacenter/upload')
  >>> browser.getControl(name='uploadfile:file').add_file(
  ...   cStringIO.StringIO(open('departments.csv', 'rb').read()),
  ...   'text/plain', 'mydepartments.csv')
  >>> browser.getControl(name='SUBMIT').click()

We create and upload also a CSV file containing courses:

  >>> open('courses.csv', 'wb').write(
  ... """code,level,title,passmark,credits,semester,faculty,department
  ... LI1,,Introduction to Literature I,40,2,1,FA,LIT
  ... LI2,,Introduction to Literature II,40,2,2,FA,LIT
  ... AN1,000,Analysis I,40,2,1,FS,MAT
  ... AN2,000,Analysis II,40,2,2,FS,MAT
  ... """)

  >>> browser.open('http://localhost/myuniversity/datacenter/upload')
  >>> browser.getControl(name='uploadfile:file').add_file(
  ...   cStringIO.StringIO(open('courses.csv', 'rb').read()),
  ...   'text/plain', 'mycourses.csv')
  >>> browser.getControl(name='SUBMIT').click()

We create and upload also a CSV file containing certificates:

  >>> open('certificates.csv', 'wb').write(
  ... """code,title,faculty_code,department_code,category,study_mode,end_level,m_prefix,max_pass,start_level,application_category
  ... LBA,BACHELOR OF LITERATURE,FA,LIT,UG,ug_ft,500,LIT,30,100,basic
  ... LMA,MASTER OF LITERATURE,FA,LIT,UG,ug_pt,500,LIT,30,100,cest
  ... DME,DIPLOMA OF MATH,FS,MAT,DP,dp_ft,200,DME,30,100,cest
  ... """)

  >>> browser.open('http://localhost/myuniversity/datacenter/upload')
  >>> browser.getControl(name='uploadfile:file').add_file(
  ...   cStringIO.StringIO(open('certificates.csv', 'rb').read()),
  ...   'text/plain', 'mycertificates.csv')
  >>> browser.getControl(name='SUBMIT').click()

We create and upload also a CSV file containing certificate courses:

  >>> open('certcourses.csv', 'wb').write(
  ... """code,faculty_code,department_code,certificate_code,level,core_or_elective
  ... LI1,FA,LIT,LBA,100,True
  ... LI2,FA,LIT,LBA,200,True
  ... """)

  >>> browser.open('http://localhost/myuniversity/datacenter/upload')
  >>> browser.getControl(name='uploadfile:file').add_file(
  ...   cStringIO.StringIO(open('certcourses.csv', 'rb').read()),
  ...   'text/plain', 'mycertcourses.csv')
  >>> browser.getControl(name='SUBMIT').click()


Importing a CSV file
--------------------

As the uploaded file conforms with what we expect from a faculty-CSV
file, we can import it. To do this we click the ``Import CSV data``
link:

  >>> browser.open('http://localhost/myuniversity/datacenter')
  >>> browser.getLink('Import CSV data').click()

Here we get all importable files listed:

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...The following files are available for import:...
  ...mycertcourses.csv...Certificate Course Importer...
  ...mycertificates.csv...Certificate Importer...
  ...mycourses.csv...Course Importer...
  ...mydepartments.csv...Department Importer...
  ...myfaculties.csv...Faculty Importer...
  ...

As we can see, the files are analyzed on-the-fly so the system can
tell us what kind of importer is available for each.

We do import the faculty file by clicking on ``Import!`` next to the
appropriate file:

  >>> facimport = browser.getControl('Import!', index=4)
  >>> facimport.click()

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Successfully imported: myfaculties.csv...
  ...

We just created two new faculties:

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

We can also import the department data:

  >>> browser.open('http://localhost/myuniversity/datacenter')
  >>> browser.getLink('Import CSV data').click()
  >>> browser.getControl('Import!', index=3).click()

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Successfully imported: mydepartments.csv...
  ...

We can also import the course data:

  >>> browser.open('http://localhost/myuniversity/datacenter')
  >>> browser.getLink('Import CSV data').click()
  >>> browser.getControl('Import!', index=2).click()

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Successfully imported: mycourses.csv...
  ...

We can also import the certificate data:

  >>> browser.open('http://localhost/myuniversity/datacenter')
  >>> browser.getLink('Import CSV data').click()
  >>> browser.getControl('Import!', index=1).click()

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Successfully imported: mycertificates.csv...
  ...

We can also import certificate courses:

  >>> browser.open('http://localhost/myuniversity/datacenter')
  >>> browser.getLink('Import CSV data').click()
  >>> browser.getControl('Import!', index=0).click()

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Successfully imported: mycertcourses.csv...
  ... 

Clean up:

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