Ignore:
Timestamp:
2 Mar 2012, 14:20:03 (13 years ago)
Author:
uli
Message:

Add cert-exporter. Fix department code export for certs and courses.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/university
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/university/export.py

    r7752 r7753  
    139139                value = None
    140140        elif name == 'department_code':
    141             value = getattr(
    142                 getattr(context, '__parent__', None),
    143                 'code', None)
     141            try:
     142                value = context.__parent__.__parent__.code
     143            except AttributeError:
     144                value = None
    144145        return super(CourseExporter, self).mangle_value(
    145146            value, name, context)
     
    157158                    self.write_item(course, writer)
    158159        return self.close_outfile(filepath, outfile)
     160
     161class CertificateExporter(CourseExporter, grok.GlobalUtility):
     162    """Exporter for courses.
     163    """
     164    grok.implements(ICSVExporter)
     165    grok.name('certificates')
     166
     167    #: Fieldnames considered by this exporter
     168    fields = ('code', 'faculty_code', 'department_code', 'title', 'study_mode',
     169              'start_level', 'end_level', 'application_category')
     170
     171    def export_all(self, site, filepath=None):
     172        """Export faculties in facultycontainer into filepath as CSV data.
     173
     174        If `filepath` is ``None``, a raw string with CSV data is returned.
     175        """
     176        writer, outfile = self.get_csv_writer(filepath)
     177        faculties = site.get('faculties', {})
     178        for faculty in faculties.values():
     179            for department in faculty.values():
     180                for cert in department.certificates.values():
     181                    self.write_item(cert, writer)
     182        return self.close_outfile(filepath, outfile)
  • main/waeup.sirp/trunk/src/waeup/sirp/university/tests/test_export.py

    r7752 r7753  
    88from waeup.sirp.testing import SIRPUnitTestLayer
    99from waeup.sirp.university import (
    10     FacultiesContainer, Faculty, Department, Course,
     10    FacultiesContainer, Faculty, Department, Course, Certificate,
    1111    )
    1212from waeup.sirp.university.export import (
    1313    FacultyExporter, DepartmentExporter, CourseExporter,
     14    CertificateExporter,
    1415    )
    1516
     
    213214        self.course2 = Course('Advanced Cheese Making', 'C2')
    214215        self.course3 = Course('Selling Cheese', 'C3')
    215         self.dept1.courses[u'C1'] = self.course1
    216         self.dept1.courses['C2'] = self.course2
    217         self.dept2.courses[u'C3'] = self.course3
     216        self.dept1.courses.addCourse(self.course1)
     217        self.dept1.courses.addCourse(self.course2)
     218        self.dept2.courses.addCourse(self.course3)
    218219        return
    219220
     
    243244            result,
    244245            'code,faculty_code,department_code,title,credits,passmark,semester\r\n'
    245             'C1,F1,,Cheese Basics,0,40,1\r\n'
     246            'C1,F1,D1,Cheese Basics,0,40,1\r\n'
    246247            )
    247248        return
     
    254255            result,
    255256            'code,faculty_code,department_code,title,credits,passmark,semester\r\n'
    256             'C1,F1,,Cheese Basics,0,40,1\r\n'
    257             'C2,F1,,Advanced Cheese Making,0,40,1\r\n'
     257            'C1,F1,D1,Cheese Basics,0,40,1\r\n'
     258            'C2,F1,D1,Advanced Cheese Making,0,40,1\r\n'
    258259            )
    259260        return
     
    267268            result,
    268269            'code,faculty_code,department_code,title,credits,passmark,semester\r\n'
    269             'C1,F1,,Cheese Basics,0,40,1\r\n'
    270             'C2,F1,,Advanced Cheese Making,0,40,1\r\n'
    271             'C3,F1,,Selling Cheese,0,40,1\r\n'
     270            'C1,F1,D1,Cheese Basics,0,40,1\r\n'
     271            'C2,F1,D1,Advanced Cheese Making,0,40,1\r\n'
     272            'C3,F1,D2,Selling Cheese,0,40,1\r\n'
    272273            )
    273274        return
     
    280281            result,
    281282            'code,faculty_code,department_code,title,credits,passmark,semester\r\n'
    282             'C1,F1,,Cheese Basics,0,40,1\r\n'
    283             'C2,F1,,Advanced Cheese Making,0,40,1\r\n'
    284             'C3,F1,,Selling Cheese,0,40,1\r\n'
    285             )
    286         return
     283            'C1,F1,D1,Cheese Basics,0,40,1\r\n'
     284            'C2,F1,D1,Advanced Cheese Making,0,40,1\r\n'
     285            'C3,F1,D2,Selling Cheese,0,40,1\r\n'
     286            )
     287        return
     288
     289class CertificateExporterTest(unittest.TestCase):
     290    # Tests for CertificateExporter
     291
     292    layer = SIRPUnitTestLayer
     293
     294    def setUp(self):
     295        self.workdir = tempfile.mkdtemp()
     296        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     297        # create some departments and courses in a fake site
     298        container = FacultiesContainer()
     299        self.site = {'faculties':container}
     300        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
     301        container.addFaculty(self.fac)
     302        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
     303        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
     304        self.fac.addDepartment(self.dept1)
     305        self.fac.addDepartment(self.dept2)
     306        self.course1 = Course('Cheese Basics', 'C1')
     307        self.course2 = Course('Advanced Cheese Making', 'C2')
     308        self.course3 = Course('Selling Cheese', 'C3')
     309        self.dept1.courses.addCourse(self.course1)
     310        self.dept1.courses.addCourse(self.course2)
     311        self.dept2.courses.addCourse(self.course3)
     312        self.cert1 = Certificate(
     313            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
     314            end_level=300, application_category='basic')
     315        self.cert2 = Certificate(
     316            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
     317            end_level=700, application_category='cest')
     318        self.cert3 = Certificate(
     319            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
     320            end_level=200, application_category='no')
     321        self.dept1.certificates.addCertificate(self.cert1)
     322        self.dept1.certificates.addCertificate(self.cert2)
     323        self.dept2.certificates.addCertificate(self.cert3)
     324        return
     325
     326    def tearDown(self):
     327        shutil.rmtree(self.workdir)
     328        return
     329
     330    def test_ifaces(self):
     331        # make sure we fullfill interface contracts
     332        obj = CertificateExporter()
     333        verifyObject(ICSVExporter, obj)
     334        verifyClass(ICSVExporter, CertificateExporter)
     335        return
     336
     337    def test_get_as_utility(self):
     338        # we can get a certificate exporter as utility
     339        result = queryUtility(ICSVExporter, name="certificates")
     340        self.assertTrue(result is not None)
     341        return
     342
     343    def test_export(self):
     344        # we can export an iterable of certificates
     345        exporter = CertificateExporter()
     346        exporter.export([self.cert1], self.outfile)
     347        result = open(self.outfile, 'rb').read()
     348        self.assertEqual(
     349            result,
     350            'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n'
     351            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n'
     352            )
     353        return
     354
     355    def test_export_to_string(self):
     356        # we can export an iterable of certificates to a string.
     357        exporter = CertificateExporter()
     358        result = exporter.export([self.cert1, self.cert2], filepath=None)
     359        self.assertEqual(
     360            result,
     361            'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n'
     362            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n'
     363            'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest\r\n'
     364            )
     365        return
     366
     367    def test_export_all(self):
     368        # we can export all certificates in a site
     369        exporter = CertificateExporter()
     370        exporter.export_all(self.site, self.outfile)
     371        result = open(self.outfile, 'rb').read()
     372        self.assertEqual(
     373            result,
     374            'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n'
     375            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n'
     376            'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest\r\n'
     377            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,100,200,no\r\n'
     378            )
     379        return
     380
     381    def test_export_all_to_string(self):
     382        # we can export all certificates in a site to a string
     383        exporter = CertificateExporter()
     384        result = exporter.export_all(self.site, filepath=None)
     385        self.assertEqual(
     386            result,
     387            'code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category\r\n'
     388            'CERT1,F1,D1,Master of Cheese,ct_ft,100,300,basic\r\n'
     389            'CERT2,F1,D1,Master of Cheddar,ct_ft,400,700,cest\r\n'
     390            'CERT3,F1,D2,Cert. of Rubbish,dp_pt,100,200,no\r\n'
     391            )
     392        return
Note: See TracChangeset for help on using the changeset viewer.