# Path: mineid/mcaccounts/models.py
# class MinecraftAccount(models.Model):
#     user = models.ForeignKey(
#         get_user_model(), related_name='minecraft_accounts')
#     profile = models.CharField(max_length=75)
#     profile_id = models.CharField(max_length=32, unique=True)
#     date_added = models.DateTimeField(auto_now_add=True)
#     primary = models.BooleanField(default=False)
# 
#     objects = MinecraftAccountManager()
# 
#     class Meta:
#         unique_together = ('user', 'primary')
# 
#     def get_avatar(self, geometry_string):
#         return get_minecraft_avatar(self.profile, geometry_string)
# 
#     def get_api_dict(self, base_url):
#         return {
#             'name': self.profile,
#             'uid': self.profile_id,
#             'avatar': base_url + self.get_avatar('64x64'),
#         }
# 
# Path: mineid/mcaccounts/exceptions.py
# class UserAlreadyExists(Exception):
#     pass


from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.contrib.auth import get_user_model
from .models import MinecraftAccount
from .exceptions import UserAlreadyExists



User = get_user_model()


def create_minecraft_user(username, profiles, primary):
    try:
        validate_email(username)
    except ValidationError:
        email = None
    else:
        email = username

    # This user can only log in through Minecraft
    # authentication backend
    user = User(username=username, email=email)
    user.set_unusable_password()

    try:
        user.save()
    except IntegrityError:
        raise UserAlreadyExists

    # Save profiles
    for profile in profiles:
        MinecraftAccount.objects.create(
            user=user,

# Path: mineid/mcaccounts/models.py
# class MinecraftAccount(models.Model):
#     user = models.ForeignKey(
#         get_user_model(), related_name='minecraft_accounts')
#     profile = models.CharField(max_length=75)
#     profile_id = models.CharField(max_length=32, unique=True)
#     date_added = models.DateTimeField(auto_now_add=True)
#     primary = models.BooleanField(default=False)
# 
#     objects = MinecraftAccountManager()
# 
#     class Meta:
#         unique_together = ('user', 'primary')
# 
#     def get_avatar(self, geometry_string):
#         return get_minecraft_avatar(self.profile, geometry_string)
# 
#     def get_api_dict(self, base_url):
#         return {
#             'name': self.profile,
#             'uid': self.profile_id,
#             'avatar': base_url + self.get_avatar('64x64'),
#         }
# 
# Path: mineid/mcaccounts/exceptions.py
# class UserAlreadyExists(Exception):
#     pass


# Path: mineid/mcaccounts/utils.py
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.contrib.auth import get_user_model
from .models import MinecraftAccount
from .exceptions import UserAlreadyExists




User = get_user_model()


def create_minecraft_user(username, profiles, primary):
    try:
        validate_email(username)
    except ValidationError:
        email = None
    else:
        email = username

    # This user can only log in through Minecraft
    # authentication backend
    user = User(username=username, email=email)
    user.set_unusable_password()

    try:
        user.save()
    except IntegrityError:
        raise UserAlreadyExists

    # Save profiles
    for profile in profiles:
        MinecraftAccount.objects.create(
            user=user,

            profile=profile.get('name'),