def superuser_required(view_func=None,
                       redirect_field_name=REDIRECT_FIELD_NAME,
                       login_url="account:login"):
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_admin or u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name,
    )
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator
Exemplo n.º 2
0
def superuser_required(view_func=None, login_url='dashboard'):
    """
    Decorator for views that checks that the user is logged in and is a
    superuser, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name='permission')
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator
Exemplo n.º 3
0
class SuperUserMixin(object):
    connection = None

    @method_decorator(user_passes_test(lambda u: u.is_superuser))
    def dispatch(self, request, *args, **kwargs):
        opts = getattr(settings, 'RQ', {}).copy()
        opts.pop('eager', None)

        with Connection(connection=redis.Redis(**opts)) as connection:
            self.connection = connection
            return super(SuperUserMixin,
                         self).dispatch(request, *args, **kwargs)
Exemplo n.º 4
0
def superuser_required(
        view_func=None, redirect_field_name=REDIRECT_FIELD_NAME,
        login_url='account:login'):
    """Check if the user is logged in and is a superuser.

    Otherwise redirects to the login page.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name)
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator
Exemplo n.º 5
0
def superuser_required(view_func=None,
                       redirect_field_name=REDIRECT_FIELD_NAME,
                       login_url='account_login'):
    """
    Decorator for views that checks that the user is logged in and is a
    superuser, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name)
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator
Exemplo n.º 6
0
def superuser_required(view_func=None,
                       redirect_field_name=REDIRECT_FIELD_NAME,
                       login_url='account:login'):
    """Check if the user is logged in and is a superuser.

    Otherwise redirects to the login page.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name)
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator
Exemplo n.º 7
0
def superuser_required(function=None,
                       redirect_field_name=REDIRECT_FIELD_NAME,
                       login_url=None):
    """
    Decorator for views that checks that the user is superuser, redirecting
    to the log-in page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name)
    if function:
        return actual_decorator(function)
    return actual_decorator
Exemplo n.º 8
0
def superuser_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME,
                       login_url=None):
    """
    Decorator for views that checks that the user is logged in and is a
    superuser, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )

    if view_func:
        return actual_decorator(view_func)
    return actual_decorator
Exemplo n.º 9
0
class HomeView(View):
    @method_decorator(login_required)
    def get(self, request):
        current_date = datetime.date.today()
        active = Tournament.objects.filter(start_date__lte=current_date,
                                           end_date__gte=current_date)
        upcoming = Tournament.objects.filter(start_date__gt=current_date)

        return render(request, 'polls/index.html', {
            'active': active,
            'upcoming': upcoming
        })


super = user_passes_test(lambda u: u.is_superuser)


class CreateTournamentView(View):
    @method_decorator(super)
    def get(self, request):
        response = requests.get('https://opentdb.com/api_category.php')
        data = json.loads(response.content)
        for cat in data['trivia_categories']:
            categories["%d" % cat["id"]] = cat['name']
        return render(request, 'polls/create_tourny.html',
                      {'categories': categories})

    @method_decorator(super)
    def post(self, request):
        name = request.POST.get('name')
Exemplo n.º 10
0
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import user_passes_test
from django.contrib.auth.models import User
from django.utils.decorators import method_decorator
from django.views import View

#Imports the appropriate models.
from .models import Tournament, Question, Answer, HighScore

#Checks if the user is a super user.
can_create = user_passes_test(lambda u: u.is_superuser)
# user: admin
# pass: admin123


class SignUpView(View):
    def post(self, request):
        #Gets user information from form and creates a user.
        user = User.objects.create_user(
            first_name=request.POST.get('firstname'),
            last_name=request.POST.get('lastname'),
            email=request.POST.get('email'),
            username=request.POST.get('username'),
            password=request.POST.get('password'))
        #Saves the created user to the database.
        user.save()
Exemplo n.º 11
0
from django.conf.urls import patterns, include, url
from django.contrib.admin.views.decorators import user_passes_test

from .views import *

