Changeset 7756


Ignore:
Timestamp:
3 Mar 2012, 03:50:29 (13 years ago)
Author:
uli
Message:

Add exporter for certcourses.

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

    r7753 r7756  
    1818        """Hook for mangling values in derived classes
    1919        """
    20         if isinstance(value, unicode):
     20        if isinstance(value, bool):
     21            value = value and '1' or '0'
     22        elif isinstance(value, unicode):
    2123            # CSV writers like byte streams better than unicode
    2224            value = value.encode('utf-8')
    23         if value is None:
     25        elif value is None:
    2426            # None is not really representable in CSV files
    2527            value = ''
     
    181183                    self.write_item(cert, writer)
    182184        return self.close_outfile(filepath, outfile)
     185
     186class CertificateCourseExporter(CourseExporter, grok.GlobalUtility):
     187    """Exporter for courses.
     188    """
     189    grok.implements(ICSVExporter)
     190    grok.name('certificate_courses')
     191
     192    #: Fieldnames considered by this exporter
     193    fields = ('course', 'faculty_code', 'department_code', 'certificate_code',
     194              'level', 'mandatory')
     195
     196    def mangle_value(self, value, name, context=None):
     197        """Hook for mangling values in derived classes
     198        """
     199        if name == 'faculty_code':
     200            try:
     201                value = context.__parent__.__parent__.__parent__.__parent__.code
     202            except AttributeError:
     203                value = None
     204        elif name == 'department_code':
     205            try:
     206                value = context.__parent__.__parent__.__parent__.code
     207            except AttributeError:
     208                value = None
     209        elif name == 'certificate_code':
     210            value = getattr(context, '__parent__', None)
     211            value = getattr(value, 'code', None)
     212        if name == 'course':
     213            value = getattr(value, 'code', None)
     214        return super(CourseExporter, self).mangle_value(
     215            value, name, context)
     216
     217    def export_all(self, site, filepath=None):
     218        """Export faculties in facultycontainer into filepath as CSV data.
     219
     220        If `filepath` is ``None``, a raw string with CSV data is returned.
     221        """
     222        writer, outfile = self.get_csv_writer(filepath)
     223        faculties = site.get('faculties', {})
     224        for faculty in faculties.values():
     225            for department in faculty.values():
     226                for cert in department.certificates.values():
     227                    for certref in cert.values():
     228                        self.write_item(certref, writer)
     229        return self.close_outfile(filepath, outfile)
  • main/waeup.sirp/trunk/src/waeup/sirp/university/tests/test_export.py

    r7753 r7756  
    1212from waeup.sirp.university.export import (
    1313    FacultyExporter, DepartmentExporter, CourseExporter,
    14     CertificateExporter,
     14    CertificateExporter, CertificateCourseExporter,
    1515    )
    1616
     
    391391            )
    392392        return
     393
     394class CertificateCourseExporterTest(unittest.TestCase):
     395    # Tests for CertificateCourseExporter
     396
     397    layer = SIRPUnitTestLayer
     398
     399    def setUp(self):
     400        self.workdir = tempfile.mkdtemp()
     401        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     402        # create some departments and courses in a fake site
     403        container = FacultiesContainer()
     404        self.site = {'faculties':container}
     405        self.fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
     406        container.addFaculty(self.fac)
     407        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
     408        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
     409        self.fac.addDepartment(self.dept1)
     410        self.fac.addDepartment(self.dept2)
     411        self.course1 = Course('Cheese Basics', 'C1')
     412        self.course2 = Course('Advanced Cheese Making', 'C2')
     413        self.course3 = Course('Selling Cheese', 'C3')
     414        self.dept1.courses.addCourse(self.course1)
     415        self.dept1.courses.addCourse(self.course2)
     416        self.dept2.courses.addCourse(self.course3)
     417        self.cert1 = Certificate(
     418            'CERT1', 'Master of Cheese', study_mode=u'ct_ft', start_level=100,
     419            end_level=300, application_category='basic')
     420        self.cert2 = Certificate(
     421            'CERT2', 'Master of Cheddar', study_mode='ct_ft', start_level=400,
     422            end_level=700, application_category='cest')
     423        self.cert3 = Certificate(
     424            'CERT3', 'Cert. of Rubbish', study_mode='dp_pt', start_level=100,
     425            end_level=200, application_category='no')
     426        self.dept1.certificates.addCertificate(self.cert1)
     427        self.dept1.certificates.addCertificate(self.cert2)
     428        self.dept2.certificates.addCertificate(self.cert3)
     429        self.cert1.addCourseRef(self.course1, 100, True)
     430        self.cert1.addCourseRef(self.course2, 400, False)
     431        self.cert3.addCourseRef(self.course3, 100, False)
     432        self.certcourse1 = self.cert1['C1_100']
     433        self.certcourse2 = self.cert1['C2_400']
     434        self.certcourse3 = self.cert3['C3_100']
     435        return
     436
     437    def tearDown(self):
     438        shutil.rmtree(self.workdir)
     439        return
     440
     441    def test_ifaces(self):
     442        # make sure we fullfill interface contracts
     443        obj = CertificateCourseExporter()
     444        verifyObject(ICSVExporter, obj)
     445        verifyClass(ICSVExporter, CertificateCourseExporter)
     446        return
     447
     448    def test_get_as_utility(self):
     449        # we can get a certificate exporter as utility
     450        result = queryUtility(ICSVExporter, name="certificate_courses")
     451        self.assertTrue(result is not None)
     452        return
     453
     454    def test_export(self):
     455        # we can export an iterable of certificates
     456        exporter = CertificateCourseExporter()
     457        exporter.export([self.certcourse1], self.outfile)
     458        result = open(self.outfile, 'rb').read()
     459        self.assertEqual(
     460            result,
     461            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
     462            'C1,F1,D1,CERT1,100,1\r\n'
     463            )
     464        return
     465
     466    def test_export_to_string(self):
     467        # we can export an iterable of certificates to a string.
     468        exporter = CertificateCourseExporter()
     469        result = exporter.export(
     470            [self.certcourse1, self.certcourse2], filepath=None)
     471        self.assertEqual(
     472            result,
     473            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
     474            'C1,F1,D1,CERT1,100,1\r\n'
     475            'C2,F1,D1,CERT1,400,0\r\n'
     476            )
     477        return
     478
     479    def test_export_all(self):
     480        # we can export all certificates in a site
     481        exporter = CertificateCourseExporter()
     482        exporter.export_all(self.site, self.outfile)
     483        result = open(self.outfile, 'rb').read()
     484        self.assertEqual(
     485            result,
     486            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
     487            'C1,F1,D1,CERT1,100,1\r\n'
     488            'C2,F1,D1,CERT1,400,0\r\n'
     489            'C3,F1,D2,CERT3,100,0\r\n'
     490            )
     491        return
     492
     493    def test_export_all_to_string(self):
     494        # we can export all certificates in a site to a string
     495        exporter = CertificateCourseExporter()
     496        result = exporter.export_all(self.site, filepath=None)
     497        self.assertEqual(
     498            result,
     499            'course,faculty_code,department_code,certificate_code,level,mandatory\r\n'
     500            'C1,F1,D1,CERT1,100,1\r\n'
     501            'C2,F1,D1,CERT1,400,0\r\n'
     502            'C3,F1,D2,CERT3,100,0\r\n'
     503            )
     504        return
Note: See TracChangeset for help on using the changeset viewer.