# Path: accounts/utils.py
# def user_without_team(user):
#     return user.profile.team is None


from django.urls import resolve, reverse_lazy
from rest_framework.reverse import reverse
from django.utils.functional import curry
from django.views.defaults import *
from django.http import HttpResponseRedirect
from django.utils.deprecation import MiddlewareMixin
from accounts.utils import user_without_team


class LoggedInUserWithoutProfileMiddleware(FilterRequestMiddlewareMixin):
    allowed_paths = [
        reverse_lazy('logout')
    ]

    def custom_filter(self, request):
        return not hasattr(request.user, 'profile')

    def response(self, request):
        return curry(server_error,
                     template_name='accounts/500_no_profile.html')(request)


class LoggedInUserWithoutTeamMiddleware(FilterRequestMiddlewareMixin):
    redirect_url = reverse('no_team')

    allowed_paths = [
        reverse('no_team'),
        #reverse('api-accounts:team-create-list'),
        reverse('user_team_request_create'),
        reverse('user_team_create')
    ]

    allowed_views = [
        'user_team_request_delete'
    ]

    def custom_filter(self, request):

# Path: accounts/utils.py
# def user_without_team(user):
#     return user.profile.team is None


# Path: accounts/middleware.py
from django.urls import resolve, reverse_lazy
from rest_framework.reverse import reverse
from django.utils.functional import curry
from django.views.defaults import *
from django.http import HttpResponseRedirect
from django.utils.deprecation import MiddlewareMixin
from accounts.utils import user_without_team



class LoggedInUserWithoutProfileMiddleware(FilterRequestMiddlewareMixin):
    allowed_paths = [
        reverse_lazy('logout')
    ]

    def custom_filter(self, request):
        return not hasattr(request.user, 'profile')

    def response(self, request):
        return curry(server_error,
                     template_name='accounts/500_no_profile.html')(request)


class LoggedInUserWithoutTeamMiddleware(FilterRequestMiddlewareMixin):
    redirect_url = reverse('no_team')

    allowed_paths = [
        reverse('no_team'),
        #reverse('api-accounts:team-create-list'),
        reverse('user_team_request_create'),
        reverse('user_team_create')
    ]

    allowed_views = [
        'user_team_request_delete'
    ]

    def custom_filter(self, request):

        return user_without_team(request.user)