def broadcast_links(src_file):
    csvfo = file(fname, 'r')
    b = BroadcastToRedis('localhost', 'silochanges')
    ur = UnicodeReader(csvfo)
    data = ur.next()
    while data:
        b.change('digital.bodleian', data[0])
        try:
            data = ur.next()
        except:
            data = None
    return
Ejemplo n.º 2
0
    def addUsersFromCSV(self, filename, heading=True):
        #csvfile order
        csvfo = file(filename, 'r')
        ur = UnicodeReader(csvfo)
        heading=ur.next()

        logfo = file('/var/log/databank/addUserFromCSV.log', 'w')
        uw = UnicodeWriter(logfo)
        title = []
        title.extend(heading)
        title.append('result')

        try:
            ui = heading.index('username')
        except:
            return False

        uw.writerow(title)
        logfo.flush()

        try:
            pi = heading.index('password')
        except:
            return False

        try:
            nmi = heading.index('name')
        except:
            nmi = -1

        try:
            fni = heading.index('firstname')
        except:
            fni = -1

        try:
            lni = heading.index('lastname')
        except:
            lni = -1

        try:
            emi = heading.index('email')
        except:
            emi = -1

        try:
            gpi = heading.index('membership')
        except:
            gpi = -1

        data = ur.next()
        while data:
            if not data[ui] or not data[pi]:
                try:
                    data = ur.next()
                except:
                    data.append('False-No username or password')
                    uw.writerow(data)
                    logfo.flush()
                    data = None
                continue
            name= None
            firstname = None
            lastname = None
            email = None
            groups = []
            if nmi > -1 and data[nmi]:
                name = data[nmi]
            if fni > -1 and data[fni]:
                firstname = data[fni]
            if lni > -1 and data[lni]:
                lastname = data[lni]
            if emi > -1 and data[emi]:
                email = data[emi]
            if gpi > -1 and data[gpi]:
                data[gpi] = data[gpi].replace("'", '"')
                membership = json.loads(data[gpi])
                if type(membership).__name__ == 'list':
                   for member in membership:
                        if not type(member).__name__ == 'list' and len(member) == 2:
                             continue
                        groups.append(member)

            ans = self.add_user(data[ui], data[pi], name=name, firstname=firstname, lastname=lastname, email=email, membership=groups)
            data.append(str(ans))
            uw.writerow(data)
            logfo.flush()
            
            try:
                data = ur.next()
            except:
                data = None
        csvfo.close()
        logfo.close()