# Path: accounts/models.py
# SKILLS_SEPARATOR = ','
# class StatsFromChallengesMixin:
# class TeamQuerySet(models.QuerySet):
# class Team(models.Model, StatsFromChallengesMixin):
# class UserProfileQuerySet(models.QuerySet):
# class UserProfile(models.Model, StatsFromChallengesMixin):
# class UserTeamRequestQuerySet(models.QuerySet):
# class UserTeamRequest(models.Model):
#     def total_points(self):
#     def num_success(self):
#     def num_fails(self):
#     def num_failed_challenges(self):
#     def num_never_tried_challenges(self):
#     def progress(self):
#     def position(self):
#     def score_over_time(self):
#     def percentage_solved_by_category(self):
#     def annotate_score(self):
#     def ordered(self, *args):
#     def num_users(self):
#     def challengesolved_set(self):
#     def solved_challenges(self):
#     def failed_challenges(self):
#     def __str__(self):
#     def annotate_score(self):
#     def ordered(self, *args):
#     def skill_list(self):
#     def __str__(self):
#     def failed_challenges(self):
#     def pending(self):
#     def accepted(self):
#     def rejected(self):
#     def clean(self):
# def save_user_profile(sender, instance, **kwargs):
# def _send_join_request_to_admin(event, team, from_user=None):
# def join_request_approved_add_to_team(sender, instance, **kwargs):
# def web_socket_notify_join_request(sender, instance, created, **kwargs):
# def web_socket_notify_join_request_delete(sender, instance, **kwargs):
# 
# Path: accounts/forms.py
# 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 UserTeamRequestCreateForm(ModelForm):
#     class Meta:
#         model = UserTeamRequest
#         fields = ['user', 'team']
# 
# class TeamCreateForm(ModelForm):
#     class Meta:
#         model = Team
#         fields = ['name']
# 
# Path: accounts/serializers.py
# class UserTeamRequestListSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = UserTeamRequest
#         fields = ('datetime', 'user', 'status')
# 
# Path: accounts/utils.py
# def user_without_team(user):
#     return user.profile.team is None
# 
# Path: challenges/models.py
# class Challenge(models.Model):
#     objects = ChallengeQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256)
#     category = models.ForeignKey('challenges.Category', related_name='challenges', on_delete=models.CASCADE)
#     description = models.TextField()
#     key = models.CharField(max_length=256)
#     points = models.IntegerField()
#     difficulty = models.CharField(max_length=1, choices=(
#         ('E', 'Easy'),
#         ('M', 'Medium'),
#         ('H', 'Hard')
#     ))
# 
#     solved_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='solved_challenges',
#                                        through='challenges.ChallengeSolved')
# 
#     failed_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='_all_failed_challenges',
#                                        through='challenges.ChallengeFail')
# 
#     @property
#     def newest_solved(self):
#         return self.challengesolved_set.order_by('-datetime')
# 
#     def __str__(self):
#         return self.name
# 
# Path: challenges/models.py
# class Category(models.Model):
#     name = models.CharField(max_length=256, unique=True)
#     description = models.TextField()
# 
#     class Meta:
#         verbose_name_plural = "categories"
# 
#     def __str__(self):
#         return self.name


import json
from builtins import super
from django.http import HttpResponseForbidden
from django.urls.base import reverse_lazy
from django.views.generic.base import TemplateView
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django_registration.views import RegistrationView
from rest_framework.response import Response
from django.shortcuts import render, redirect
from django.contrib.auth import get_user_model
from rest_framework.viewsets import ModelViewSet
from accounts.models import Team, UserTeamRequest, User, UserProfile
from accounts.forms import CustomRegistrationForm, UserProfileForm, UserTeamRequestCreateForm, TeamCreateForm
from accounts.serializers import UserTeamRequestListSerializer
from accounts.utils import user_without_team
from challenges.models import Challenge
from challenges.models import Category
    success_url = '/'
    form_name = None

    # TODO: use permissions
    def get(self, *args, **kwargs):
        return redirect('no_team')

    def form_invalid(self, form):
        return no_team_view(self.request, **{self.form_name: form})


class TeamCreateView(NoTeamPostView):
    form_class = TeamCreateForm
    success_url = reverse_lazy('index')
    form_name = 'create_form'

    def form_valid(self, form):
        team=form.save(commit=False)
        team.created_by=self.request.user
        team.save()
        team.created_by.profile.team = team
        team.created_by.profile.save()
        return super(TeamCreateView, self).form_valid(form)


class UserTeamRequestCreate(NoTeamPostView):
    form_class = UserTeamRequestCreateForm
    form_name = 'join_form'

    def get_form(self):

