# Imports the supplied names into the Person table in the DB 
# supplied in the config
from webapp.models import Person

# CSV text
names = """DOE, JOHN
BESS, CHRISTOPHER
DOE, JANE"""

import_count = 0
skip_count = 0
# iterate names to import
for name in names.split('\n'):
    # normalize the name
    parts = name.split(',')
    name = parts[0].strip().title()
    if len(parts) >= 2:
        name = '%s %s' % (parts[1].strip().title(), name)
    # prevent duplicates
    if Person.objects(name=name).count():
        skip_count += 1
        continue
    person = Person(name=name)
    person.save()
    import_count += 1
    
print 'Imported %d persons, %d skipped. Total: %d' % \
    (import_count, skip_count, Person.objects.count())
    
    email = CharField()
    is_admin = IntegerField()
    name = CharField()
    password = CharField()

    class Meta:
        db_table = 'user'


sqlite_db.connect()

## Ad-hoc migration

# move all persons
for person in LegacyPerson.select():
    if Person.objects(name=person.name).count():
        continue
    Person(name=person.name).save()
    print 'Migrated Person: '+person.name

# move all users
for user in LegacyUser.select():
    if User.objects(name=user.name).count():
        continue
    # transfer user data
    User(
        name=user.name,
        email=user.email,
        password=user.password,
        is_admin=bool(user.is_admin)
    ).save()