# Path: accounts/models.py
# class UserProfile(models.Model, StatsFromChallengesMixin):
#     objects = UserProfileQuerySet.as_manager()
# 
#     team = models.ForeignKey('accounts.Team', null=True, blank=True, on_delete=models.CASCADE)
#     user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
#     created_at = models.DateTimeField(auto_now_add=True)
#     image = models.FileField(upload_to='accounts/', default='accounts/user.png')
#     job = models.CharField(max_length=255)
#     gender = models.CharField(max_length=1, choices=(('M', 'Male'), ('F','Female')))
#     website = models.CharField(max_length=255, null=True, blank=True)
#     country = models.ForeignKey(Country, on_delete=models.CASCADE)
#     skills = models.TextField(null=True, blank=True)
# 
#     @property
#     def skill_list(self):
#         return self.skills.split(SKILLS_SEPARATOR) if self.skills else []
# 
#     def __str__(self):
#         return '{}, Team: {}'.format(self.user.username, self.team)
# 
#     @property
#     def failed_challenges(self):
#         return self._all_failed_challenges.exclude(pk__in=self.solved_challenges.all())
# 
# class UserTeamRequest(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     team = models.ForeignKey(Team, on_delete=models.CASCADE)
#     datetime = models.DateTimeField(auto_now=True)
#     status = models.CharField(max_length=1, default='P', choices=(
#         ('P', 'Pending'),
#         ('A', 'Accepted'),
#         ('R', 'Rejected'),
#     ))
# 
#     objects = UserTeamRequestQuerySet.as_manager()
# 
#     def clean(self):
#         if not user_without_team(self.user):
#             raise ValidationError('User is already a team member')
# 
#         if self.status == 'P' and self.user.userteamrequest_set.filter(status='P').exists():
#             raise ValidationError('Other pending request exists')
# 
#         if self.status == 'P' and self.user.userteamrequest_set.filter(status='A').exists():
#             raise ValidationError('Other accepted request exists')
# 
# class Team(models.Model, StatsFromChallengesMixin):
#     objects = TeamQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256, unique=True)
#     users = models.ManyToManyField(User, through='accounts.userprofile')
#     created_at = models.DateTimeField(auto_now_add=True, editable=False)
#     created_by = models.ForeignKey(User, related_name='created_team', on_delete=models.CASCADE)
# 
#     @property
#     def num_users(self):
#         return self.users.count()
# 
#     @property
#     def challengesolved_set(self):
#         from challenges.models import ChallengeSolved
#         return ChallengeSolved.objects.filter(user__team=self)
# 
#     @property
#     def solved_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects.filter(solved_by__team=self).distinct()
# 
#     @property
#     def failed_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects\
#             .filter(failed_by__team=self)\
#             .distinct()\
#             .exclude(pk__in=self.solved_challenges.all())
# 
#     def __str__(self):
#         return self.name


from django import forms
from django.forms.models import ModelForm
from django_registration.forms import RegistrationForm
from accounts.models import UserProfile, UserTeamRequest, Team
# from registration.forms import RegistrationForm


class CustomRegistrationForm(RegistrationForm):
    first_name = forms.CharField(required=True)
    last_name = forms.CharField(required=True)

    class Meta:
        model = RegistrationForm.Meta.model
        fields = RegistrationForm.Meta.fields + ['first_name', 'last_name']


class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile
        exclude = ['user', 'team']


class TeamCreateForm(ModelForm):

# Path: accounts/models.py
# class UserProfile(models.Model, StatsFromChallengesMixin):
#     objects = UserProfileQuerySet.as_manager()
# 
#     team = models.ForeignKey('accounts.Team', null=True, blank=True, on_delete=models.CASCADE)
#     user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
#     created_at = models.DateTimeField(auto_now_add=True)
#     image = models.FileField(upload_to='accounts/', default='accounts/user.png')
#     job = models.CharField(max_length=255)
#     gender = models.CharField(max_length=1, choices=(('M', 'Male'), ('F','Female')))
#     website = models.CharField(max_length=255, null=True, blank=True)
#     country = models.ForeignKey(Country, on_delete=models.CASCADE)
#     skills = models.TextField(null=True, blank=True)
# 
#     @property
#     def skill_list(self):
#         return self.skills.split(SKILLS_SEPARATOR) if self.skills else []
# 
#     def __str__(self):
#         return '{}, Team: {}'.format(self.user.username, self.team)
# 
#     @property
#     def failed_challenges(self):
#         return self._all_failed_challenges.exclude(pk__in=self.solved_challenges.all())
# 
# class UserTeamRequest(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     team = models.ForeignKey(Team, on_delete=models.CASCADE)
#     datetime = models.DateTimeField(auto_now=True)
#     status = models.CharField(max_length=1, default='P', choices=(
#         ('P', 'Pending'),
#         ('A', 'Accepted'),
#         ('R', 'Rejected'),
#     ))
# 
#     objects = UserTeamRequestQuerySet.as_manager()
# 
#     def clean(self):
#         if not user_without_team(self.user):
#             raise ValidationError('User is already a team member')
# 
#         if self.status == 'P' and self.user.userteamrequest_set.filter(status='P').exists():
#             raise ValidationError('Other pending request exists')
# 
#         if self.status == 'P' and self.user.userteamrequest_set.filter(status='A').exists():
#             raise ValidationError('Other accepted request exists')
# 
# class Team(models.Model, StatsFromChallengesMixin):
#     objects = TeamQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256, unique=True)
#     users = models.ManyToManyField(User, through='accounts.userprofile')
#     created_at = models.DateTimeField(auto_now_add=True, editable=False)
#     created_by = models.ForeignKey(User, related_name='created_team', on_delete=models.CASCADE)
# 
#     @property
#     def num_users(self):
#         return self.users.count()
# 
#     @property
#     def challengesolved_set(self):
#         from challenges.models import ChallengeSolved
#         return ChallengeSolved.objects.filter(user__team=self)
# 
#     @property
#     def solved_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects.filter(solved_by__team=self).distinct()
# 
#     @property
#     def failed_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects\
#             .filter(failed_by__team=self)\
#             .distinct()\
#             .exclude(pk__in=self.solved_challenges.all())
# 
#     def __str__(self):
#         return self.name


# Path: accounts/forms.py
from django import forms
from django.forms.models import ModelForm
from django_registration.forms import RegistrationForm
from accounts.models import UserProfile, UserTeamRequest, Team

# from registration.forms import RegistrationForm


class CustomRegistrationForm(RegistrationForm):
    first_name = forms.CharField(required=True)
    last_name = forms.CharField(required=True)

    class Meta:
        model = RegistrationForm.Meta.model
        fields = RegistrationForm.Meta.fields + ['first_name', 'last_name']


class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile
        exclude = ['user', 'team']


class TeamCreateForm(ModelForm):

    class Meta: