# Path: api/models.py
# class ApiClient(AbstractApplication):
#     """
#     This is our custom 'Application' model for Oauth authentication.
#     """
# 
#     agree_tos = models.BooleanField(default=False)
#     password_grant_is_allowed = models.BooleanField(default=False)  # Enable password grant in OAuth2 authentication
#     created = models.DateTimeField(auto_now_add=True)
#     #  Add more custom fields here like description, logo, preferred services...
# 
#     def get_absolute_url(self):
#         return reverse('developers-app-detail', args=[self.id])
# 
#     def clean(self):
#         """
#         The AbstractApplication model includes a clean method that raises a ValidationError
#         if the redirect_uri is empty and is required by the authorization_grant_type. We have
#         implemented a custom version of this check (with a custom message) in api.forms.ApiClientForm
#         therefore we don't need model's clean method to do anything.
#         """
#         pass


from django import forms
from django.forms import ModelForm
from django.forms.fields import Field
from django.utils.text import mark_safe
from django.forms.widgets import HiddenInput
from api.models import ApiClient
setattr(Field, 'is_checkbox', lambda self: isinstance(self.widget, forms.CheckboxInput))  # Used in the template


class ApiClientForm(ModelForm):

    # We define a number of predefined combinations of client type and authorization grant type
    # from which developers can choose when registering a new API client
    PUBLIC_IMPLICIT = 'public_implicit'
    PUBLIC_PASSWORD = 'public_password'
    CONFIDENTIAL_AUTHORIZATION_CODE = 'confidential_authorization-code'
    APPLICATION_TYPES = (
        (PUBLIC_IMPLICIT, 'Public client with implicit grant'),
        (CONFIDENTIAL_AUTHORIZATION_CODE, 'Confidential client with authorization code grant'),
        (PUBLIC_PASSWORD, 'Public client with password grant'),
    )
    application_type = forms.ChoiceField(
        choices=APPLICATION_TYPES,
        help_text=mark_safe('Please read the documentation on '
                            '<a href="/docs/api_authentication.html" target="_blank">'
                            'choosing the application type</a> before selecting an option.'),
    )

    class Meta:

# Path: api/models.py
# class ApiClient(AbstractApplication):
#     """
#     This is our custom 'Application' model for Oauth authentication.
#     """
# 
#     agree_tos = models.BooleanField(default=False)
#     password_grant_is_allowed = models.BooleanField(default=False)  # Enable password grant in OAuth2 authentication
#     created = models.DateTimeField(auto_now_add=True)
#     #  Add more custom fields here like description, logo, preferred services...
# 
#     def get_absolute_url(self):
#         return reverse('developers-app-detail', args=[self.id])
# 
#     def clean(self):
#         """
#         The AbstractApplication model includes a clean method that raises a ValidationError
#         if the redirect_uri is empty and is required by the authorization_grant_type. We have
#         implemented a custom version of this check (with a custom message) in api.forms.ApiClientForm
#         therefore we don't need model's clean method to do anything.
#         """
#         pass


# Path: api/forms.py
from django import forms
from django.forms import ModelForm
from django.forms.fields import Field
from django.utils.text import mark_safe
from django.forms.widgets import HiddenInput
from api.models import ApiClient

setattr(Field, 'is_checkbox', lambda self: isinstance(self.widget, forms.CheckboxInput))  # Used in the template


class ApiClientForm(ModelForm):

    # We define a number of predefined combinations of client type and authorization grant type
    # from which developers can choose when registering a new API client
    PUBLIC_IMPLICIT = 'public_implicit'
    PUBLIC_PASSWORD = 'public_password'
    CONFIDENTIAL_AUTHORIZATION_CODE = 'confidential_authorization-code'
    APPLICATION_TYPES = (
        (PUBLIC_IMPLICIT, 'Public client with implicit grant'),
        (CONFIDENTIAL_AUTHORIZATION_CODE, 'Confidential client with authorization code grant'),
        (PUBLIC_PASSWORD, 'Public client with password grant'),
    )
    application_type = forms.ChoiceField(
        choices=APPLICATION_TYPES,
        help_text=mark_safe('Please read the documentation on '
                            '<a href="/docs/api_authentication.html" target="_blank">'
                            'choosing the application type</a> before selecting an option.'),
    )

    class Meta:

        model = ApiClient