urlpatterns = patterns('',
    url(r'^$',                              user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(Portal.as_view()),name='AdminPortal'),
    url(r'^user/$',                         user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(UsersList.as_view()), name='AdminUsers'),
    url(r'^user/create/$',                  user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(UserCreation.as_view()), name='AdminUserCreation'),
    url(r'^user/(?P<pk>\d+)/$',             user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(UserDetails.as_view()), name='AdminUsersDetails'),
    url(r'^user/delete/(?P<pk>\d+)/$',      user_passes_test(lambda u: u.is_staff, login_url='/',
                                                              redirect_field_name='')(UserDeletion.as_view()), name='AdminUserDelete'),
    url(r'^forum/$',                        user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(ForumList.as_view()), name='AdminForum'),
    url(r'^forum/create/childcategory/$',   user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(CategoryCreation.as_view()), name='AdminChildcategoryCreation'),
    url(r'^forum/create/category/$',        user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(CategoryCreation.as_view()), name='AdminCategoryCreation'),
    url(r'^forum/(?P<pk>\d+)/$',            user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(ForumDetails.as_view()), name='AdminForumDetails'),
    url(r'^tickets/$',                      user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(TicketList.as_view()), name='AdminTickets'),
    url(r'^tickets/(?P<pk>\d+)/$',          user_passes_test(lambda u: u.is_staff, login_url='/',
                                                             redirect_field_name='')(TicketDetails.as_view()), name='AdminTicketsDetails'),
)
Exemplo n.º 12
0
from django.shortcuts import render, redirect
from django.http.response import JsonResponse
from django.utils.decorators import method_decorator
from django.http import HttpResponse, HttpResponseBadRequest
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.admin.views.decorators import staff_member_required, user_passes_test

from distance import forms as distance_forms
from distance import models as distance_models

from django.contrib.auth import get_user_model
User = get_user_model()

extended_perms = [
    login_required,
    user_passes_test(lambda u: u.is_superuser, login_url='forbidden'),
]


@method_decorator(extended_perms, name='dispatch')
class ExtendedAPI(View):
    related_delimiter = '.'
    delimiter = ','
    k_v_delimiter = ':'
    model = None

    # e.g. /transactions/?filter=user__gt:1,cost__lt:0
    def filter(self, request, queryset):
        if not request.GET.get('filter'): return queryset

        raw_filters = request.GET.get('filter').split(self.delimiter)
Exemplo n.º 13
0
from django.conf.urls import patterns, include, url
from django.contrib.admin.views.decorators import user_passes_test
from django.contrib.auth.forms import UserCreationForm
from django.views.generic.edit import CreateView

from .views import *

urlpatterns = patterns('',
    url(r'^$', Index.as_view(), name='home'),
    url(r'^contact/$', Contact.as_view(), name='contact'),
    url(r'^signup/$', user_passes_test(lambda u: not u.is_authenticated(), login_url='/',
                                       redirect_field_name='')(CreateView.as_view(template_name='signup.html',
                                                                                  form_class=UserCreationForm,
                                                                                  success_url='/signin/'))),
    url(r'^signin/$', 'apps.core.views.signin', {'template_name': 'signin.html'}, name='signin'),
    url(r'^signout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}, name='signout'),

    (r'^i18n/', include('django.conf.urls.i18n')),
)
Exemplo n.º 14
0
from django.core.urlresolvers import reverse_lazy
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.admin.views.decorators import user_passes_test
from django.views.generic import RedirectView

from app.views import *


urlpatterns = patterns('', url(r'^$', Index.as_view(), name='home'),
    url(r'^contact/$',                          Contact.as_view(), name='contact'),
    url(r'^signup/$',                           SignUp.as_view(), name='signup'),
    url(r'^signin/$',                           'app.views.signin', {'template_name': 'signin.html'}, name='signin'),
    url(r'^signout/$',                          'django.contrib.auth.views.logout', {'next_page': '/'}, name='signout'),

    url(r'^admin/$',                            RedirectView.as_view(url=reverse_lazy('AdminUsers')), name='AdminDefault'),

    url(r'^admin/user/$',                       user_passes_test(lambda u: u.is_staff, login_url='/',
                                                                 redirect_field_name='')(UsersList.as_view()), name='AdminUsers'),
    url(r'^admin/user/create/$',                user_passes_test(lambda u: u.is_staff, login_url='/',
                                                                 redirect_field_name='')(UserCreation.as_view()), name='AdminUserCreation'),
    url(r'^admin/user/(?P<pk>\d+)/$',           user_passes_test(lambda u: u.is_staff, login_url='/',
                                                                 redirect_field_name='')(UserDetails.as_view()), name='AdminUsersDetails'),
    url(r'^admin/user/delete/(?P<pk>\d+)/$',    user_passes_test(lambda u: u.is_staff, login_url='/',
                                                                 redirect_field_name='')(UserDeletion.as_view()), name='AdminUserDelete'),

    url(r'^djangoadmin/', include(admin.site.urls)),
)