Skip to content

Commit

Permalink
Merge 673475a into 4041575
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Mijnhout committed Jan 11, 2019
2 parents 4041575 + 673475a commit 701893e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Always reference the ticket number at the end of the issue description.

## Pending

### Fixed

- Do not logout logged-in users which visit the login view, but redirect them to home

## 1.3.5 (2018-12-21)

Expand Down
13 changes: 12 additions & 1 deletion arctic/generics.py
Expand Up @@ -910,7 +910,12 @@ def get_context_data(self, **kwargs):
return context

def get(self, request, *args, **kwargs):
logout(request)
# If the logout url is the login url, log the user out of the system
if settings.LOGOUT_URL == settings.LOGIN_URL:
logout(request)
# Else redirect a logged in user to the homepage
elif request.user.is_authenticated:
return redirect("/")
return super(LoginView, self).get(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
Expand All @@ -932,3 +937,9 @@ def post(self, request, *args, **kwargs):
return render(
request, self.template_name, self.get_context_data(**kwargs)
)


class LogoutView(View):
def dispatch(self, request, *args, **kwargs):
logout(request)
return redirect(settings.LOGIN_URL)
3 changes: 2 additions & 1 deletion arctic/project_template/config/settings.py
Expand Up @@ -112,7 +112,8 @@

STATIC_URL = "/static/"

LOGIN_URL = LOGOUT_URL = "login"
LOGIN_URL = "login"
LOGOUT_URL = "logout"

# Arctic configuration
ARCTIC_SITE_NAME = "{{ project_name }}'s Arctic website"
Expand Down
4 changes: 4 additions & 0 deletions docs/reference.md
Expand Up @@ -122,6 +122,10 @@ Being a pure Django settings, LOGIN_URL and LOGOUT_URL used in Arctic to display
login and logout links. Both items supposed to be names of URLs. Defaults are 'login'
and 'logout'. Could be set to `None` if you don't want to use authentication in your app.

If the LOGIN_URL and LOGOUT_URL are the same, the LoginView will automatically logout
the user when he visits the login page. If they are different, a logged in user will be
redirected to the homepage of the CMS and not be logged out.

# Generic Class Based Views

Arctic provides a number of class based views that add integration with the
Expand Down
3 changes: 2 additions & 1 deletion example/config/settings.py
Expand Up @@ -120,7 +120,8 @@
MEDIA_ROOT = location("media")
MEDIA_URL = "/media/"

LOGIN_URL = LOGOUT_URL = "login"
LOGIN_URL = "login"
LOGOUT_URL = "logout"


try:
Expand Down
3 changes: 2 additions & 1 deletion example/config/urls.py
Expand Up @@ -5,7 +5,7 @@
from django.conf.urls import include, url
from django.conf.urls.static import static

from arctic.generics import LoginView
from arctic.generics import LoginView, LogoutView
from arctic.views import handler400, handler403, handler404, handler500 # noqa

from countries.views import CountryAPIView, CountryListView
Expand All @@ -14,6 +14,7 @@
urlpatterns = [
url(r"^$", DashboardView.as_view(), name="index"),
url(r"^login/$", LoginView.as_view(), name="login"),
url(r"^logout/$", LogoutView.as_view(), name="logout"),
url(r"^articles/", include("articles.urls", "articles")),
url(r"^users/", include("arctic.users.urls", namespace="users")),
url(r"^countries/$", CountryListView.as_view(), name="countries-list"),
Expand Down

0 comments on commit 701893e

Please sign in to comment.