class CustomLoginRequiredMixin(LoginRequiredMixin): permission_denied_message = get_conf_message('WARNING', 'LOGIN_REQUIRED') def dispatch(self, request, *args, **kwargs): if not request.user.is_authenticated: messages.add_message(request, messages.WARNING, self.permission_denied_message) return self.handle_no_permission() return super(CustomLoginRequiredMixin, self).dispatch(request, *args, **kwargs)
class ObjectAccessPermissionMixin(AccessMixin): permission_denied_message = get_conf_message('WARNING', 'PERMISSION_DENIED') def dispatch(self, request, *args, **kwargs): instance = self.get_object() instance_owner = instance.user if request.user != instance_owner: messages.add_message(request, messages.WARNING, self.permission_denied_message) return self.handle_no_permission() return super(ObjectAccessPermissionMixin, self).dispatch(request, *args, **kwargs)
class EditProfile(ObjectAccessPermissionMixin, SuccessMessageMixin, UpdateView): model = models.Profile form_class = forms.ProfileForm template_name = 'accounts/edit_profile.html' success_message = get_conf_message('SUCCESS', 'PROFILE_UPDATE') def handle_no_permission(self): return HttpResponseRedirect( reverse('accounts:profile', kwargs={'slug': self.kwargs['slug']})) def get_success_url(self): return reverse('accounts:profile', kwargs={'slug': self.kwargs['slug']})
class Register(SuccessMessageMixin, CreateView): form_class = forms.RegisterForm success_url = reverse_lazy('accounts:login') success_message = get_conf_message('SUCCESS', 'REGISTER') template_name = 'accounts/register.html'
class Login(SuccessMessageMixin, views.LoginView): template_name = 'accounts/login.html' success_message = get_conf_message('SUCCESS', 'LOGIN')
def logout_view(request): logout(request) messages.add_message(request, messages.SUCCESS, get_conf_message('SUCCESS', 'LOGOUT')) return HttpResponseRedirect(reverse('home'))