# Path: authentication/models.py
# class User(AbstractBaseUser, PermissionsMixin):
#     main_character_id = models.PositiveIntegerField(primary_key=True)
#     email = models.EmailField(max_length=255, null=True, blank=True)
#     is_staff = models.BooleanField(default=False)
#     is_active = models.BooleanField(default=False)
# 
#     USERNAME_FIELD = 'main_character_id'
#     REQUIRED_FIELDS = []
# 
#     objects = UserManager()
# 
#     class Meta:
#         verbose_name = 'user'
#         verbose_name_plural = 'users'
# 
#     @property
#     def main_character(self):
#         if self.characters.filter(id=self.main_character_id).exists():
#             return self.characters.get(id=self.main_character_id)
#         elif EVECharacter.objects.filter(id=self.main_character_id).exists():
#             logger.warn("User with main character ID %s has detached main character model" % self.get_full_name())
#             return EVECharacter.objects.get(id=self.main_character_id)
#         else:
#             return None
# 
#     def get_full_name(self):
#         return str(self.main_character_id)
#     def get_short_name(self):
#         if self.main_character:
#             return str(self.main_character)
#         else:
#             return self.get_full_name()
# 
#     def __unicode__(self):
#         if self.main_character:
#             return str(self.main_character).encode('utf-8')
#         else:
#             logger.debug("Missing character model for user with main character id %s, returning id as __unicode__." % self.main_character_id)
#             return self.get_short_name().encode('utf-8')
# 
# Path: authentication/managers.py
# class UserManager(BaseUserManager):
#     def _create_user(self, main_character_id, email=None, is_staff=False, is_superuser=False, password=None, **extra_fields):
#         logger.info("Creating user for character id: %s" % str(main_character_id))
#         if not main_character_id:
#             raise ValueError('Users require a main character')
#         if email:
#             email = self.normalize_email(email)
#             logger.debug("Email supplied for user creation: %s" % email)
#         user = self.model(main_character_id=main_character_id, email=email, is_staff=is_staff, is_active=True, is_superuser=is_superuser, **extra_fields)
#         if password:
#             user.set_password(password)
#             logger.debug("Received password for user.")
#         else:
#             user.set_unusable_password()
#             logger.debug("Setting unusable password for user.")
#         char = EVECharacter.objects.get_by_id(main_character_id)
#         logger.debug("Got character model %s for main character id %s" % (char, main_character_id))
#         if not char:
#             logger.error("Unable to create user with main character id %s - possibly bad id." % main_character_id)
#             return None
#         user.save()
#         char.assign_user()
#         logger.debug("User created succesfully: %s" % str(user))
#         user_created.send(sender=UserManager, user=user)
#         return user
# 
#     def create_user(self, main_character_id, email=None, **extra_fields):
#         return self._create_user(main_character_id, email, **extra_fields)
# 
#     def create_superuser(self, main_character_id, **extra_fields):
#         user = self._create_user(main_character_id=main_character_id, is_staff=True, is_superuser=True, **extra_fields)
#         user.is_active=True
#         user.save()
#         logger.info("Created superuser: %s" % str(user))
#         return user


from django.test import TestCase
from .models import User
from .managers import UserManager
import logging

logger = logging.getLogger(__name__)

class UserTestCate(TestCase):
    def setUp(self):
        pass

    def test_create_user_with_good_char_id(self):
        logger.debug("-----------------------------------------")
        logger.debug("   test_create_user_with_good_char_id")
        logger.debug("-----------------------------------------")
        #ensure generating character and assigning user
        main_character_id = 234899860
        user = User.objects.create_user(main_character_id=main_character_id)
        #ensure user has characters
        self.assertEqual(len(user.characters.all()), 1)

    def test_create_user_with_bad_char_id(self):
        logger.debug("-----------------------------------------")
        logger.debug("   test_create_user_with_bad_char_id")
        logger.debug("-----------------------------------------")
        #ensure failing to generate character
        main_character_id = 534899860

