Changeset 12738 for main


Ignore:
Timestamp:
11 Mar 2015, 22:44:17 (10 years ago)
Author:
uli
Message:

Implement payment related stuff for customers.

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

Legend:

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

    r12553 r12738  
    2525from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
    2626from zope.password.interfaces import IPasswordManager
    27 from zope.component import getUtility, createObject
     27from zope.catalog.interfaces import ICatalog
     28from zope.component import getUtility, createObject, queryUtility
    2829from zope.component.interfaces import IFactory
    2930from zope.interface import implementedBy
     
    4344from waeup.ikoba.customers.documents import CustomerDocumentsContainer
    4445from waeup.ikoba.customers.contracts import ContractsContainer
     46from waeup.ikoba.payments.interfaces import IPayer, IPayerFinder
    4547from waeup.ikoba.utils.helpers import attrs_to_fields, now, copy_filesystem_tree
    4648
     
    399401        return file, path, IkobaImageFile(
    400402            file_obj.filename, file_obj.data)
     403
     404
     405class CustomerPayer(grok.Adapter):
     406    """Adapter to turn customers into IPayers.
     407    """
     408    grok.implements(IPayer)
     409    grok.context(ICustomer)
     410
     411    @property
     412    def first_name(self):
     413        return getattr(self.context, 'firstname', None)
     414
     415    @property
     416    def last_name(self):
     417        return getattr(self.context, 'lastname', None)
     418
     419    @property
     420    def payer_id(self):
     421        return getattr(self.context, 'customer_id', None)
     422
     423
     424class CustomerFinder(grok.GlobalUtility):
     425    """Find customers.
     426    """
     427    grok.name('customer_finder')
     428    grok.implements(IPayerFinder)
     429
     430    def get_payer_by_id(self, customer_id):
     431        catalog = queryUtility(ICatalog, 'customers_catalog')
     432        if catalog is None:
     433            return None
     434        result = catalog.searchResults(
     435            customer_id=(customer_id, customer_id))
     436        result = [x for x in result]
     437        if not result:
     438            return None
     439        # there should not be more than one result really.
     440        return result[0]
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/tests/test_customer.py

    r12297 r12738  
    2424from cStringIO import StringIO
    2525from datetime import tzinfo
    26 from zope.component import getUtility, queryUtility, createObject
     26from zope.component import (
     27    getUtility, queryUtility, createObject, getUtilitiesFor,
     28    )
     29from zope.component.hooks import setSite
    2730from zope.catalog.interfaces import ICatalog
    2831from zope.component.interfaces import IFactory
     
    3134from zope.schema.interfaces import RequiredMissing
    3235from waeup.ikoba.interfaces import IExtFileStore, IFileStoreNameChooser
     36from waeup.ikoba.app import Company
    3337from waeup.ikoba.customers.customer import (
    34     Customer, CustomerFactory, handle_customer_removed, path_from_custid)
     38    Customer, CustomerFactory, handle_customer_removed, path_from_custid,
     39    CustomerPayer, CustomerFinder,
     40    )
    3541from waeup.ikoba.customers.interfaces import (
    3642    ICustomer, ICustomerNavigation, ICustomersUtils)
    3743from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
     44from waeup.ikoba.payments.interfaces import IPayer, IPayerFinder
    3845from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
    3946
     
    9299
    93100
     101class TestCustomerHelpers(FunctionalTestCase):
     102
     103    layer = FunctionalLayer
     104
     105    def test_payer_adapter(self):
     106        # we can adapt ICustomer to IPayer (i.e. create a payer)
     107        customer = Customer()
     108        customer.firstname, customer.lastname = u'Anna', u'Tester'
     109        result = IPayer(customer)
     110        self.assertTrue(isinstance(result, CustomerPayer))
     111        verify.verifyObject(IPayer, result)
     112        self.assertEqual(result.first_name, u'Anna')
     113        self.assertEqual(result.last_name, u'Tester')
     114        self.assertEqual(result.payer_id, customer.customer_id)
     115
     116    def test_customer_finder_iface(self):
     117        # we have a customer finder that returns IPayableFinder data.
     118        verify.verifyClass(IPayerFinder, CustomerFinder)
     119
     120    def test_customer_finder_registered(self):
     121        # the customer finder is a utility registered on startup
     122        util = getUtility(IPayerFinder, name='customer_finder')
     123        self.assertTrue(isinstance(util, CustomerFinder))
     124        utils = [util for name, util in getUtilitiesFor(IPayerFinder)
     125                 if isinstance(util, CustomerFinder)]
     126        self.assertEqual(len(utils), 1)
     127
     128    def create_customer_and_site(self):
     129        customer = Customer()
     130        customer.customer_id = u'CUST1'
     131        self.getRootFolder()['app'] = Company()
     132        app = self.getRootFolder()['app']
     133        setSite(app)
     134        return customer, app
     135
     136    def test_customer_finder(self):
     137        # the customer finder can really find customers
     138        customer, app = self.create_customer_and_site()
     139        app['mycustomer'] = customer  # trigger cataloging
     140        finder = CustomerFinder()
     141        result = finder.get_payer_by_id('CUST1')
     142        self.assertTrue(result is customer)
     143
     144    def test_customer_finder_not_stored(self):
     145        # we get none if an id is not stored
     146        customer, app = self.create_customer_and_site()
     147        app['mycustomer'] = customer  # trigger cataloging
     148        finder = CustomerFinder()
     149        result = finder.get_payer_by_id('Not-a-valid-id')
     150        self.assertTrue(result is None)
     151
     152    def test_customer_finder_no_catalog(self):
     153        # customer finder does not complain about missing catalog
     154        finder = CustomerFinder()
     155        result = finder.get_payer_by_id('CUST1')
     156        self.assertTrue(result is None)
     157
     158
    94159class CustomerRemovalTests(CustomerImportExportSetup):
    95160    # Test handle_customer_removed
Note: See TracChangeset for help on using the changeset viewer.