source: WAeUP_SRP/branches/srpp_on_branch/Extensions/updateDirectories.py @ 17952

Last change on this file since 17952 was 19, checked in by joachim, 19 years ago

initial import

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1from zLOG import LOG, DEBUG, INFO
2import re
3
4def update(self):
5
6    _log = []
7    def pr(bla, _log=_log):
8        if bla == 'flush':
9            return '\n'.join(_log)
10        _log.append(bla)
11        if (bla):
12            LOG('csvupdate:', INFO, bla)
13
14    pr("Directories update - started")
15    pr("")
16
17    portal = self.portal_url.getPortalObject()
18
19    csv_dir_name = 'csv'
20    csv_dir = getattr(portal,csv_dir_name)
21
22    members_file = 'Utilisateurs.csv'
23
24    try:
25        members_data = str(portal.restrictedTraverse("%s/%s" % (csv_dir_name, members_file)))
26    except KeyError:
27        members_data = ""
28        pr(" Warning: file %s not found. Directory members was not updated." % members_file)
29
30    if members_data:
31        members = []
32        pr("Updating Members directory")
33        member_schema_fields = ['id', 'password', 'givenName', 'sn', 'email', 'groups', 'roles']
34        lines = members_data.splitlines()
35       
36        CSV_SEPARATOR = ';'
37
38        for line in lines:
39            # Some items might contain the csv separator...
40            # Try to handle this gracefully, detecting separator
41            # followed by a space.
42           
43            member_items = line.split(CSV_SEPARATOR)
44
45            if member_items and not (len(member_items) == 1):
46                member_data = {}
47                for field in member_schema_fields:
48                    try:
49                        raw_field = member_items.pop(0).strip()
50                    except IndexError:
51                        raw_field = ''
52                        member_data['bad_entry'] = 1
53                    # Many fields are enclosed in double quotes
54                    if raw_field and raw_field[0] == '"' and raw_field[-1] == '"':
55                        raw_field = raw_field[1:-1]
56                    member_data[field] = raw_field
57
58                member_data['confirm'] = member_data['password']
59                member_data['roles'] = member_data['roles'].capitalize()
60                if member_data['roles'] == 'Manager':
61                    member_data['roles'] = ('Manager', 'Member')
62                else:
63                    member_data['roles'] = ('Member',)
64                member_data['groups'] = tuple(member_data['groups'].split(','))
65
66                if not member_data.has_key('bad_entry'):
67                    # declare user as member of the portal
68                    members.append(member_data)
69                else:
70                    pr("** Bad entry detected **")
71                    pr("A data field might contain the csv separator not followed by a space.")
72                    pr("The id of this entry was %s."%member_data['id'])
73                    pr("The entry will NOT be added to the members directory.")
74
75
76        # create members in member directory
77        directory = portal.portal_directories.members
78        for member in members:
79            m_id = member.get('id')
80            pr('Directories update: checking entry '+m_id)
81            if not directory.hasEntry(m_id):
82                # member login does not exist, create
83                pr('Directories update:  creating entry '+m_id)
84                for gr in member['groups']:
85                    if not portal.portal_directories.groups.hasEntry(gr):
86                         portal.portal_directories.groups.createEntry({'group':gr})
87                directory.createEntry(member)
88            else:
89                # member login already exists
90                pr('Directories update:  entry '+m_id+' exists')
91   
92    pr("")
93    pr("Directories update - end")
94
95    return pr("flush")
Note: See TracBrowser for help on using the repository browser.