Skip to content
Snippets Groups Projects
views.py 3.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • from django.contrib import messages
    from django.contrib.auth import get_user_model, login, update_session_auth_hash
    
    from django.contrib.auth.decorators import login_required
    
    from django.contrib.auth.forms import AdminPasswordChangeForm
    
    Dan Braghis's avatar
    Dan Braghis committed
    from django.contrib.auth.tokens import PasswordResetTokenGenerator
    from django.shortcuts import redirect, render
    
    from django.template.response import TemplateResponse
    
    Dan Braghis's avatar
    Dan Braghis committed
    from django.urls import reverse_lazy
    
    Dan Braghis's avatar
    Dan Braghis committed
    from django.utils.encoding import force_text
    from django.utils.http import urlsafe_base64_decode
    from django.views.generic.base import TemplateView
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    from hijack.views import login_with_id
    
    
    from wagtail.admin.views.account import password_management_enabled
    
    from .decorators import require_oauth_whitelist
    
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    User = get_user_model()
    
    
    Dan Braghis's avatar
    Dan Braghis committed
    @login_required(login_url=reverse_lazy('users:login'))
    
    def account(request):
    
    Dan Braghis's avatar
    Dan Braghis committed
        """Account page placeholder view"""
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        if request.user.is_superuser:
            swappable = User.objects.filter(is_active=True, is_superuser=False)
        else:
            swappable = []
    
    Dan Braghis's avatar
    Dan Braghis committed
    
        return render(request, 'users/account.html', {
            'show_change_password': password_management_enabled() and request.user.has_usable_password(),
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            'swappable': swappable,
    
    Dan Braghis's avatar
    Dan Braghis committed
        })
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    @login_required(login_url=reverse_lazy('users:login'))
    def become(request):
        if request.POST:
            id = request.POST['user']
            return login_with_id(request, id)
        return redirect('users:account')
    
    
    
    @login_required(login_url=reverse_lazy('users:login'))
    @require_oauth_whitelist
    def oauth(request):
    
    Dan Braghis's avatar
    Dan Braghis committed
        """Generic, empty view for the OAuth associations."""
    
        return TemplateResponse(request, 'users/oauth.html', {})
    
    
    class ActivationView(TemplateView):
        def get(self, request, *args, **kwargs):
    
    Dan Braghis's avatar
    Dan Braghis committed
            user = self.get_user(kwargs.get('uidb64'))
    
            if self.valid(user, kwargs.get('token')):
                user.is_active = True
                user.save()
    
    
    Dan Braghis's avatar
    Dan Braghis committed
                user.backend = 'django.contrib.auth.backends.ModelBackend'
                login(request, user)
    
                return redirect('users:activate_password')
    
    Dan Braghis's avatar
    Dan Braghis committed
    
            return render(request, 'users/activation/invalid.html')
    
    
    Dan Braghis's avatar
    Dan Braghis committed
        def valid(self, user, token):
    
    Dan Braghis's avatar
    Dan Braghis committed
            """
    
    Dan Braghis's avatar
    Dan Braghis committed
            Verify that the activation token is valid and within the
            permitted activation time window.
    
    Dan Braghis's avatar
    Dan Braghis committed
            """
    
            token_generator = PasswordResetTokenGenerator()
    
    Dan Braghis's avatar
    Dan Braghis committed
            return user is not None and token_generator.check_token(user, token)
    
    Dan Braghis's avatar
    Dan Braghis committed
        def get_user(self, uidb64):
    
    Dan Braghis's avatar
    Dan Braghis committed
            """
            Given the verified uid, look up and return the
            corresponding user account if it exists, or ``None`` if it
            doesn't.
            """
            try:
                user = User.objects.get(**{
    
    Dan Braghis's avatar
    Dan Braghis committed
                    'pk': force_text(urlsafe_base64_decode(uidb64)),
    
    Dan Braghis's avatar
    Dan Braghis committed
                    'is_active': False
                })
                return user
            except (TypeError, ValueError, OverflowError, User.DoesNotExist):
                return None
    
    
    
    def create_password(request):
    
        """
        A custom view for the admin password change form used for account activation.
        """
    
    
        if request.method == 'POST':
            form = AdminPasswordChangeForm(request.user, request.POST)
            if form.is_valid():
                user = form.save()
                update_session_auth_hash(request, user)  # Important!
                messages.success(request, 'Your password was successfully updated!')
                return redirect('users:account')
            else:
                messages.error(request, 'Please correct the errors below.')
        else:
            form = AdminPasswordChangeForm(request.user)
        return render(request, 'users/change_password.html', {
            'form': form
        })