Ignore:
Timestamp:
26 Oct 2012, 17:57:26 (12 years ago)
Author:
Henrik Bettermann
Message:

Implement BedTicketsExporter?.

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

    r9420 r9427  
    2424from waeup.kofa.students.interfaces import (
    2525    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket,
    26     IStudentOnlinePayment, ICSVStudentExporter)
     26    IStudentOnlinePayment, ICSVStudentExporter, IBedTicket)
    2727from waeup.kofa.utils.batching import ExporterBase
    2828from waeup.kofa.utils.helpers import iface_names
     
    7676    return payments
    7777
     78def get_bedtickets(students):
     79    """Get all bedtickets of `students`.
     80    """
     81    tickets = []
     82    for student in students:
     83        for ticket in student.get('accommodation', {}).values():
     84            tickets.append(ticket)
     85    return tickets
    7886
    7987class StudentExporterBase(ExporterBase):
     
    273281    def export_student(self, student, filepath=None):
    274282        return self.export(get_payments([student]), filepath)
     283
     284class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase):
     285    """Exporter for BedTicket instances.
     286    """
     287    grok.name('bedtickets')
     288
     289    #: Fieldnames considered by this exporter
     290    fields = tuple(
     291        sorted(iface_names(
     292            IBedTicket, exclude_attribs=False))) + (
     293                'student_id', 'actual_bed_type')
     294
     295    #: The title under which this exporter will be displayed
     296    title = _(u'Bed Tickets')
     297
     298    def mangle_value(self, value, name, context=None):
     299        """Treat location values and others special.
     300        """
     301        if context is not None:
     302            student = context.student
     303            if name in ['student_id'] and student is not None:
     304                value = getattr(student, name, None)
     305        if name == 'bed' and value is not None:
     306            value = getattr(value, 'bed_id', None)
     307        if name == 'actual_bed_type':
     308            value = getattr(getattr(context, 'bed', None), 'bed_type')
     309        return super(
     310            BedTicketsExporter, self).mangle_value(
     311            value, name, context=context)
     312
     313    def export_all(self, site, filepath=None):
     314        """Export payments into filepath as CSV data.
     315
     316        If `filepath` is ``None``, a raw string with CSV data is returned.
     317        """
     318        return self.export(get_bedtickets(get_students(site)), filepath)
     319
     320    def export_student(self, student, filepath=None):
     321        return self.export(get_bedtickets([student]), filepath)
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py

    r9316 r9427  
    55from waeup.kofa.students.export import (
    66    StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,
    7     CourseTicketExporter, PaymentsExporter)
     7    CourseTicketExporter, PaymentsExporter, BedTicketsExporter)
    88from waeup.kofa.students.payments import StudentOnlinePayment
     9from waeup.kofa.students.accommodation import BedTicket
    910from waeup.kofa.students.studycourse import StudentStudyCourse
    1011from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
     
    440441            )
    441442        return
     443
     444class BedTicketsExporterTest(StudentImportExportSetup):
     445
     446    layer = FunctionalLayer
     447
     448    def setUp(self):
     449        super(BedTicketsExporterTest, self).setUp()
     450        self.setup_for_export()
     451        return
     452
     453    def test_ifaces(self):
     454        # make sure we fullfill interface contracts
     455        obj = BedTicketsExporter()
     456        verifyObject(ICSVExporter, obj)
     457        verifyClass(ICSVExporter, BedTicketsExporter)
     458        return
     459
     460    def test_get_as_utility(self):
     461        # we can get a bedtickets exporter as utility
     462        result = queryUtility(ICSVExporter, name="bedtickets")
     463        self.assertTrue(result is not None)
     464        return
     465
     466    def test_export_empty(self):
     467        # we can export a nearly empty bedticket
     468        bedticket = BedTicket()
     469        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
     470        bedticket.bed = bed
     471        exporter = BedTicketsExporter()
     472        exporter.export([bedticket], self.outfile)
     473        result = open(self.outfile, 'rb').read()
     474        self.assertMatches(
     475            result,
     476            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
     477            'booking_session,student_id,actual_bed_type\r\n'
     478            'hall-1_A_101_A,,,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,,,regular_male_fr\r\n'
     479            )
     480        return
     481
     482    def test_export(self):
     483        # we can really export student bedtickets.
     484        # set values we can expect in export file
     485        self.setup_student(self.student)
     486        bedticket = self.student['accommodation']['2004']
     487        exporter = BedTicketsExporter()
     488        exporter.export([bedticket], self.outfile)
     489        result = open(self.outfile, 'rb').read()
     490        self.assertMatches(
     491            result,
     492            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
     493            'booking_session,student_id,actual_bed_type\r\n'
     494            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
     495            'A111111,regular_male_fr\r\n'
     496            )
     497        return
     498
     499    def test_export_all(self):
     500        # we can really export all bedtickets
     501        # set values we can expect in export file
     502        self.setup_student(self.student)
     503        exporter = BedTicketsExporter()
     504        exporter.export_all(self.app, self.outfile)
     505        result = open(self.outfile, 'rb').read()
     506        self.assertMatches(
     507            result,
     508            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
     509            'booking_session,student_id,actual_bed_type\r\n'
     510            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
     511            'A111111,regular_male_fr\r\n'
     512            )
     513        return
     514
     515    def test_export_student(self):
     516        # we can really export all bedtickets of a certain student
     517        # set values we can expect in export file
     518        self.setup_student(self.student)
     519        exporter = BedTicketsExporter()
     520        exporter.export_student(self.student, self.outfile)
     521        result = open(self.outfile, 'rb').read()
     522        self.assertMatches(
     523            result,
     524            'bed,bed_coordinates,bed_type,booking_code,booking_date,'
     525            'booking_session,student_id,actual_bed_type\r\n'
     526            'hall-1_A_101_A,,any bed type,,<YYYY-MM-DD hh:mm:ss>.<6-DIGITS>,2004,'
     527            'A111111,regular_male_fr\r\n'
     528            )
     529        return
Note: See TracChangeset for help on using the changeset viewer.