Changeset 12716


Ignore:
Timestamp:
10 Mar 2015, 14:59:34 (10 years ago)
Author:
uli
Message:

Introduce contract finder.

Location:
main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/contracts.py

    r12698 r12716  
    2020"""
    2121import grok
     22from zope.catalog.interfaces import ICatalog
    2223from zope.component import getUtility
    2324from zope.component.interfaces import IFactory
     
    3233    IContractSelectProduct, ICustomersUtils, ISampleContract,
    3334    ISampleContractProcess, ISampleContractEdit, ISampleContractOfficialUse)
    34 from waeup.ikoba.payments.interfaces import IPayer
     35from waeup.ikoba.payments.interfaces import IPayer, IPayableFinder
    3536from waeup.ikoba.payments.payment import PaymentItem
    3637from waeup.ikoba.utils.helpers import attrs_to_fields
     
    258259        IWorkflowInfo(contract).fireTransition('create')
    259260    return
     261
     262
     263class ContractFinder(grok.GlobalUtility):
     264    grok.name('contracts_finder')
     265    grok.implements(IPayableFinder)
     266
     267    def get_item_by_id(self, contract_id):
     268        catalog = getUtility(ICatalog, 'contracts_catalog')
     269        result = catalog.searchResults(
     270            contract_id=(contract_id, contract_id))
     271        result = [x for x in result]
     272        if not result:
     273            return None
     274        # there should not be more than one result really.
     275        return result[0]
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/tests/test_contract.py

    r12697 r12716  
    2121import decimal
    2222from zope.interface.verify import verifyClass, verifyObject
    23 from zope.component import createObject
     23from zope.component import createObject, getUtility, getUtilitiesFor
     24from zope.component.hooks import setSite
    2425from hurry.workflow.interfaces import (
    2526    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
     
    2829from waeup.ikoba.customers.contracts import (
    2930    ContractsContainer, SampleContract, payment_items_from_contract,
    30     ContractPayer,
     31    ContractPayer, ContractFinder
    3132    )
     33from waeup.ikoba.app import Company
    3234from waeup.ikoba.customers.customer import Customer
    33 from waeup.ikoba.payments.interfaces import IPaymentItem, IPayer
     35from waeup.ikoba.payments.interfaces import (
     36    IPaymentItem, IPayer, IPayableFinder,
     37    )
    3438from waeup.ikoba.products.productoptions import ProductOption
    3539from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
     
    138142        self.assertEqual(result.last_name, u'Tester')
    139143        self.assertEqual(result.payer_id, customer.customer_id)
     144
     145    def test_contract_finder_iface(self):
     146        # we have a contract finder that returns IPayableFinder data.
     147        verifyClass(IPayableFinder, ContractFinder)
     148
     149    def test_contract_finder_registered(self):
     150        # the contract finder is a utility registered on startup
     151        util = getUtility(IPayableFinder, name='contracts_finder')
     152        self.assertTrue(isinstance(util, ContractFinder))
     153        utils = [util for name, util in getUtilitiesFor(IPayableFinder)
     154                 if isinstance(util, ContractFinder)]
     155        self.assertEqual(len(utils), 1)
     156
     157    def create_contract_and_site(self):
     158        contract = SampleContract()
     159        option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "USD")
     160        option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD")
     161        contract.product_options = [option1, option2]
     162        contract.contract_id = u'CON1234'
     163        self.getRootFolder()['app'] = Company()
     164        app = self.getRootFolder()['app']
     165        setSite(app)
     166        return contract, app
     167
     168    def test_contract_finder(self):
     169        # the contract finder can really find contracts
     170        contract, app = self.create_contract_and_site()
     171        app['mycontract'] = contract  # trigger cataloging
     172        finder = ContractFinder()
     173        result = finder.get_item_by_id('CON1234')
     174        self.assertTrue(result is contract)
     175
     176    def test_contract_finder_not_stored(self):
     177        # we get none if an id is not stored
     178        contract, app = self.create_contract_and_site()
     179        app['mycontract'] = contract  # trigger cataloging
     180        finder = ContractFinder()
     181        result = finder.get_item_by_id('Not-a-valid-id')
     182        self.assertTrue(result is None)
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/interfaces.py

    r12713 r12716  
    144144
    145145
     146class IPayableFinder(Interface):
     147    """Finds payables.
     148    """
     149    def get_item_by_id(item_id):
     150        """Get an item by its Id, or none.
     151        """
     152
     153
    146154class IPaymentItem(Interface):
    147155    """Something to sell.
Note: See TracChangeset for help on using the changeset viewer.