From b74f137d00a808a9a1ddf0281dd0733172fa1f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 10 Apr 2017 14:17:45 +0200 Subject: [PATCH] general: add possibility to redirect to an "initial login" page (#6705) --- combo/profile/__init__.py | 0 combo/profile/migrations/__init__.py | 0 combo/profile/models.py | 23 +++++++++++++++++++++++ combo/public/views.py | 12 ++++++++++++ combo/settings.py | 4 ++++ tests/test_public.py | 19 +++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 combo/profile/__init__.py create mode 100644 combo/profile/migrations/__init__.py create mode 100644 combo/profile/models.py diff --git a/combo/profile/__init__.py b/combo/profile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/combo/profile/migrations/__init__.py b/combo/profile/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/combo/profile/models.py b/combo/profile/models.py new file mode 100644 index 0000000..1e7824e --- /dev/null +++ b/combo/profile/models.py @@ -0,0 +1,23 @@ +# combo - content management system +# Copyright (C) 2014-2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.conf import settings +from django.db import models + + +class Profile(models.Model): + user = models.OneToOneField(settings.AUTH_USER_MODEL) + initial_login_view_timestamp = models.DateTimeField(null=True) diff --git a/combo/public/views.py b/combo/public/views.py index 7c9afa6..f5532ec 100644 --- a/combo/public/views.py +++ b/combo/public/views.py @@ -29,6 +29,7 @@ from django.http import (Http404, HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect) from django.shortcuts import render, resolve_url from django.template import RequestContext, loader +from django.utils import timezone if django.VERSION >= (1, 8): from django.utils import lorem_ipsum @@ -46,6 +47,7 @@ else: get_idps = lambda: [] from combo.data.models import CellBase, Page, ParentContentCell, TextCell +from combo.profile.models import Profile from combo.apps.search.models import SearchCell from combo import utils @@ -266,6 +268,16 @@ def page(request): return HttpResponsePermanentRedirect('/') if not parts: parts = ['index'] + + if parts == ['index'] and settings.COMBO_INITIAL_LOGIN_PAGE_PATH and ( + request.user and not request.user.is_anonymous()): + profile, created = Profile.objects.get_or_create(user=request.user) + if not profile.initial_login_view_timestamp: + # first connection of user, record that and redirect to welcome URL + profile.initial_login_view_timestamp = timezone.now() + profile.save() + return HttpResponseRedirect(settings.COMBO_INITIAL_LOGIN_PAGE_PATH) + try: page = Page.objects.get(slug=parts[-1]) except Page.DoesNotExist: diff --git a/combo/settings.py b/combo/settings.py index 7126bb9..2a8ae0b 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -60,6 +60,7 @@ INSTALLED_APPS = ( 'gadjo', 'cmsplugin_blurp', 'combo.data', + 'combo.profile', 'combo.manager', 'combo.public', 'combo.apps.wcs', @@ -266,6 +267,9 @@ LINGO_NO_ONLINE_PAYMENT_REASONS = {} JSON_CELL_TYPES = {} +# page to redirect the first time the user logs in. +COMBO_INITIAL_LOGIN_PAGE_PATH = None + local_settings_file = os.environ.get('COMBO_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py')) diff --git a/tests/test_public.py b/tests/test_public.py index d3e8532..6ca4339 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -241,3 +241,22 @@ def test_ajax_cell(app): resp = app.get(reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}), status=403) + + +def test_initial_login_page(app, admin_user): + Page.objects.all().delete() + page = Page(title='Home', slug='index', template_name='standard') + page.save() + page = Page(title='Initial Login', slug='initial-login', template_name='standard', public=False) + page.save() + + with override_settings(COMBO_INITIAL_LOGIN_PAGE_PATH='/initial-login/'): + resp = app.get('/', status=200) + + # first visit + app = login(app) + resp = app.get('/', status=302) + assert resp.location == 'http://testserver/initial-login/' + + # visit again + resp = app.get('/', status=200) -- 2.11.0