"""
Utility functions and classes
"""
from django.contrib.auth.views import redirect_to_login
from django.core.exceptions import PermissionDenied
from django.contrib.auth.mixins import UserPassesTestMixin
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.http import urlencode


class OSFAdmin(UserPassesTestMixin):
    login_url = settings.LOGIN_URL
    permission_denied_message = 'You are not in the OSF admin group.'

    def handle_no_permission(self):
        if not self.request.user.is_authenticated():
            return redirect_to_login(self.request.get_full_path(),
                                     self.get_login_url(),
                                     self.get_redirect_field_name())
        else:
            raise PermissionDenied(self.get_permission_denied_message())

    def test_func(self):
        return self.request.user.is_authenticated() and (self.request.user.is_in_group('osf_admin') or self.request.user.is_superuser)


class NodesAndUsers(OSFAdmin):
    """User needs to be in the nodes_and_users group to be able to access views with node
    and user information. Specific admin permissions to be checked template side
    """
    permission_denied_message = 'You are not allowed to access information about Nodes and Users on the OSF Admin.'

    def test_func(self):
        return self.request.user.is_authenticated() and (self.request.user.is_in_group('nodes_and_users') or self.request.user.is_superuser)


class SuperUser(OSFAdmin):
    permission_denied_message = (
        'You are not a superuser,'
        ' please contact one in order to do this action.'
    )

    def test_func(self):
        return self.request.user.is_authenticated() and self.request.user.is_superuser


class Prereg(OSFAdmin):
    """For testing for Prereg credentials of user."""
    permission_denied_message = 'You are not in the Pre-reg admin group.'

    def test_func(self):
        return self.request.user.is_authenticated() and (self.request.user.is_in_group('prereg') or self.request.user.is_superuser)


def reverse_qs(view, urlconf=None, args=None, kwargs=None, current_app=None, query_kwargs=None):
    base_url = reverse(view, urlconf=urlconf, args=args, kwargs=kwargs, current_app=current_app)
    if query_kwargs:
        return '{}?{}'.format(base_url, urlencode(query_kwargs))


def osf_admin_check(user):
    return user.is_authenticated() and user.groups.filter(name='osf_admin')

@@ -49,2037 +49,78 @@
 o.co
-ntrib.auth.views import redirect_to_login%0Afrom django.core.exceptions import PermissionDenied%0Afrom django.contrib.auth.mixins import UserPassesTestMixin%0Afrom django.conf import settings%0Afrom django.core.urlresolvers import reverse%0Afrom django.utils.http import urlencode%0A%0A%0Aclass OSFAdmin(UserPassesTestMixin):%0A    login_url = settings.LOGIN_URL%0A    permission_denied_message = 'You are not in the OSF admin group.'%0A%0A    def handle_no_permission(self):%0A        if not self.request.user.is_authenticated():%0A            return redirect_to_login(self.request.get_full_path(),%0A                                     self.get_login_url(),%0A                                     self.get_redirect_field_name())%0A        else:%0A            raise PermissionDenied(self.get_permission_denied_message())%0A%0A    def test_func(self):%0A        return self.request.user.is_authenticated() and (self.request.user.is_in_group('osf_admin') or self.request.user.is_superuser)%0A%0A%0Aclass NodesAndUsers(OSFAdmin):%0A    %22%22%22User needs to be in the nodes_and_users group to be able to access views with node%0A    and user information. Specific admin permissions to be checked template side%0A    %22%22%22%0A    permission_denied_message = 'You are not allowed to access information about Nodes and Users on the OSF Admin.'%0A%0A    def test_func(self):%0A        return self.request.user.is_authenticated() and (self.request.user.is_in_group('nodes_and_users') or self.request.user.is_superuser)%0A%0A%0Aclass SuperUser(OSFAdmin):%0A    permission_denied_message = (%0A        'You are not a superuser,'%0A        ' please contact one in order to do this action.'%0A    )%0A%0A    def test_func(self):%0A        return self.request.user.is_authenticated() and self.request.user.is_superuser%0A%0A%0Aclass Prereg(OSFAdmin):%0A    %22%22%22For testing for Prereg credentials of user.%22%22%22%0A    permission_denied_message = 'You are not in the Pre-reg admin group.'%0A%0A    def test_func(self):%0A        return self.request.user.is_authenticated() and (self.request.user.is_in_group('prereg') or self.request.user.is_superuser)
+re.urlresolvers import reverse%0Afrom django.utils.http import urlencode
 %0A%0A%0Ad

Remove permission check classes that used groups