source: main/waeup.kofa/trunk/src/waeup/kofa/userscontainer.py @ 15189

Last change on this file since 15189 was 12915, checked in by Henrik Bettermann, 10 years ago

Start integrating doc tests into documentation.

  • Property svn:keywords set to Id
File size: 4.1 KB
RevLine 
[7193]1## $Id: userscontainer.py 12915 2015-05-08 07:09:08Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
[7819]18"""Users container for the Kofa portal.
[4089]19"""
20import grok
[6180]21from zope.event import notify
[7811]22from waeup.kofa.authentication import Account
23from waeup.kofa.interfaces import IUsersContainer
24from waeup.kofa.utils.logger import Logger
[8787]25from waeup.kofa.utils.batching import ExporterBase
26from waeup.kofa.interfaces import ICSVExporter
27from waeup.kofa.interfaces import MessageFactory as _
[4089]28
[8787]29
[7653]30class UsersContainer(grok.Container, Logger):
[12915]31    """A container for officers.
[4089]32
33    See interfaces.py and users.txt for extensive description.
34    """
[7172]35    grok.implements(IUsersContainer)
[4089]36    grok.require('waeup.manageUsers')
37
[7221]38    def addUser(self, name, password, title=None,
[8757]39                description=None, email=None, phone=None,
40                public_name= None, roles=[]):
[4638]41        """Add a new Account instance, created from parameters.
42        """
[4089]43        if title is None:
44            title = name
[7636]45        self[name] = Account(name=name, password=password, title=title,
[8757]46                             description=description, public_name=public_name,
[7636]47                             email=email, phone=phone, roles=roles)
[4089]48
49    def addAccount(self, account):
[4638]50        """Add the account passed.
51        """
[4089]52        self[account.name] = account
[6180]53
[4089]54    def delUser(self, name):
[4638]55        """Delete user, if an account with the given name exists.
56
57        Do not complain, if the name does not exist.
58        """
[4089]59        if name in self.keys():
60            del self[name]
[8787]61
62
[12079]63class UserExporter(grok.GlobalUtility, ExporterBase):
[12859]64    """The User Exporter exports all user accounts. It iterates over all
65    objects of the ``users`` container.
[8787]66    """
67    grok.implements(ICSVExporter)
68    grok.name('users')
69
70    fields = ('name', 'title', 'public_name', 'description',
71              'email', 'phone', 'roles', 'local_roles', 'password')
72    title = _(u'Users')
73
74    def mangle_value(self, value, name, context=None):
[12859]75        """The mangler determines the local roles each user has and computes
76        a Python expression like:
77
78        ``{u'waeup.local.ClearanceOfficer': [u'faculties/ABC/', u'faculties/DEF/']}``
[8787]79        """
80        if name == 'local_roles' and context is not None:
81            local_roles = context.getLocalRoles()
82            value = {}
83            for role in local_roles.keys():
84                objects = local_roles[role]
85                object_list = []
86                for object in objects:
[10648]87                    obj= object
88                    path = ''
89                    while obj.__class__.__name__ != 'University':
90                        path = '%s/' % obj.__name__ + path
91                        obj = obj.__parent__
92                    object_list.append(path)
[8787]93                value[role] = object_list
94        return super(
[12079]95            UserExporter, self).mangle_value(
[8787]96            value, name, context=context)
97
98    def export(self, users, filepath=None):
99        """Export `users`, an iterable, as CSV file.
100
101        If `filepath` is ``None``, a raw string with CSV data is returned.
102        """
103        writer, outfile = self.get_csv_writer(filepath)
104        for user in users:
105            self.write_item(user, writer)
106        return self.close_outfile(filepath, outfile)
107
108    def export_all(self, site, filepath=None):
109        """Export users into filepath as CSV data.
110
111        If `filepath` is ``None``, a raw string with CSV data is returned.
112        """
113        users = site.get('users', {})
114        return self.export(users.values(), filepath)
Note: See TracBrowser for help on using the repository browser.