Ignore:
Timestamp:
28 Nov 2014, 07:29:14 (10 years ago)
Author:
Henrik Bettermann
Message:

Add product exporter with tests.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba/products
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/products/export.py

    r12073 r12077  
    1919"""
    2020import grok
     21from zope.securitypolicy.interfaces import IPrincipalRoleMap
    2122from waeup.ikoba.interfaces import ICSVExporter
    2223from waeup.ikoba.interfaces import MessageFactory as _
     
    2425from waeup.ikoba.utils.helpers import iface_names
    2526from waeup.ikoba.products.interfaces import IProduct
     27
     28
     29class ProductExporter(grok.GlobalUtility, ExporterBase):
     30    """Exporter for products.
     31    """
     32    grok.implements(ICSVExporter)
     33    grok.name('products')
     34
     35    #: Fieldnames considered by this exporter
     36    fields = tuple(sorted(iface_names(IProduct))) + (
     37        'users_with_local_roles',)
     38
     39    #: The title under which this exporter will be displayed
     40    title = _(u'Products')
     41
     42    def mangle_value(self, value, name, context=None):
     43        """Hook for mangling values in derived classes
     44        """
     45        if name == 'users_with_local_roles':
     46            value = []
     47            role_map = IPrincipalRoleMap(context)
     48            for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
     49                value.append({'user_name':user_name,'local_role':local_role})
     50        return super(ProductExporter, self).mangle_value(
     51            value, name, context)
     52
     53    def export(self, products, filepath=None):
     54        """Export `products`, an iterable, as CSV file.
     55
     56        If `filepath` is ``None``, a raw string with CSV data is returned.
     57        """
     58        writer, outfile = self.get_csv_writer(filepath)
     59        for product in products:
     60            self.write_item(product, writer)
     61        return self.close_outfile(filepath, outfile)
     62
     63    def export_all(self, site, filepath=None):
     64        """Export products in productscontainer into filepath as CSV data.
     65
     66        If `filepath` is ``None``, a raw string with CSV data is returned.
     67        """
     68        products = site.get('products', {})
     69        return self.export(products.values(), filepath)
  • main/waeup.ikoba/trunk/src/waeup/ikoba/products/tests/test_batching.py

    r12073 r12077  
    3131from zope.catalog.interfaces import ICatalog
    3232from zope.interface.verify import verifyClass, verifyObject
     33from zope.securitypolicy.interfaces import IPrincipalRoleManager
    3334
    3435from waeup.ikoba.app import Company
     
    8283        self.app['products'][product.product_id] = self.product = product
    8384        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     85        role_manager = IPrincipalRoleManager(product)
     86        role_manager.assignRoleToPrincipal(u'johnsrole', u'john')
    8487        return
    8588
  • main/waeup.ikoba/trunk/src/waeup/ikoba/products/tests/test_browser.py

    r12073 r12077  
    5555    return None
    5656
    57 class ProductssFullSetup(FunctionalTestCase):
     57class ProductsFullSetup(FunctionalTestCase):
    5858    # A test case that only contains a setup and teardown
    5959    #
     
    6767
    6868    def setUp(self):
    69         super(ProductssFullSetup, self).setUp()
     69        super(ProductsFullSetup, self).setUp()
    7070
    7171        # Setup a sample site for each test
     
    113113
    114114    def tearDown(self):
    115         super(ProductssFullSetup, self).tearDown()
     115        super(ProductsFullSetup, self).tearDown()
    116116        clearSite()
    117117        shutil.rmtree(self.dc_root)
    118118
    119 class ProductsContainerUITests(ProductssFullSetup):
     119class ProductsContainerUITests(ProductsFullSetup):
    120120    # Tests for ProductsContainer class views and pages
    121121
     
    163163
    164164
    165 class ProductsUITests(ProductssFullSetup):
     165class ProductsUITests(ProductsFullSetup):
    166166    # Tests for Products class views and pages
    167167
Note: See TracChangeset for help on using the changeset viewer.