# Path: account/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     profile_picture = models.ImageField(upload_to=user_profile_picture, null=True, blank=True)
#     description = models.TextField(null=True, blank=True)
#     roll_number = models.CharField(max_length=16, null=True, blank=True)
#     type = models.CharField(max_length=16, null=True, blank=True)
#     mobile = models.CharField(max_length=16, null=True, blank=True)
#     is_alumni = models.BooleanField(default=False)
#     sex = models.CharField(max_length=10, choices=SEXES, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.user.username
# 
# Path: application/models.py
# class Application(AbstractApplication):
#     """
#     Extended oauth2_provider.models.AbstractApplication to include application description and logo
#     """
#     description = models.TextField()
#     logo = models.ImageField(upload_to=application_logo, blank=True, null=True)
#     is_anonymous = models.BooleanField(default=False,
#                                        help_text='Valid for complete anonymous apps. Requires admin permission')
#     required_scopes = models.CharField(max_length=256,
#                                        help_text='Default non-tracking permissions. '
#                                                  'Valid only if application is anonymous', null=True, blank=True)
#     private_scopes = models.CharField(max_length=256, help_text='Private API scopes', null=True, blank=True)
#     website = models.URLField(null=True, blank=True)
#     privacy_policy = models.URLField(null=True, blank=True, help_text='Link of privacy policy of application')
#     created_on = models.DateTimeField(auto_now_add=True)
#     modified_on = models.DateTimeField(auto_now=True)
#     _history_ = HistoricalRecords()
# 
#     def get_logo_url(self):
#         try:
#             url = self.logo.url
#             url = urljoin(settings.MEDIA_URL, url)
#         except ValueError:
#             url = None
#         return url
# 
#     def get_absolute_url(self):
#         return reverse('oauth2_provider:detail', args=[str(self.id)])
# 
#     def clean(self):
#         super(Application, self).clean()
#         if self.authorization_grant_type == AbstractApplication.GRANT_PASSWORD:
#             error = _("Resource owner password based grants are not allowed for now")
#             raise ValidationError(error)
# 
#     def get_user_count(self):
#         return AccessToken.objects.all().filter(application=self).values_list('user', flat=True).distinct().count()
# 
#     def __str__(self):
#         if self.name:
#             return self.name
#         else:
#             return self.client_id
# 
# Path: core/templatetags/absolute_url.py
# @register.simple_tag(takes_context=True)
# def absolute_url(context, location):
#     request = context['request']
#     return request.build_absolute_uri(location)
# 
# Path: core/templatetags/model_media.py
# @register.simple_tag()
# def model_field_media_url(field):
#     try:
#         url = field.url
#         url = urljoin(settings.MEDIA_URL, url)
#     except ValueError:
#         url = None
#     return url
# 
# Path: core/utils.py
# class TabNav(object):
#     def __init__(self, tab, name=None, template_name=None, base_url=None, is_default=False):
#         if not tab or not base_url:
#             raise ValueError('tab and base_url cannot be None')
#         self.tab = tab
#         if not name:
#             self.name = tab.title()
#         else:
#             self.name = name
#         if not template_name:
#             self.template_name = '%s.html' % tab
#         else:
#             self.template_name = template_name
#         self.is_default = is_default
#         self.base_url = base_url
#         self.is_active = False
# 
#     @property
#     def url(self):
#         if not self.is_default:
#             return reverse(self.base_url, args=[self.tab])
#         return reverse(self.base_url)
# 
#     @property
#     def tab_name(self):
#         if not self.is_default:
#             return self.tab
#         return ''
# 
# def attr_to_dict(instance, key=None):
#     """
#     Convert an instance to dictionary by following rules:
#     1. If instance is model then covert it directly to dictionary using forms.model_to_dict
#     2. If instance is dictionary or dict like object, return it
#     3. If instace is string type or list type, return {key: instance}
#     :param instance: A python object
#     :param key: @str
#     :return: @dict
#     """
#     if isinstance(instance, Model):
#         return model_to_dict(instance)
#     if isinstance(instance, collections.Mapping):
#         return instance
#     if isinstance(instance, six.string_types):
#         assert key is not None, _('Key cannot be None when instance is not a Model or collections.Mapping type')
#         return {key: instance}
# 
# def get_default_scopes(application):
#     if application.is_anonymous:
#         return application.required_scopes.split()
#     return settings.OAUTH2_DEFAULT_SCOPES


import django.utils.six as six
from django.conf import settings
from django.contrib.auth.models import User
from django.test import RequestFactory, TestCase, override_settings
from account.models import UserProfile
from application.models import Application
from .templatetags.absolute_url import absolute_url
from .templatetags.model_media import model_field_media_url
from .utils import TabNav, attr_to_dict, get_default_scopes




class CoreUtilsTest(TestCase):
    @override_settings(OAUTH2_DEFAULT_SCOPES=['basic', 'profile'])
    def test_default_scopes(self):
        application1 = Application(
            name='application1',
            description='testapp',
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
            redirect_uris='http://localhost:7000/',
            is_anonymous=True,
            required_scopes='send_mail picture',
        )

