Index: /main/waeup.kofa/trunk/src/waeup/kofa/students/export.py
===================================================================
--- /main/waeup.kofa/trunk/src/waeup/kofa/students/export.py	(revision 9762)
+++ /main/waeup.kofa/trunk/src/waeup/kofa/students/export.py	(revision 9763)
@@ -43,39 +43,29 @@
     Just an idea right now. There might be better ways.
     """
-
-    def __init__(self, session=None, level=None,
-                 faculty_code=None, department_code=None):
-        self.session = session
-        self.level = level
-        self.faculty_code = faculty_code
-        self.department_code = department_code
+    cat_name = 'students_catalog'
+    default_params = dict(student_id=None)
+
+    def __init__(self, **kw):
+        self._kw = dict(self.default_params)
+        self._kw.update(kw)
         return
 
-    def get_students(self, catalog):
-        students = catalog.searchResults(
-            student_id=(None, None),
-            current_session=(self.session, self.session),
-            #level=(self.level, self.level), # would be nice
-            faccode=(self.faculty_code, self.faculty_code),
-            depcode=(self.department_code, self.department_code)
-            )
-        # filter out by level
-        if self.level is not None:
-            return [x for x in students
-                    if x['studycourse'].current_level == self.level]
-        return students
-
-def get_students(site, stud_filter=None):
+    def query_catalog(self, catalog):
+        query = dict([(idx_name, (value, value))
+                     for idx_name, value in self._kw.items()])
+        result = catalog.searchResults(**query)
+        return result
+
+    def query(self, context=None):
+        catalog = queryUtility(
+            ICatalog, context=context, name=self.cat_name, default=None)
+        if catalog is None:
+            return []
+        return self.query_catalog(catalog)
+
+def get_students(site, stud_filter=StudentsExportFilter()):
     """Get all students registered in catalog in `site`.
     """
-    catalog = queryUtility(
-        ICatalog, context=site, name='students_catalog', default=None)
-    if catalog is None:
-        return []
-    if stud_filter is None:
-        students = catalog.searchResults(student_id=(None, None))
-    else:
-        students = stud_filter.get_students(catalog)
-    return students
+    return stud_filter.query(context=site)
 
 def get_studycourses(students):
@@ -140,7 +130,13 @@
         return self.close_outfile(filepath, outfile)
 
-    def export_filtered(self, site, session, level, faculty_code=None,
-                        department_code=None, filepath=None):
-        pass
+    def export_filtered(self, site, session, level, faccode=None,
+                        depcode=None, filepath=None):
+        """Export all students denoted by the given parameters.
+        """
+        filter_inst = StudentsExportFilter(
+            current_session=session, current_level=level,
+            faccode=faccode, depcode=depcode)
+        result_set = get_students(site, stud_filter=filter_inst)
+        return self.export(result_set, filepath=filepath)
 
 
Index: /main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py
===================================================================
--- /main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py	(revision 9762)
+++ /main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py	(revision 9763)
@@ -42,9 +42,9 @@
     def test_get_students_by_session(self):
         # we can filter out students of a certain session
-        my_filter1 = StudentsExportFilter(session=2012)
+        my_filter1 = StudentsExportFilter(current_session=2012)
         result = get_students(self.app, stud_filter=my_filter1)
         self.assertEqual(len(list(result)), 1)
 
-        my_filter2 = StudentsExportFilter(session=1964)
+        my_filter2 = StudentsExportFilter(current_session=1964)
         result = get_students(self.app, stud_filter=my_filter2)
         self.assertEqual(len(list(result)), 0)
@@ -53,16 +53,45 @@
     def test_get_students_by_level(self):
         # we can filter out students of a certain level
-        my_filter1 = StudentsExportFilter(level=200)
+        my_filter1 = StudentsExportFilter(current_level=200)
         result = get_students(self.app, stud_filter=my_filter1)
         self.assertEqual(len(list(result)), 1)
 
-        my_filter2 = StudentsExportFilter(level=300)
+        my_filter2 = StudentsExportFilter(current_level=300)
         result = get_students(self.app, stud_filter=my_filter2)
         self.assertEqual(len(list(result)), 0)
         return
 
+    def test_get_students_by_deptcode(self):
+        # we can filter out students of a certain dept.
+        my_filter1 = StudentsExportFilter(depcode='NA')
+        result = get_students(self.app, stud_filter=my_filter1)
+        self.assertEqual(len(list(result)), 1)
+
+        my_filter2 = StudentsExportFilter(depcode='NOTEXISTING')
+        result = get_students(self.app, stud_filter=my_filter2)
+        self.assertEqual(len(list(result)), 0)
+        return
+
+    def test_get_students_by_faccode(self):
+        # we can filter out students of a certain faculty.
+        my_filter1 = StudentsExportFilter(faccode='NA')
+        result = get_students(self.app, stud_filter=my_filter1)
+        self.assertEqual(len(list(result)), 1)
+
+        my_filter2 = StudentsExportFilter(faccode='NOTEXISTING')
+        result = get_students(self.app, stud_filter=my_filter2)
+        self.assertEqual(len(list(result)), 0)
+        return
+
+
 class StudentsExporterTest(StudentImportExportSetup):
 
     layer = FunctionalLayer
+
+    std_csv_entry = (
+        'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
+        'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
+        '+234-123-12345#,123,f,A111111,0,,,created'
+        )
 
     def setUp(self):
@@ -144,4 +173,62 @@
             in result
             )
+        return
+
+    def test_export_filtered(self):
+        # we can export a filtered set of students (filtered by session/level)
+        self.setup_student(self.student)
+        self.app['students'].addStudent(self.student)
+        notify(grok.ObjectModifiedEvent(self.student))
+        exporter = StudentsExporter()
+
+        exporter.export_filtered(self.app, None, None, filepath=self.outfile)
+        result1 = open(self.outfile, 'rb').read()
+        exporter.export_filtered(self.app, 2012, None, filepath=self.outfile)
+        result2 = open(self.outfile, 'rb').read()
+        exporter.export_filtered(self.app, None, 200, filepath=self.outfile)
+        result3 = open(self.outfile, 'rb').read()
+        exporter.export_filtered(self.app, 2011, None, filepath=self.outfile)
+        result4 = open(self.outfile, 'rb').read()
+        exporter.export_filtered(self.app, None, 100, filepath=self.outfile)
+        result5 = open(self.outfile, 'rb').read()
+        self.assertTrue(self.std_csv_entry in result1)
+        self.assertTrue(self.std_csv_entry in result2)
+        self.assertTrue(self.std_csv_entry in result3)
+        self.assertTrue(self.std_csv_entry not in result4)
+        self.assertTrue(self.std_csv_entry not in result5)
+        return
+
+    def test_export_filtered_by_dept(self):
+        # we can export a set of students filtered by department
+        self.setup_student(self.student)
+        self.app['students'].addStudent(self.student)
+        notify(grok.ObjectModifiedEvent(self.student))
+        exporter = StudentsExporter()
+
+        exporter.export_filtered(self.app, 2012, 200, depcode='NA',
+                                 filepath=self.outfile)
+        result1 = open(self.outfile, 'rb').read()
+        exporter.export_filtered(self.app, 2012, 200, depcode='NODEPT',
+                                 filepath=self.outfile)
+        result2 = open(self.outfile, 'rb').read()
+        self.assertTrue(self.std_csv_entry in result1)
+        self.assertTrue(self.std_csv_entry not in result2)
+        return
+
+    def test_export_filtered_by_faculty(self):
+        # we can export a set of students filtered by faculty
+        self.setup_student(self.student)
+        self.app['students'].addStudent(self.student)
+        notify(grok.ObjectModifiedEvent(self.student))
+        exporter = StudentsExporter()
+
+        exporter.export_filtered(
+            self.app, 2012, 200, faccode='NA', filepath=self.outfile)
+        result1 = open(self.outfile, 'rb').read()
+        exporter.export_filtered(self.app, 2012, 200, faccode='NOFAC',
+                                 filepath=self.outfile)
+        result2 = open(self.outfile, 'rb').read()
+        self.assertTrue(self.std_csv_entry in result1)
+        self.assertTrue(self.std_csv_entry not in result2)
         return
 
