implement login and logout
- add LogoutView to dashboard app - define logout URL pattern - only use login view from django.contrib.auth.views instead of including all auth URLs - change base template to support login/logout - add template dashboard/user_dashboard.html
This commit is contained in:
parent
1deed46d34
commit
2bc278ae92
5 changed files with 55 additions and 9 deletions
|
@ -4,6 +4,7 @@ from django.conf.urls import patterns, url
|
|||
|
||||
from .views import (
|
||||
IndexView,
|
||||
LogoutView,
|
||||
UserDashboardView,
|
||||
)
|
||||
|
||||
|
@ -13,4 +14,6 @@ urlpatterns = patterns(
|
|||
url(r'^$', IndexView.as_view(), name='dashboard'),
|
||||
url(r'^user/(?P<slug>[\w0-9@.+-_]+)/$',
|
||||
UserDashboardView.as_view(), name='customer_dashboard'),
|
||||
url(r'^logout/',
|
||||
LogoutView.as_view(), name='logout'),
|
||||
)
|
||||
|
|
|
@ -4,11 +4,14 @@ This module defines the views for the gnuviechadmin customer dashboard.
|
|||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.http import HttpResponseForbidden
|
||||
from django.views.generic import (
|
||||
DetailView,
|
||||
TemplateView,
|
||||
)
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.views.generic.base import RedirectView
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.contrib.auth import get_user_model, logout
|
||||
|
||||
from braces.views import LoginRequiredMixin
|
||||
|
||||
|
@ -21,11 +24,33 @@ class IndexView(TemplateView):
|
|||
template_name = 'dashboard/index.html'
|
||||
|
||||
|
||||
class UserDashboardView(DetailView, LoginRequiredMixin):
|
||||
class UserDashboardView(LoginRequiredMixin, DetailView):
|
||||
"""
|
||||
This is the user dashboard view.
|
||||
|
||||
"""
|
||||
model = get_user_model()
|
||||
context_object_name = 'dashboard_user'
|
||||
slug_field = 'username'
|
||||
template_name = 'dashboard/user_dashboard.html'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if (request.user.is_staff or request.user == self.get_object()):
|
||||
return super(UserDashboardView, self).dispatch(
|
||||
request, *args, **kwargs
|
||||
)
|
||||
return HttpResponseForbidden(
|
||||
_('You are not allowed to view this page.')
|
||||
)
|
||||
|
||||
class LogoutView(RedirectView):
|
||||
pattern_name = 'dashboard'
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
logout(self.request)
|
||||
return super(LogoutView, self).get(*args, **kwargs)
|
||||
|
||||
def get_redirect_url(self, *args, **kwargs):
|
||||
if 'next' in self.request.GET:
|
||||
return self.request.GET['next']
|
||||
return super(LogoutView, self).get_redirect_url(*args, **kwargs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue