Beispiel #1
0
    def importUsers(self):
        """Import users from CSV file.

        In case of error, return a CSV file filled with the lines where
        errors occured.
        """
        file_upload = self.request.form.get('csv_upload', None)
        if file_upload is None or not file_upload.filename:
            return

        reader = csv.reader(file_upload)
        header = reader.next()

        if header != CSV_HEADER:
            msg = _('Wrong specification of the CSV file. Please correct it and retry.')
            type = 'error'
            IStatusMessage(self.request).addStatusMessage(msg, type=type)
            return

        pr = getToolByName(self.context, 'portal_registration')
        pg = getToolByName(self.context, 'portal_groups')
        acl = getToolByName(self.context,'acl_users')
        groupsIds = set([item['id'] for item in acl.searchGroups()])
        groupsDict = {}
        invalidLines = []
        validLines = []

        groupsNumber = 0

        for line in reader:
            datas = dict(zip(header, line))
#            try:
#                groups = [g.strip() for g in datas.pop('groups').split(',') if g]
#                import pdb
#                pdb.set_trace
#                for group in groups:
#                    if not group in groupsIds: # New group, 'have to create it
#                        pg.addGroup(group)
#                        groupsNumber += 1
#
#            except:
#                invalidLines.append(line)
#                print "Invalid line: %s" % line
#                continue
            validLines.append(line)

        usersNumber = 0
        for line in validLines:
            datas = dict(zip(header, line))
#            import pdb
#            pdb.set_trace()
            try:
#                groups = [g.strip() for g in datas.pop('groups').split(',') if g]
                username = datas['email']
                if self.memberIsExist(username):continue
                id = datas['id']
                if self.IdIsExist(id):continue                
                password = datas.pop('password')
                title = datas.pop('title')
                description = datas['description']
                homepage = datas.pop('homepage')
                phone = datas.pop('phone')
                organization = datas['organization']
                sector = datas.pop('sector')
                position = datas.pop('position')
                province = datas.pop('province')
                address = datas.pop('address')                                                                 
                roles = datas.pop('roles').split(',')
                
# send a add memberuser event
                try:
                    event.notify(CreateMembraneEvent(
                                                id,username,password,title,description,
                                                homepage,phone,organization,sector,
                                                position,province,address))

                except (AttributeError, ValueError), err:
                    logging.exception(err)
                    IStatusMessage(self.request).addStatusMessage(err, type="error")
                    return               
#                pr.addMember(username, password, roles, [], datas)
#                for group in groups:
#
#                    if not group in groupsDict.keys():
#                        groupsDict[group] = acl.getGroupById(group)
#                    groupsDict[group].addMember(username)
                usersNumber += 1
            except:
Beispiel #2
0
#                pr.addMember(username, password, roles, [], datas)
#                for group in groups:
#
#                    if not group in groupsDict.keys():
#                        groupsDict[group] = acl.getGroupById(group)
#                    groupsDict[group].addMember(username)
                usersNumber += 1
            except:
                invalidLines.append(line)
                print "Invalid line: %s" % line

        if invalidLines:
            datafile = self._createCSV(invalidLines)
            self.request['csverrors'] = True
            self.request.form['users_sheet_errors'] = datafile.getvalue()
            msg = _('Some errors occured. Please check your CSV syntax and retry.')
            type = 'error'
        else:
            msg, type = _('Members successfully imported.'), 'info'

        IStatusMessage(self.request).addStatusMessage(msg, type=type)
        self.request['users_results'] = usersNumber
        self.request['groups_results'] = groupsNumber
        return self.index()

    def getCSVWithErrors(self):
        """Return a CSV file that contains lines witch failed."""

        users_sheet_errors = self.request.form.get('users_sheet_errors', None)
        if users_sheet_errors is None:
            return # XXX