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

Last change on this file since 11589 was 10648, checked in by Henrik Bettermann, 11 years ago

Export path to object with local role.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1## $Id: userscontainer.py 10648 2013-09-24 20:04:17Z 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##
18"""Users container for the Kofa portal.
19"""
20import grok
21from zope.event import notify
22from waeup.kofa.authentication import Account
23from waeup.kofa.interfaces import IUsersContainer
24from 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
29
30class UsersContainer(grok.Container, Logger):
31    """A container for principals.
32
33    See interfaces.py and users.txt for extensive description.
34    """
35    grok.implements(IUsersContainer)
36    grok.require('waeup.manageUsers')
37
38    def addUser(self, name, password, title=None,
39                description=None, email=None, phone=None,
40                public_name= None, roles=[]):
41        """Add a new Account instance, created from parameters.
42        """
43        if title is None:
44            title = name
45        #if description is None:
46        #    description = title
47        self[name] = Account(name=name, password=password, title=title,
48                             description=description, public_name=public_name,
49                             email=email, phone=phone, roles=roles)
50        #self.logger.info('User account %s added.' % name)
51
52    def addAccount(self, account):
53        """Add the account passed.
54        """
55        self[account.name] = account
56        #self.logger.info('User account %s added.' % account.name)
57
58    def delUser(self, name):
59        """Delete user, if an account with the given name exists.
60
61        Do not complain, if the name does not exist.
62        """
63        if name in self.keys():
64            del self[name]
65            #self.logger.info('User account %s deleted.' % name)
66
67
68class UsersExporter(grok.GlobalUtility, ExporterBase):
69    """Exporter for user accounts.
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                    obj= object
93                    path = ''
94                    while obj.__class__.__name__ != 'University':
95                        path = '%s/' % obj.__name__ + path
96                        obj = obj.__parent__
97                    object_list.append(path)
98                value[role] = object_list
99        return super(
100            UsersExporter, self).mangle_value(
101            value, name, context=context)
102
103    def export(self, users, filepath=None):
104        """Export `users`, an iterable, as CSV file.
105
106        If `filepath` is ``None``, a raw string with CSV data is returned.
107        """
108        writer, outfile = self.get_csv_writer(filepath)
109        for user in users:
110            self.write_item(user, writer)
111        return self.close_outfile(filepath, outfile)
112
113    def export_all(self, site, filepath=None):
114        """Export users into filepath as CSV data.
115
116        If `filepath` is ``None``, a raw string with CSV data is returned.
117        """
118        users = site.get('users', {})
119        return self.export(users.values(), filepath)
Note: See TracBrowser for help on using the repository browser.