Beispiel #1
0
def import_csv(filename, session):
    """Import CSV-file to database

    Returns (member_dict_pairs, notfoundlist, conflictingset) where:
    member_dict_pairs - Tuples of found members with corresponding memberdict.
    notfoundlist - Members not found in the database.
    conflictingset - Members with too similar names.

    """
    # List of dicts with field and value pairs
    memberdictlist = parse_csv(filename)

    member_dict_pairs = []
    notfoundlist = []
    conflictingset = set()

    dbsession = session
    for memberdict in memberdictlist:
        firstname = memberdict["givenNames_fld"].split(" ")[0]
        lastname = memberdict["surName_fld"]
        try:
            member = get_member_with_real_name(firstname, lastname, dbsession)
            member_dict_pairs.append((member, memberdict))

        except PersonNotFoundException:
            notfoundlist.append(memberdict)

        except DuplicateNamesException as e:
            conflictingset.add(tuple(e.query.all()))

    return (member_dict_pairs, notfoundlist, conflictingset)
Beispiel #2
0
def main():
    SessionMaker = connect.connect('members', 'members', 'localhost', 'members')
    session = SessionMaker()

    alreadyimported = []

    # uid from email with cc.hut.fi
    # fetch users without username and cc.hut.fi email
    no_username = session.query(Member).filter(Member.username_fld == None)
    from_email = no_username.join(ContactInformation).filter(
            ContactInformation.email_fld.like('%cc.hut.fi')).all()
    for member in from_email:
        username = member.contactinfo.email_fld.split('@')[0]
        alreadyimported.append(username)
        set_username(member, username)

    # uid from ldapsearch query stored in csv_file.

    print("Already imported: alreadyimported")

    not_found = []
    conflictingset = set()
    if len(sys.argv) > 1:
        reader = csv.reader(open(sys.argv[1], 'r'))

        for cn, uid in reader:
            if uid not in alreadyimported:
                firstname, surname = split_ldap_common_name(cn)

                try:
                    member = get_member_with_real_name(firstname, surname, session)
                    set_username(member, uid)

                except PersonNotFoundException:
                    not_found.append((cn, uid))

                except DuplicateNamesException as e:
                    conflictingset.add(tuple(e.query.all()))

    print("NOT FOUND" + '#' * 70)
    writer = csv.writer(open('not_found.csv', 'w'))
    for cn, uid in not_found:
        writer.writerow((cn, uid))
        print(cn, uid)

    print("CONFLICTS" + '#' * 70)
    for conflicts in conflictingset:
        for conflictingmember in conflicts:
            print(conflictingmember.getWholeName())
        print("-" * 80)

    if input("commit? [y/N]").lower() in ('y', 'yes'):
        print("Committing...")
        session.commit()

    return 0