# -*- coding: utf-8 -*-

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

from ...models import *


class Command(BaseCommand):
    args = ''
    help = 'Import training from folder: .'

    def handle(self, *args, **options):

        for fname in args:
            print("import %s" % (fname))

            f = open(fname)
            lines = f.readlines()
            print("%d user to add\n" % (len(lines)))
            count = 0
            for l in lines:
                last, first, ipn = l.replace('"', '').replace("\n", "").split(',')
                try:
                    u = get_user_model().objects.get(username=ipn.lower())
                except:
                    u = get_user_model()(first_name=first, last_name=last, username=ipn.lower())
                    u.set_password("123456")
                    u.save()
                    count += 1

            print("%d user added\n" % (count))
