# Path: core/utils.py
# def get_default_scopes(application):
#     if application.is_anonymous:
#         return application.required_scopes.split()
#     return settings.OAUTH2_DEFAULT_SCOPES
# 
# Path: application/forms.py
# class RegistrationForm(forms.ModelForm):
#     name = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'class': 'form-control'}))
#     authorization_grant_type = forms.ChoiceField(choices=get_choices_with_blank_dash(Application.GRANT_TYPES[:2]),
#                                                  widget=forms.Select(attrs={'class': 'form-control'}))
# 
#     class Meta:
#         model = Application
#         fields = (
#             'name', 'description', 'logo', 'website', 'privacy_policy', 'client_id', 'client_secret', 'client_type',
#             'authorization_grant_type', 'redirect_uris')
#         widgets = {
#             'description': forms.Textarea(
#                 attrs={'class': 'form-control', 'rows': 3, }
#             ),
#             'logo': forms.ClearableFileInput(
#                 attrs={'class': 'form-control', 'accept': 'image/*', }
#             ),
#             'website': forms.URLInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'privacy_policy': forms.URLInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'client_id': forms.TextInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'client_secret': forms.TextInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'client_type': forms.Select(
#                 attrs={'class': 'form-control', }
#             ),
#             'redirect_uris': forms.Textarea(
#                 attrs={'class': 'form-control', 'rows': 4, }
#             )
#         }


from braces.views import LoginRequiredMixin
from django.views.generic import UpdateView
from oauth2_provider.exceptions import OAuthToolkitError
from oauth2_provider.http import HttpResponseUriRedirect
from oauth2_provider.models import get_application_model as get_oauth2_application_model
from oauth2_provider.settings import oauth2_settings
from oauth2_provider.views import AuthorizationView
from oauth2_provider.views.application import ApplicationRegistration
from core.utils import get_default_scopes
from .forms import RegistrationForm




class ApplicationRegistrationView(ApplicationRegistration):
    form_class = RegistrationForm


class ApplicationUpdateView(LoginRequiredMixin, UpdateView):
    """
    View used to update an application owned by the request.user
    """
    form_class = RegistrationForm
    context_object_name = 'application'
    template_name = "oauth2_provider/application_form.html"

    def get_queryset(self):
        return get_oauth2_application_model().objects.filter(user=self.request.user)


class CustomAuthorizationView(AuthorizationView):

    def form_valid(self, form):
        client_id = form.cleaned_data.get('client_id', '')
        application = get_oauth2_application_model().objects.get(client_id=client_id)
        scopes = form.cleaned_data.get('scope', '')
        scopes = set(scopes.split(' '))

# Path: core/utils.py
# def get_default_scopes(application):
#     if application.is_anonymous:
#         return application.required_scopes.split()
#     return settings.OAUTH2_DEFAULT_SCOPES
# 
# Path: application/forms.py
# class RegistrationForm(forms.ModelForm):
#     name = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'class': 'form-control'}))
#     authorization_grant_type = forms.ChoiceField(choices=get_choices_with_blank_dash(Application.GRANT_TYPES[:2]),
#                                                  widget=forms.Select(attrs={'class': 'form-control'}))
# 
#     class Meta:
#         model = Application
#         fields = (
#             'name', 'description', 'logo', 'website', 'privacy_policy', 'client_id', 'client_secret', 'client_type',
#             'authorization_grant_type', 'redirect_uris')
#         widgets = {
#             'description': forms.Textarea(
#                 attrs={'class': 'form-control', 'rows': 3, }
#             ),
#             'logo': forms.ClearableFileInput(
#                 attrs={'class': 'form-control', 'accept': 'image/*', }
#             ),
#             'website': forms.URLInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'privacy_policy': forms.URLInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'client_id': forms.TextInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'client_secret': forms.TextInput(
#                 attrs={'class': 'form-control', }
#             ),
#             'client_type': forms.Select(
#                 attrs={'class': 'form-control', }
#             ),
#             'redirect_uris': forms.Textarea(
#                 attrs={'class': 'form-control', 'rows': 4, }
#             )
#         }


# Path: application/views.py
from braces.views import LoginRequiredMixin
from django.views.generic import UpdateView
from oauth2_provider.exceptions import OAuthToolkitError
from oauth2_provider.http import HttpResponseUriRedirect
from oauth2_provider.models import get_application_model as get_oauth2_application_model
from oauth2_provider.settings import oauth2_settings
from oauth2_provider.views import AuthorizationView
from oauth2_provider.views.application import ApplicationRegistration
from core.utils import get_default_scopes
from .forms import RegistrationForm





class ApplicationRegistrationView(ApplicationRegistration):
    form_class = RegistrationForm


class ApplicationUpdateView(LoginRequiredMixin, UpdateView):
    """
    View used to update an application owned by the request.user
    """
    form_class = RegistrationForm
    context_object_name = 'application'
    template_name = "oauth2_provider/application_form.html"

    def get_queryset(self):
        return get_oauth2_application_model().objects.filter(user=self.request.user)


class CustomAuthorizationView(AuthorizationView):

    def form_valid(self, form):
        client_id = form.cleaned_data.get('client_id', '')
        application = get_oauth2_application_model().objects.get(client_id=client_id)
        scopes = form.cleaned_data.get('scope', '')
        scopes = set(scopes.split(' '))

        scopes.update(set(get_default_scopes(application)))