# Path: authentication/models.py
# class User(AbstractBaseUser, PermissionsMixin):
#     main_character_id = models.PositiveIntegerField(primary_key=True)
#     email = models.EmailField(max_length=255, null=True, blank=True)
#     is_staff = models.BooleanField(default=False)
#     is_active = models.BooleanField(default=False)
# 
#     USERNAME_FIELD = 'main_character_id'
#     REQUIRED_FIELDS = []
# 
#     objects = UserManager()
# 
#     class Meta:
#         verbose_name = 'user'
#         verbose_name_plural = 'users'
# 
#     @property
#     def main_character(self):
#         if self.characters.filter(id=self.main_character_id).exists():
#             return self.characters.get(id=self.main_character_id)
#         elif EVECharacter.objects.filter(id=self.main_character_id).exists():
#             logger.warn("User with main character ID %s has detached main character model" % self.get_full_name())
#             return EVECharacter.objects.get(id=self.main_character_id)
#         else:
#             return None
# 
#     def get_full_name(self):
#         return str(self.main_character_id)
#     def get_short_name(self):
#         if self.main_character:
#             return str(self.main_character)
#         else:
#             return self.get_full_name()
# 
#     def __unicode__(self):
#         if self.main_character:
#             return str(self.main_character).encode('utf-8')
#         else:
#             logger.debug("Missing character model for user with main character id %s, returning id as __unicode__." % self.main_character_id)
#             return self.get_short_name().encode('utf-8')
# 
# Path: authentication/managers.py
# class UserManager(BaseUserManager):
#     def _create_user(self, main_character_id, email=None, is_staff=False, is_superuser=False, password=None, **extra_fields):
#         logger.info("Creating user for character id: %s" % str(main_character_id))
#         if not main_character_id:
#             raise ValueError('Users require a main character')
#         if email:
#             email = self.normalize_email(email)
#             logger.debug("Email supplied for user creation: %s" % email)
#         user = self.model(main_character_id=main_character_id, email=email, is_staff=is_staff, is_active=True, is_superuser=is_superuser, **extra_fields)
#         if password:
#             user.set_password(password)
#             logger.debug("Received password for user.")
#         else:
#             user.set_unusable_password()
#             logger.debug("Setting unusable password for user.")
#         char = EVECharacter.objects.get_by_id(main_character_id)
#         logger.debug("Got character model %s for main character id %s" % (char, main_character_id))
#         if not char:
#             logger.error("Unable to create user with main character id %s - possibly bad id." % main_character_id)
#             return None
#         user.save()
#         char.assign_user()
#         logger.debug("User created succesfully: %s" % str(user))
#         user_created.send(sender=UserManager, user=user)
#         return user
# 
#     def create_user(self, main_character_id, email=None, **extra_fields):
#         return self._create_user(main_character_id, email, **extra_fields)
# 
#     def create_superuser(self, main_character_id, **extra_fields):
#         user = self._create_user(main_character_id=main_character_id, is_staff=True, is_superuser=True, **extra_fields)
#         user.is_active=True
#         user.save()
#         logger.info("Created superuser: %s" % str(user))
#         return user


# Path: authentication/tests.py
from django.test import TestCase
from .models import User
from .managers import UserManager
import logging


logger = logging.getLogger(__name__)

class UserTestCate(TestCase):
    def setUp(self):
        pass

    def test_create_user_with_good_char_id(self):
        logger.debug("-----------------------------------------")
        logger.debug("   test_create_user_with_good_char_id")
        logger.debug("-----------------------------------------")
        #ensure generating character and assigning user
        main_character_id = 234899860
        user = User.objects.create_user(main_character_id=main_character_id)
        #ensure user has characters
        self.assertEqual(len(user.characters.all()), 1)

    def test_create_user_with_bad_char_id(self):
        logger.debug("-----------------------------------------")
        logger.debug("   test_create_user_with_bad_char_id")
        logger.debug("-----------------------------------------")
        #ensure failing to generate character
        main_character_id = 534899860

        user = User.objects.create_user(main_character_id=main_character_id)