Ignore:
Timestamp:
17 Jun 2016, 07:46:34 (8 years ago)
Author:
Henrik Bettermann
Message:

Add ApplicantPaymentExporter.

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

Legend:

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

    r13542 r13949  
    2222from zope.component import queryUtility
    2323from waeup.kofa.applicants.interfaces import (
    24     IApplicantBaseData, IApplicantsContainer)
     24    IApplicantBaseData, IApplicantsContainer, IApplicantOnlinePayment)
    2525from waeup.kofa.interfaces import ICSVExporter
    2626from waeup.kofa.interfaces import MessageFactory as _
     
    128128            # We therefore search for applicant_id.
    129129            applicant_id=(None, None))
    130         used = [value for value in applicants if value.container_code.endswith('+')]
     130        used = [value for value in applicants
     131                if value.container_code.endswith('+')]
    131132        return self.export(used, filepath=filepath)
    132133
     
    138139        container = grok.getSite()['applicants'][kw['container']]
    139140        container_values = container.values()
    140         used = [value for value in container_values if value.container_code.endswith('+')]
     141        used = [value for value in container_values
     142                if value.container_code.endswith('+')]
    141143        return self.export(used, filepath=filepath)
     144
     145
     146class ApplicantPaymentExporter(grok.GlobalUtility, ExporterBase):
     147    """The Applicant Payments Exporter exports all payments made by applicants.
     148    In other words, it exports payment tickets in state 'paid'. The exporter
     149    searches :class:`ApplicantsCatalog` and iterates over all payment tickets
     150    which are stored in an applicant (container).
     151
     152    The exporter exports all applicant payments if started in the Data Center
     153    which means in the context of the `DataCenter` object. The exporter can also
     154    be started 'locally' which means in the context of an
     155    `ApplicantsContainer` container, see `ApplicantExporter` above.
     156    """
     157    grok.implements(ICSVExporter)
     158    grok.name('applicantpayments')
     159
     160    fields = tuple(sorted(iface_names(
     161        IApplicantOnlinePayment,
     162        exclude_attribs=False,
     163        omit=['display_item']))) + ('applicant_id',)
     164    title = _(u'Applicant Payments')
     165
     166    def mangle_value(self, value, name, context=None):
     167        """The mangler determines the applicant's id.
     168        """
     169        if name == 'applicant_id' and context is not None:
     170            applicant = context.__parent__
     171            value = getattr(applicant, name, None)
     172        return super(
     173            ApplicantPaymentExporter, self).mangle_value(
     174            value, name, context=context)
     175
     176    def export(self, payments, filepath=None):
     177        """
     178        """
     179        writer, outfile = self.get_csv_writer(filepath)
     180        for payment in payments:
     181            self.write_item(payment, writer)
     182        return self.close_outfile(filepath, outfile)
     183
     184    def export_all(self, site, filepath=None):
     185        """
     186        """
     187        catalog = queryUtility(
     188            ICatalog, context=site, name='applicants_catalog', default=None)
     189        if catalog is None:
     190            return self.export([], filepath)
     191        applicants = catalog.searchResults(
     192            # reg_num might not be set and then would not be found.
     193            # We therefore search for applicant_id.
     194            applicant_id=(None, None))
     195        used = [value for value in applicants
     196                if value.container_code.endswith('+')]
     197        payments = []
     198        for applicant in used:
     199            for payment in applicant.values():
     200                if payment.p_state == 'paid':
     201                    payments.append(payment)
     202        return self.export(payments, filepath=filepath)
     203
     204    def export_filtered(self, site, filepath=None, **kw):
     205        """
     206        """
     207        container = grok.getSite()['applicants'][kw['container']]
     208        container_values = container.values()
     209        used = [value for value in container_values
     210                if value.container_code.endswith('+')]
     211        payments = []
     212        for applicant in used:
     213            for payment in applicant.values():
     214                if payment.p_state == 'paid':
     215                    payments.append(payment)
     216        return self.export(payments, filepath=filepath)
  • main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_export.py

    r13080 r13949  
    66import unittest
    77from zope.catalog.interfaces import ICatalog
    8 from zope.component import queryUtility, getUtility
     8from zope.component import queryUtility, getUtility, createObject
    99from zope.interface.verify import verifyObject, verifyClass
    1010from zope.intid.interfaces import IIntIds
    1111from waeup.kofa.applicants import ApplicantsContainer
    1212from waeup.kofa.applicants.export import (
    13     ApplicantsContainerExporter, ApplicantExporter)
     13    ApplicantsContainerExporter, ApplicantExporter,
     14    ApplicantPaymentExporter)
    1415from waeup.kofa.applicants.interfaces import (
    1516    AppCatSource, ApplicationTypeSource)
     
    233234            in result)
    234235        return
     236
     237
     238class ApplicantPaymentExporterTest(ApplicantImportExportSetup):
     239
     240    layer = FunctionalLayer
     241
     242    def setUp(self):
     243        super(ApplicantPaymentExporterTest, self).setUp()
     244        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     245        self.cat = getUtility(ICatalog, name='applicants_catalog')
     246        self.intids = getUtility(IIntIds)
     247        return
     248
     249    def test_ifaces(self):
     250        # make sure we fullfill interface contracts
     251        obj = ApplicantPaymentExporter()
     252        verifyObject(ICSVExporter, obj)
     253        verifyClass(ICSVExporter, ApplicantPaymentExporter)
     254        return
     255
     256    def test_get_as_utility(self):
     257        # we can get an applicant exporter as utility
     258        result = queryUtility(ICSVExporter, name="applicantpayments")
     259        self.assertTrue(result is not None)
     260        return
     261
     262    def setup_applicant(self, applicant):
     263        # set predictable values for `applicant`
     264        applicant.reg_number = u'123456'
     265        applicant.applicant_id = u'dp2011_654321'
     266        applicant.firstname = u'Anna'
     267        applicant.lastname = u'Tester'
     268        applicant.middlename = u'M.'
     269        applicant.date_of_birth = datetime.date(1981, 2, 4)
     270        applicant.sex = 'f'
     271        applicant.email = 'anna@sample.com'
     272        applicant.phone = u'+234-123-12345'
     273        # Add payment
     274        payment = createObject(u'waeup.ApplicantOnlinePayment')
     275        payment.p_id = 'p120'
     276        payment.p_session = 2012
     277        payment.p_category = 'application'
     278        payment.p_state = 'paid'
     279        applicant['p120'] = payment
     280        return applicant
     281
     282    def test_export(self):
     283        applicant = self.setup_applicant(self.applicant)
     284        exporter = ApplicantPaymentExporter()
     285        exporter.export(applicant.values(), self.outfile)
     286        result = open(self.outfile, 'rb').read()
     287        cdate = str('%s#' % self.applicant['p120'].creation_date)
     288        self.assertEqual(
     289            'ac,amount_auth,creation_date,p_category,p_id,p_item,p_session,'
     290            'p_state,payment_date,r_amount_approved,r_code,r_desc,'
     291            'applicant_id\r\n,'
     292            '0.0,%s,application,p120,,2012,'
     293            'paid,,0.0,,,dp2011_654321\r\n' % cdate, result)
     294        return
     295
     296    def test_export_all(self):
     297        self.applicant = self.setup_applicant(self.applicant)
     298        exporter = ApplicantPaymentExporter()
     299        exporter.export_all(self.app, self.outfile)
     300        result = open(self.outfile, 'rb').read()
     301        cdate = str('%s#' % self.applicant['p120'].creation_date)
     302        self.assertEqual(
     303            'ac,amount_auth,creation_date,p_category,p_id,p_item,p_session,'
     304            'p_state,payment_date,r_amount_approved,r_code,r_desc,'
     305            'applicant_id\r\n,'
     306            '0.0,%s,application,p120,,2012,'
     307            'paid,,0.0,,,dp2011_654321\r\n' % cdate, result)
     308        return
     309
     310    def test_export_filtered(self):
     311        self.applicant = self.setup_applicant(self.applicant)
     312        exporter = ApplicantPaymentExporter()
     313        exporter.export_filtered(
     314            self.app, self.outfile, container=self.container.code)
     315        result = open(self.outfile, 'rb').read()
     316        cdate = str('%s#' % self.applicant['p120'].creation_date)
     317        self.assertEqual(
     318            'ac,amount_auth,creation_date,p_category,p_id,p_item,p_session,'
     319            'p_state,payment_date,r_amount_approved,r_code,r_desc,'
     320            'applicant_id\r\n,'
     321            '0.0,%s,application,p120,,2012,'
     322            'paid,,0.0,,,dp2011_654321\r\n' % cdate, result)
     323        return
Note: See TracChangeset for help on using the changeset viewer.