# Path: account/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     profile_picture = models.ImageField(upload_to=user_profile_picture, null=True, blank=True)
#     description = models.TextField(null=True, blank=True)
#     roll_number = models.CharField(max_length=16, null=True, blank=True)
#     type = models.CharField(max_length=16, null=True, blank=True)
#     mobile = models.CharField(max_length=16, null=True, blank=True)
#     is_alumni = models.BooleanField(default=False)
#     sex = models.CharField(max_length=10, choices=SEXES, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.user.username
# 
# Path: application/models.py
# class Application(AbstractApplication):
#     """
#     Extended oauth2_provider.models.AbstractApplication to include application description and logo
#     """
#     description = models.TextField()
#     logo = models.ImageField(upload_to=application_logo, blank=True, null=True)
#     is_anonymous = models.BooleanField(default=False,
#                                        help_text='Valid for complete anonymous apps. Requires admin permission')
#     required_scopes = models.CharField(max_length=256,
#                                        help_text='Default non-tracking permissions. '
#                                                  'Valid only if application is anonymous', null=True, blank=True)
#     private_scopes = models.CharField(max_length=256, help_text='Private API scopes', null=True, blank=True)
#     website = models.URLField(null=True, blank=True)
#     privacy_policy = models.URLField(null=True, blank=True, help_text='Link of privacy policy of application')
#     created_on = models.DateTimeField(auto_now_add=True)
#     modified_on = models.DateTimeField(auto_now=True)
#     _history_ = HistoricalRecords()
# 
#     def get_logo_url(self):
#         try:
#             url = self.logo.url
#             url = urljoin(settings.MEDIA_URL, url)
#         except ValueError:
#             url = None
#         return url
# 
#     def get_absolute_url(self):
#         return reverse('oauth2_provider:detail', args=[str(self.id)])
# 
#     def clean(self):
#         super(Application, self).clean()
#         if self.authorization_grant_type == AbstractApplication.GRANT_PASSWORD:
#             error = _("Resource owner password based grants are not allowed for now")
#             raise ValidationError(error)
# 
#     def get_user_count(self):
#         return AccessToken.objects.all().filter(application=self).values_list('user', flat=True).distinct().count()
# 
#     def __str__(self):
#         if self.name:
#             return self.name
#         else:
#             return self.client_id
# 
# Path: core/templatetags/absolute_url.py
# @register.simple_tag(takes_context=True)
# def absolute_url(context, location):
#     request = context['request']
#     return request.build_absolute_uri(location)
# 
# Path: core/templatetags/model_media.py
# @register.simple_tag()
# def model_field_media_url(field):
#     try:
#         url = field.url
#         url = urljoin(settings.MEDIA_URL, url)
#     except ValueError:
#         url = None
#     return url
# 
# Path: core/utils.py
# class TabNav(object):
#     def __init__(self, tab, name=None, template_name=None, base_url=None, is_default=False):
#         if not tab or not base_url:
#             raise ValueError('tab and base_url cannot be None')
#         self.tab = tab
#         if not name:
#             self.name = tab.title()
#         else:
#             self.name = name
#         if not template_name:
#             self.template_name = '%s.html' % tab
#         else:
#             self.template_name = template_name
#         self.is_default = is_default
#         self.base_url = base_url
#         self.is_active = False
# 
#     @property
#     def url(self):
#         if not self.is_default:
#             return reverse(self.base_url, args=[self.tab])
#         return reverse(self.base_url)
# 
#     @property
#     def tab_name(self):
#         if not self.is_default:
#             return self.tab
#         return ''
# 
# def attr_to_dict(instance, key=None):
#     """
#     Convert an instance to dictionary by following rules:
#     1. If instance is model then covert it directly to dictionary using forms.model_to_dict
#     2. If instance is dictionary or dict like object, return it
#     3. If instace is string type or list type, return {key: instance}
#     :param instance: A python object
#     :param key: @str
#     :return: @dict
#     """
#     if isinstance(instance, Model):
#         return model_to_dict(instance)
#     if isinstance(instance, collections.Mapping):
#         return instance
#     if isinstance(instance, six.string_types):
#         assert key is not None, _('Key cannot be None when instance is not a Model or collections.Mapping type')
#         return {key: instance}
# 
# def get_default_scopes(application):
#     if application.is_anonymous:
#         return application.required_scopes.split()
#     return settings.OAUTH2_DEFAULT_SCOPES


# Path: core/tests.py
import django.utils.six as six
from django.conf import settings
from django.contrib.auth.models import User
from django.test import RequestFactory, TestCase, override_settings
from account.models import UserProfile
from application.models import Application
from .templatetags.absolute_url import absolute_url
from .templatetags.model_media import model_field_media_url
from .utils import TabNav, attr_to_dict, get_default_scopes





class CoreUtilsTest(TestCase):
    @override_settings(OAUTH2_DEFAULT_SCOPES=['basic', 'profile'])
    def test_default_scopes(self):
        application1 = Application(
            name='application1',
            description='testapp',
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
            redirect_uris='http://localhost:7000/',
            is_anonymous=True,
            required_scopes='send_mail picture',
        )

        six.assertCountEqual(self, ['send_mail', 'picture'], get_default_scopes(application1))