# Path: accounts/models.py
# SKILLS_SEPARATOR = ','
# class StatsFromChallengesMixin:
# class TeamQuerySet(models.QuerySet):
# class Team(models.Model, StatsFromChallengesMixin):
# class UserProfileQuerySet(models.QuerySet):
# class UserProfile(models.Model, StatsFromChallengesMixin):
# class UserTeamRequestQuerySet(models.QuerySet):
# class UserTeamRequest(models.Model):
#     def total_points(self):
#     def num_success(self):
#     def num_fails(self):
#     def num_failed_challenges(self):
#     def num_never_tried_challenges(self):
#     def progress(self):
#     def position(self):
#     def score_over_time(self):
#     def percentage_solved_by_category(self):
#     def annotate_score(self):
#     def ordered(self, *args):
#     def num_users(self):
#     def challengesolved_set(self):
#     def solved_challenges(self):
#     def failed_challenges(self):
#     def __str__(self):
#     def annotate_score(self):
#     def ordered(self, *args):
#     def skill_list(self):
#     def __str__(self):
#     def failed_challenges(self):
#     def pending(self):
#     def accepted(self):
#     def rejected(self):
#     def clean(self):
# def save_user_profile(sender, instance, **kwargs):
# def _send_join_request_to_admin(event, team, from_user=None):
# def join_request_approved_add_to_team(sender, instance, **kwargs):
# def web_socket_notify_join_request(sender, instance, created, **kwargs):
# def web_socket_notify_join_request_delete(sender, instance, **kwargs):
# 
# Path: accounts/forms.py
# 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 UserTeamRequestCreateForm(ModelForm):
#     class Meta:
#         model = UserTeamRequest
#         fields = ['user', 'team']
# 
# class TeamCreateForm(ModelForm):
#     class Meta:
#         model = Team
#         fields = ['name']
# 
# Path: accounts/serializers.py
# class UserTeamRequestListSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = UserTeamRequest
#         fields = ('datetime', 'user', 'status')
# 
# Path: accounts/utils.py
# def user_without_team(user):
#     return user.profile.team is None
# 
# Path: challenges/models.py
# class Challenge(models.Model):
#     objects = ChallengeQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256)
#     category = models.ForeignKey('challenges.Category', related_name='challenges', on_delete=models.CASCADE)
#     description = models.TextField()
#     key = models.CharField(max_length=256)
#     points = models.IntegerField()
#     difficulty = models.CharField(max_length=1, choices=(
#         ('E', 'Easy'),
#         ('M', 'Medium'),
#         ('H', 'Hard')
#     ))
# 
#     solved_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='solved_challenges',
#                                        through='challenges.ChallengeSolved')
# 
#     failed_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='_all_failed_challenges',
#                                        through='challenges.ChallengeFail')
# 
#     @property
#     def newest_solved(self):
#         return self.challengesolved_set.order_by('-datetime')
# 
#     def __str__(self):
#         return self.name
# 
# Path: challenges/models.py
# class Category(models.Model):
#     name = models.CharField(max_length=256, unique=True)
#     description = models.TextField()
# 
#     class Meta:
#         verbose_name_plural = "categories"
# 
#     def __str__(self):
#         return self.name


# Path: accounts/views.py
import json
from builtins import super
from django.http import HttpResponseForbidden
from django.urls.base import reverse_lazy
from django.views.generic.base import TemplateView
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django_registration.views import RegistrationView
from rest_framework.response import Response
from django.shortcuts import render, redirect
from django.contrib.auth import get_user_model
from rest_framework.viewsets import ModelViewSet
from accounts.models import Team, UserTeamRequest, User, UserProfile
from accounts.forms import CustomRegistrationForm, UserProfileForm, UserTeamRequestCreateForm, TeamCreateForm
from accounts.serializers import UserTeamRequestListSerializer
from accounts.utils import user_without_team
from challenges.models import Challenge
from challenges.models import Category

    success_url = '/'
    form_name = None

    # TODO: use permissions
    def get(self, *args, **kwargs):
        return redirect('no_team')

    def form_invalid(self, form):
        return no_team_view(self.request, **{self.form_name: form})


class TeamCreateView(NoTeamPostView):
    form_class = TeamCreateForm
    success_url = reverse_lazy('index')
    form_name = 'create_form'

    def form_valid(self, form):
        team=form.save(commit=False)
        team.created_by=self.request.user
        team.save()
        team.created_by.profile.team = team
        team.created_by.profile.save()
        return super(TeamCreateView, self).form_valid(form)


class UserTeamRequestCreate(NoTeamPostView):
    form_class = UserTeamRequestCreateForm
    form_name = 'join_form'

    def get_form(self):

        return UserTeamRequestCreateForm(data=dict(