# Path: saas/models.py
# class Customer(models.Model):
#     created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
#     updated_on = models.DateTimeField(_("Updated on"), auto_now=True)
#     name = models.CharField(_('Customer name'), max_length=255, blank=False)
#     email = models.EmailField(_('Email address'), blank=True)
#     phone = PhoneNumberField(_('Phone Number'), max_length=255, blank=True)
#     is_active = models.BooleanField(_('Active'), default=True,
#                                     help_text=_('Designates whether this customer should be treated as '
#                                                 'active.'))
# 
#     class Meta:
#         verbose_name = _("Customer")
#         verbose_name_plural = _("Customers")
# 
#     def __str__(self):
#         return self.name
# 
# Path: users/utils.py
# def get_user_group(name, customer):
#     group, created = UserGroup.objects.get_or_create(name=name,
#                                                      customer=customer)
#     return group
# 
# Path: core/import_users.py
# def import_users(filename, password=None, customer=None):
#     """takes a csv with First Name, Last Name, Email and creates users"""
#     with open(filename, "rb") as ifile:
#         reader = csv.reader(ifile)
#         t = zip(reader)
#     import_list = [x[0] for x in t]
#     for i, row in enumerate(import_list):
#         first_name = row[0].strip().title()
#         last_name = row[1].strip().title()
#         if not last_name:
#             names = first_name.split(" ")
#             if len(names) > 1:
#                 last_name = " ".join(names[1:])
#                 first_name = names[0]
#         unique_username = get_adapter().generate_unique_username([
#             first_name,
#             last_name,
#             row[2].strip(),
#         ])
#         data = dict(
#             first_name=first_name,
#             last_name=last_name,
#             email=row[2].strip(),
#             username=unique_username
#         )
#         result = User(**data)
#         if password:
#             result.set_password(password)
#         result.save()
#         if customer:
#             result.userprofile.customer = customer
#             result.userprofile.save()


import csv
from django.contrib.auth.models import User
from saas.models import Customer
from users.utils import get_user_group
from core import import_users

        filename = "example.csv"
        password = "Hunter2"
        customer_id = 6

        import_geminia_users(filename, password, customer_id)
    """
    try:
        this_customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        pass
    else:
        with open(filename, "rb") as ifile:
            reader = csv.reader(ifile)
            t = zip(reader)
        import_list = [x[0] for x in t]
        export_list = []
        for i, row in enumerate(import_list):
            first_name = row[0].strip()
            last_name = row[1].strip()
            if not first_name:
                first_name = last_name.split()[0]
                last_name = " ".join(last_name.split()[1:])
            email = row[4].strip()
            export_list.append([first_name, last_name, email])
            export_filname = "/tmp/geminia-users.csv"
        with open(export_filname, "wb") as csv_file:
            writer = csv.writer(csv_file, delimiter=',')
            for line in export_list:
                writer.writerow(line)

# Path: saas/models.py
# class Customer(models.Model):
#     created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
#     updated_on = models.DateTimeField(_("Updated on"), auto_now=True)
#     name = models.CharField(_('Customer name'), max_length=255, blank=False)
#     email = models.EmailField(_('Email address'), blank=True)
#     phone = PhoneNumberField(_('Phone Number'), max_length=255, blank=True)
#     is_active = models.BooleanField(_('Active'), default=True,
#                                     help_text=_('Designates whether this customer should be treated as '
#                                                 'active.'))
# 
#     class Meta:
#         verbose_name = _("Customer")
#         verbose_name_plural = _("Customers")
# 
#     def __str__(self):
#         return self.name
# 
# Path: users/utils.py
# def get_user_group(name, customer):
#     group, created = UserGroup.objects.get_or_create(name=name,
#                                                      customer=customer)
#     return group
# 
# Path: core/import_users.py
# def import_users(filename, password=None, customer=None):
#     """takes a csv with First Name, Last Name, Email and creates users"""
#     with open(filename, "rb") as ifile:
#         reader = csv.reader(ifile)
#         t = zip(reader)
#     import_list = [x[0] for x in t]
#     for i, row in enumerate(import_list):
#         first_name = row[0].strip().title()
#         last_name = row[1].strip().title()
#         if not last_name:
#             names = first_name.split(" ")
#             if len(names) > 1:
#                 last_name = " ".join(names[1:])
#                 first_name = names[0]
#         unique_username = get_adapter().generate_unique_username([
#             first_name,
#             last_name,
#             row[2].strip(),
#         ])
#         data = dict(
#             first_name=first_name,
#             last_name=last_name,
#             email=row[2].strip(),
#             username=unique_username
#         )
#         result = User(**data)
#         if password:
#             result.set_password(password)
#         result.save()
#         if customer:
#             result.userprofile.customer = customer
#             result.userprofile.save()


# Path: documentation/customers/geminia2017.py
import csv
from django.contrib.auth.models import User
from saas.models import Customer
from users.utils import get_user_group
from core import import_users


        filename = "example.csv"
        password = "Hunter2"
        customer_id = 6

        import_geminia_users(filename, password, customer_id)
    """
    try:
        this_customer = Customer.objects.get(pk=customer_id)
    except Customer.DoesNotExist:
        pass
    else:
        with open(filename, "rb") as ifile:
            reader = csv.reader(ifile)
            t = zip(reader)
        import_list = [x[0] for x in t]
        export_list = []
        for i, row in enumerate(import_list):
            first_name = row[0].strip()
            last_name = row[1].strip()
            if not first_name:
                first_name = last_name.split()[0]
                last_name = " ".join(last_name.split()[1:])
            email = row[4].strip()
            export_list.append([first_name, last_name, email])
            export_filname = "/tmp/geminia-users.csv"
        with open(export_filname, "wb") as csv_file:
            writer = csv.writer(csv_file, delimiter=',')
            for line in export_list:
                writer.writerow(line)

        import_users.import_users(export_filname, password, this_customer)