# 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/authenticate.py
# def authenticate(username, password):
#     url = 'https://authserver.mojang.com/authenticate'
# 
#     payload = {
#         'agent': {
#             'name': 'Minecraft',
#             'version': 1,
#         },
#         'username': username,
#         'password': password,
#     }
# 
#     headers = {
#         'Content-Type': 'application/json',
#         'Accept': 'application/json',
#     }
# 
#     try:
#         request = urllib2.Request(url, json.dumps(payload), headers)
#         response = urllib2.urlopen(request)
# 
#         try:
#             response_payload = json.loads(response.read())
# 
#         finally:
#             response.close()  # always close socket
# 
#         return response_payload
# 
#     except urllib2.HTTPError as e:
#         raise MinecraftAuthenticationError(e)


from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import MinecraftAccount
from .authenticate import authenticate


class MinecraftAccountForm(forms.Form):
    username = forms.CharField(label=_('Username'))
    password = forms.CharField(
        label=_('Password'),
        help_text=_('Your password will not be stored in our database'),
        widget=forms.PasswordInput)

    class Meta:
        fields = ()

    def __init__(self, *args, **kwargs):
        super(MinecraftAccountForm, self).__init__(*args, **kwargs)
        self.profiles = []

    def clean(self):
        cleaned_data = super(MinecraftAccountForm, self).clean()


# 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/authenticate.py
# def authenticate(username, password):
#     url = 'https://authserver.mojang.com/authenticate'
# 
#     payload = {
#         'agent': {
#             'name': 'Minecraft',
#             'version': 1,
#         },
#         'username': username,
#         'password': password,
#     }
# 
#     headers = {
#         'Content-Type': 'application/json',
#         'Accept': 'application/json',
#     }
# 
#     try:
#         request = urllib2.Request(url, json.dumps(payload), headers)
#         response = urllib2.urlopen(request)
# 
#         try:
#             response_payload = json.loads(response.read())
# 
#         finally:
#             response.close()  # always close socket
# 
#         return response_payload
# 
#     except urllib2.HTTPError as e:
#         raise MinecraftAuthenticationError(e)


# Path: mineid/mcaccounts/forms.py
from django import forms
from django.utils.translation import ugettext_lazy as _
from .models import MinecraftAccount
from .authenticate import authenticate



class MinecraftAccountForm(forms.Form):
    username = forms.CharField(label=_('Username'))
    password = forms.CharField(
        label=_('Password'),
        help_text=_('Your password will not be stored in our database'),
        widget=forms.PasswordInput)

    class Meta:
        fields = ()

    def __init__(self, *args, **kwargs):
        super(MinecraftAccountForm, self).__init__(*args, **kwargs)
        self.profiles = []

    def clean(self):
        cleaned_data = super(MinecraftAccountForm, self).clean()


        username = self.cleaned_data.get('username')