Ignore:
Timestamp:
6 May 2012, 14:33:14 (13 years ago)
Author:
uli
Message:

Add payments exporter.

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

    r8342 r8371  
    2424from waeup.kofa.interfaces import MessageFactory as _
    2525from waeup.kofa.students.interfaces import (
    26     IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket)
     26    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket,
     27    IStudentOnlinePayment)
    2728from waeup.kofa.utils.batching import ExporterBase
    2829from waeup.kofa.utils.helpers import iface_names
     
    248249        return self.export(tickets, filepath)
    249250
     251class PaymentsExporter(grok.GlobalUtility, ExporterBase):
     252    """Exporter for OnlinePayment instances.
     253    """
     254    grok.implements(ICSVExporter)
     255    grok.name('studentpayments')
     256
     257    #: Fieldnames considered by this exporter
     258    fields = tuple(
     259        sorted(['reg_number', 'matric_number'] +
     260               iface_names(IStudentOnlinePayment, exclude_attribs=False)))
     261
     262    #: The title under which this exporter will be displayed
     263    title = _(u'Course Tickets')
     264
     265    def mangle_value(self, value, name, context=None):
     266        """Treat location values special.
     267        """
     268        if context is not None:
     269            student = context.getStudent()
     270            if name in ['reg_number', 'matric_number'] and student is not None:
     271                value = getattr(student, name, None)
     272        return super(
     273            PaymentsExporter, self).mangle_value(
     274            value, name, context=context)
     275
     276    def export(self, payments, filepath=None):
     277        """Export `payments`, an iterable, as CSV file.
     278
     279        If `filepath` is ``None``, a raw string with CSV data is returned.
     280        """
     281        writer, outfile = self.get_csv_writer(filepath)
     282        for payment in payments:
     283            self.write_item(payment, writer)
     284        return self.close_outfile(filepath, outfile)
     285
     286    def export_all(self, site, filepath=None):
     287        """Export payments into filepath as CSV data.
     288
     289        If `filepath` is ``None``, a raw string with CSV data is returned.
     290        """
     291        catalog = queryUtility(
     292            ICatalog, context=site, name='students_catalog', default=None)
     293        if catalog is None:
     294            return self.export([], filepath)
     295        students = catalog.searchResults(
     296            student_id=(None, None))
     297        payments = []
     298        for student in students:
     299            if not 'payments' in student.keys():
     300                continue
     301            for payment in student['payments'].values():
     302                payments.append(payment)
     303        return self.export(payments, filepath)
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_export.py

    r8341 r8371  
     1import datetime
    12from zope.component import queryUtility
    23from zope.interface.verify import verifyObject, verifyClass
     
    45from waeup.kofa.students.export import (
    56    StudentsExporter, StudentStudyCourseExporter, StudentStudyLevelExporter,
    6     CourseTicketExporter)
     7    CourseTicketExporter, PaymentsExporter)
     8from waeup.kofa.students.payments import StudentOnlinePayment
    79from waeup.kofa.students.student import StudentStudyCourse
    810from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
     
    273275            )
    274276        return
     277
     278class PaymentsExporterTest(StudentImportExportSetup):
     279
     280    layer = FunctionalLayer
     281
     282    def setUp(self):
     283        super(PaymentsExporterTest, self).setUp()
     284        self.setup_for_export()
     285        return
     286
     287    def get_payment(self):
     288        # get a payment with all fields set
     289        payment = StudentOnlinePayment()
     290        payment.creation_date = datetime.datetime(2012, 4, 1, 13, 12, 1)
     291        payment.p_id = 'my-id'
     292        payment.ac = u'666'
     293        payment.p_item = u'p-item'
     294        payment.p_level = 100
     295        payment.p_session = 2012
     296        payment.payment_date = datetime.datetime(2012, 4, 1, 14, 12, 1)
     297        payment.r_amount_approved = 12.12
     298        payment.r_code = u'r-code'
     299        # XXX: there is no addPayment method to give predictable names
     300        self.student['payments']['my-payment'] = payment
     301        return payment
     302
     303    def test_ifaces(self):
     304        # make sure we fullfill interface contracts
     305        obj = PaymentsExporter()
     306        verifyObject(ICSVExporter, obj)
     307        verifyClass(ICSVExporter, PaymentsExporter)
     308        return
     309
     310    def test_get_as_utility(self):
     311        # we can get a payments exporter as utility
     312        result = queryUtility(ICSVExporter, name="studentpayments")
     313        self.assertTrue(result is not None)
     314        return
     315
     316    def test_export_empty(self):
     317        # we can export a nearly empty payment
     318        payment = StudentOnlinePayment()
     319        payment.creation_date = datetime.datetime(2012, 4, 1, 13, 12, 1)
     320        exporter = PaymentsExporter()
     321        exporter.export([payment], self.outfile)
     322        result = open(self.outfile, 'rb').read()
     323        self.assertEqual(
     324            result,
     325            'ac,amount_auth,creation_date,matric_number,p_category,p_id,'
     326            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
     327            'r_code,reg_number\r\n'
     328
     329            ',0.0,2012-04-01 13:12:01,,schoolfee,,,,,unpaid,,0.0,,\r\n'
     330            )
     331        return
     332
     333    def test_export(self):
     334        # we can really export student payments.
     335        # set values we can expect in export file
     336        self.setup_student(self.student)
     337        payment = self.get_payment()
     338        exporter = PaymentsExporter()
     339        exporter.export([payment], self.outfile)
     340        result = open(self.outfile, 'rb').read()
     341        self.assertEqual(
     342            result,
     343            'ac,amount_auth,creation_date,matric_number,p_category,p_id,'
     344            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
     345            'r_code,reg_number\r\n'
     346
     347            '666,0.0,2012-04-01 13:12:01,M123456,schoolfee,my-id,'
     348            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
     349            'r-code,123456\r\n'
     350            )
     351        return
     352
     353    def test_export_all(self):
     354        # we can really export all payments
     355        # set values we can expect in export file
     356        self.setup_student(self.student)
     357        self.get_payment() # adds new payment to student
     358        exporter = PaymentsExporter()
     359        exporter.export_all(self.app, self.outfile)
     360        result = open(self.outfile, 'rb').read()
     361        self.assertEqual(
     362            result,
     363            'ac,amount_auth,creation_date,matric_number,p_category,p_id,'
     364            'p_item,p_level,p_session,p_state,payment_date,r_amount_approved,'
     365            'r_code,reg_number\r\n'
     366
     367            '666,0.0,2012-04-01 13:12:01,M123456,schoolfee,my-id,'
     368            'p-item,100,2012,unpaid,2012-04-01 14:12:01,12.12,'
     369            'r-code,123456\r\n'
     370            )
     371        return
Note: See TracChangeset for help on using the changeset viewer.