Changeset 8787 for main


Ignore:
Timestamp:
25 Jun 2012, 11:22:19 (13 years ago)
Author:
Henrik Bettermann
Message:

Add UsersExporter?.

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

Legend:

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

    r8757 r8787  
    2323from waeup.kofa.interfaces import IUsersContainer
    2424from waeup.kofa.utils.logger import Logger
     25from waeup.kofa.utils.batching import ExporterBase
     26from waeup.kofa.interfaces import ICSVExporter
     27from waeup.kofa.interfaces import MessageFactory as _
     28
    2529
    2630class UsersContainer(grok.Container, Logger):
     
    6064            del self[name]
    6165            #self.logger.info('User account %s deleted.' % name)
     66
     67
     68class UsersExporter(grok.GlobalUtility, ExporterBase):
     69    """Exporter for faculties.
     70    """
     71    grok.implements(ICSVExporter)
     72    grok.name('users')
     73
     74    #: Fieldnames considered by this exporter
     75    fields = ('name', 'title', 'public_name', 'description',
     76              'email', 'phone', 'roles', 'local_roles', 'password')
     77
     78    #: The title under which this exporter will be displayed
     79    title = _(u'Users')
     80
     81    def mangle_value(self, value, name, context=None):
     82        """Treat location values special.
     83        """
     84       
     85        if name == 'local_roles' and context is not None:
     86            local_roles = context.getLocalRoles()
     87            value = {}
     88            for role in local_roles.keys():
     89                objects = local_roles[role]
     90                object_list = []
     91                for object in objects:
     92                    object.__class__.__name__
     93                    object_list.append((object.__name__, object.__class__.__name__))
     94                value[role] = object_list
     95        return super(
     96            UsersExporter, self).mangle_value(
     97            value, name, context=context)
     98
     99    def export(self, users, filepath=None):
     100        """Export `users`, an iterable, as CSV file.
     101
     102        If `filepath` is ``None``, a raw string with CSV data is returned.
     103        """
     104        writer, outfile = self.get_csv_writer(filepath)
     105        for user in users:
     106            self.write_item(user, writer)
     107        return self.close_outfile(filepath, outfile)
     108
     109    def export_all(self, site, filepath=None):
     110        """Export users into filepath as CSV data.
     111
     112        If `filepath` is ``None``, a raw string with CSV data is returned.
     113        """
     114        writer, outfile = self.get_csv_writer(filepath)
     115        users = site.get('users', {})
     116        return self.export(users.values(), filepath)
  • main/waeup.kofa/trunk/src/waeup/kofa/userscontainer.txt

    r8343 r8787  
    4848    '{SSHA}...'
    4949
     50We can export user accounts:
     51
     52    >>> from waeup.kofa.userscontainer import UsersExporter
     53    >>> import os
     54    >>> import tempfile
     55    >>> workdir = tempfile.mkdtemp()
     56    >>> outfile = os.path.join(workdir, 'myoutput.csv')
     57    >>> exporter = UsersExporter()
     58    >>> site = {'users':myusers}
     59    >>> exporter.export_all(site, outfile)
     60    >>> result = open(outfile, 'rb').read()
     61    >>> 'name,title,public_name,description,email,phone,roles,local_roles,password' in result
     62    True
     63    >>> '{SSHA}' in result
     64    True
     65
    5066We can delete users:
    5167
Note: See TracChangeset for help on using the changeset viewer.