Changeset 9763


Ignore:
Timestamp:
3 Dec 2012, 16:30:10 (12 years ago)
Author:
uli
Message:

Make the student specific parts of StudentsExportFilter? class attributes and provide an implementation for filtered students.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/export.py

    r9761 r9763  
    4343    Just an idea right now. There might be better ways.
    4444    """
    45 
    46     def __init__(self, session=None, level=None,
    47                  faculty_code=None, department_code=None):
    48         self.session = session
    49         self.level = level
    50         self.faculty_code = faculty_code
    51         self.department_code = department_code
     45    cat_name = 'students_catalog'
     46    default_params = dict(student_id=None)
     47
     48    def __init__(self, **kw):
     49        self._kw = dict(self.default_params)
     50        self._kw.update(kw)
    5251        return
    5352
    54     def get_students(self, catalog):
    55         students = catalog.searchResults(
    56             student_id=(None, None),
    57             current_session=(self.session, self.session),
    58             #level=(self.level, self.level), # would be nice
    59             faccode=(self.faculty_code, self.faculty_code),
    60             depcode=(self.department_code, self.department_code)
    61             )
    62         # filter out by level
    63         if self.level is not None:
    64             return [x for x in students
    65                     if x['studycourse'].current_level == self.level]
    66         return students
    67 
    68 def get_students(site, stud_filter=None):
     53    def query_catalog(self, catalog):
     54        query = dict([(idx_name, (value, value))
     55                     for idx_name, value in self._kw.items()])
     56        result = catalog.searchResults(**query)
     57        return result
     58
     59    def query(self, context=None):
     60        catalog = queryUtility(
     61            ICatalog, context=context, name=self.cat_name, default=None)
     62        if catalog is None:
     63            return []
     64        return self.query_catalog(catalog)
     65
     66def get_students(site, stud_filter=StudentsExportFilter()):
    6967    """Get all students registered in catalog in `site`.
    7068    """
    71     catalog = queryUtility(
    72         ICatalog, context=site, name='students_catalog', default=None)
    73     if catalog is None:
    74         return []
    75     if stud_filter is None:
    76         students = catalog.searchResults(student_id=(None, None))
    77     else:
    78         students = stud_filter.get_students(catalog)
    79     return students
     69    return stud_filter.query(context=site)
    8070
    8171def get_studycourses(students):
     
    140130        return self.close_outfile(filepath, outfile)
    141131
    142     def export_filtered(self, site, session, level, faculty_code=None,
    143                         department_code=None, filepath=None):
    144         pass
     132    def export_filtered(self, site, session, level, faccode=None,
     133                        depcode=None, filepath=None):
     134        """Export all students denoted by the given parameters.
     135        """
     136        filter_inst = StudentsExportFilter(
     137            current_session=session, current_level=level,
     138            faccode=faccode, depcode=depcode)
     139        result_set = get_students(site, stud_filter=filter_inst)
     140        return self.export(result_set, filepath=filepath)
    145141
    146142
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py

    r9761 r9763  
    4242    def test_get_students_by_session(self):
    4343        # we can filter out students of a certain session
    44         my_filter1 = StudentsExportFilter(session=2012)
     44        my_filter1 = StudentsExportFilter(current_session=2012)
    4545        result = get_students(self.app, stud_filter=my_filter1)
    4646        self.assertEqual(len(list(result)), 1)
    4747
    48         my_filter2 = StudentsExportFilter(session=1964)
     48        my_filter2 = StudentsExportFilter(current_session=1964)
    4949        result = get_students(self.app, stud_filter=my_filter2)
    5050        self.assertEqual(len(list(result)), 0)
     
    5353    def test_get_students_by_level(self):
    5454        # we can filter out students of a certain level
    55         my_filter1 = StudentsExportFilter(level=200)
     55        my_filter1 = StudentsExportFilter(current_level=200)
    5656        result = get_students(self.app, stud_filter=my_filter1)
    5757        self.assertEqual(len(list(result)), 1)
    5858
    59         my_filter2 = StudentsExportFilter(level=300)
     59        my_filter2 = StudentsExportFilter(current_level=300)
    6060        result = get_students(self.app, stud_filter=my_filter2)
    6161        self.assertEqual(len(list(result)), 0)
    6262        return
    6363
     64    def test_get_students_by_deptcode(self):
     65        # we can filter out students of a certain dept.
     66        my_filter1 = StudentsExportFilter(depcode='NA')
     67        result = get_students(self.app, stud_filter=my_filter1)
     68        self.assertEqual(len(list(result)), 1)
     69
     70        my_filter2 = StudentsExportFilter(depcode='NOTEXISTING')
     71        result = get_students(self.app, stud_filter=my_filter2)
     72        self.assertEqual(len(list(result)), 0)
     73        return
     74
     75    def test_get_students_by_faccode(self):
     76        # we can filter out students of a certain faculty.
     77        my_filter1 = StudentsExportFilter(faccode='NA')
     78        result = get_students(self.app, stud_filter=my_filter1)
     79        self.assertEqual(len(list(result)), 1)
     80
     81        my_filter2 = StudentsExportFilter(faccode='NOTEXISTING')
     82        result = get_students(self.app, stud_filter=my_filter2)
     83        self.assertEqual(len(list(result)), 0)
     84        return
     85
     86
    6487class StudentsExporterTest(StudentImportExportSetup):
    6588
    6689    layer = FunctionalLayer
     90
     91    std_csv_entry = (
     92        'my adm code,0,my clr code,1981-02-04#,anna@sample.com,,'
     93        'Anna,Tester,234,M.,NG,,"Studentroad 21\nLagos 123456\n",,'
     94        '+234-123-12345#,123,f,A111111,0,,,created'
     95        )
    6796
    6897    def setUp(self):
     
    144173            in result
    145174            )
     175        return
     176
     177    def test_export_filtered(self):
     178        # we can export a filtered set of students (filtered by session/level)
     179        self.setup_student(self.student)
     180        self.app['students'].addStudent(self.student)
     181        notify(grok.ObjectModifiedEvent(self.student))
     182        exporter = StudentsExporter()
     183
     184        exporter.export_filtered(self.app, None, None, filepath=self.outfile)
     185        result1 = open(self.outfile, 'rb').read()
     186        exporter.export_filtered(self.app, 2012, None, filepath=self.outfile)
     187        result2 = open(self.outfile, 'rb').read()
     188        exporter.export_filtered(self.app, None, 200, filepath=self.outfile)
     189        result3 = open(self.outfile, 'rb').read()
     190        exporter.export_filtered(self.app, 2011, None, filepath=self.outfile)
     191        result4 = open(self.outfile, 'rb').read()
     192        exporter.export_filtered(self.app, None, 100, filepath=self.outfile)
     193        result5 = open(self.outfile, 'rb').read()
     194        self.assertTrue(self.std_csv_entry in result1)
     195        self.assertTrue(self.std_csv_entry in result2)
     196        self.assertTrue(self.std_csv_entry in result3)
     197        self.assertTrue(self.std_csv_entry not in result4)
     198        self.assertTrue(self.std_csv_entry not in result5)
     199        return
     200
     201    def test_export_filtered_by_dept(self):
     202        # we can export a set of students filtered by department
     203        self.setup_student(self.student)
     204        self.app['students'].addStudent(self.student)
     205        notify(grok.ObjectModifiedEvent(self.student))
     206        exporter = StudentsExporter()
     207
     208        exporter.export_filtered(self.app, 2012, 200, depcode='NA',
     209                                 filepath=self.outfile)
     210        result1 = open(self.outfile, 'rb').read()
     211        exporter.export_filtered(self.app, 2012, 200, depcode='NODEPT',
     212                                 filepath=self.outfile)
     213        result2 = open(self.outfile, 'rb').read()
     214        self.assertTrue(self.std_csv_entry in result1)
     215        self.assertTrue(self.std_csv_entry not in result2)
     216        return
     217
     218    def test_export_filtered_by_faculty(self):
     219        # we can export a set of students filtered by faculty
     220        self.setup_student(self.student)
     221        self.app['students'].addStudent(self.student)
     222        notify(grok.ObjectModifiedEvent(self.student))
     223        exporter = StudentsExporter()
     224
     225        exporter.export_filtered(
     226            self.app, 2012, 200, faccode='NA', filepath=self.outfile)
     227        result1 = open(self.outfile, 'rb').read()
     228        exporter.export_filtered(self.app, 2012, 200, faccode='NOFAC',
     229                                 filepath=self.outfile)
     230        result2 = open(self.outfile, 'rb').read()
     231        self.assertTrue(self.std_csv_entry in result1)
     232        self.assertTrue(self.std_csv_entry not in result2)
    146233        return
    147234
Note: See TracChangeset for help on using the changeset viewer.