Projet

Général

Profil

0001-general-add-possibility-to-redirect-to-a-first-login.patch

Frédéric Péters, 11 avril 2017 15:58

Télécharger (6,21 ko)

Voir les différences:

Subject: [PATCH] general: add possibility to redirect to a "first login" page
 (#6705)

 combo/profile/__init__.py                |  0
 combo/profile/migrations/0001_initial.py | 23 +++++++++++++++++++++++
 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 +++++++++++++++++++
 7 files changed, 81 insertions(+)
 create mode 100644 combo/profile/__init__.py
 create mode 100644 combo/profile/migrations/0001_initial.py
 create mode 100644 combo/profile/migrations/__init__.py
 create mode 100644 combo/profile/models.py
combo/profile/migrations/0001_initial.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.db import migrations, models
5
from django.conf import settings
6

  
7

  
8
class Migration(migrations.Migration):
9

  
10
    dependencies = [
11
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12
    ]
13

  
14
    operations = [
15
        migrations.CreateModel(
16
            name='Profile',
17
            fields=[
18
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
19
                ('welcome_message_view_timestamp', models.DateTimeField(null=True)),
20
                ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
21
            ],
22
        ),
23
    ]
combo/profile/models.py
1
# combo - content management system
2
# Copyright (C) 2014-2017  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf import settings
18
from django.db import models
19

  
20

  
21
class Profile(models.Model):
22
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
23
    welcome_message_view_timestamp = models.DateTimeField(null=True)
combo/public/views.py
29 29
        HttpResponsePermanentRedirect)
30 30
from django.shortcuts import render, resolve_url
31 31
from django.template import RequestContext, loader
32
from django.utils import timezone
32 33

  
33 34
if django.VERSION >= (1, 8):
34 35
    from django.utils import lorem_ipsum
......
46 47
    get_idps = lambda: []
47 48

  
48 49
from combo.data.models import CellBase, Page, ParentContentCell, TextCell
50
from combo.profile.models import Profile
49 51
from combo.apps.search.models import SearchCell
50 52
from combo import utils
51 53

  
......
266 268
        return HttpResponsePermanentRedirect('/')
267 269
    if not parts:
268 270
        parts = ['index']
271

  
272
    if parts == ['index'] and settings.COMBO_INITIAL_LOGIN_PAGE_PATH and (
273
            request.user and not request.user.is_anonymous()):
274
        profile, created = Profile.objects.get_or_create(user=request.user)
275
        if not profile.welcome_message_view_timestamp:
276
            # first connection of user, record that and redirect to welcome URL
277
            profile.welcome_message_view_timestamp = timezone.now()
278
            profile.save()
279
            return HttpResponseRedirect(settings.COMBO_INITIAL_LOGIN_PAGE_PATH)
280

  
269 281
    try:
270 282
        page = Page.objects.get(slug=parts[-1])
271 283
    except Page.DoesNotExist:
combo/settings.py
60 60
    'gadjo',
61 61
    'cmsplugin_blurp',
62 62
    'combo.data',
63
    'combo.profile',
63 64
    'combo.manager',
64 65
    'combo.public',
65 66
    'combo.apps.wcs',
......
266 267

  
267 268
JSON_CELL_TYPES = {}
268 269

  
270
# page to redirect the first time the user logs in.
271
COMBO_INITIAL_LOGIN_PAGE_PATH = None
272

  
269 273

  
270 274
local_settings_file = os.environ.get('COMBO_SETTINGS_FILE',
271 275
        os.path.join(os.path.dirname(__file__), 'local_settings.py'))
tests/test_public.py
241 241
    resp = app.get(reverse('combo-public-ajax-page-cell',
242 242
        kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}),
243 243
        status=403)
244

  
245

  
246
def test_welcome_page(app, admin_user):
247
    Page.objects.all().delete()
248
    page = Page(title='Home', slug='index', template_name='standard')
249
    page.save()
250
    page = Page(title='Initial Login', slug='initial-login', template_name='standard', public=False)
251
    page.save()
252

  
253
    with override_settings(COMBO_INITIAL_LOGIN_PAGE_PATH='/initial-login/'):
254
        resp = app.get('/', status=200)
255

  
256
        # first visit
257
        app = login(app)
258
        resp = app.get('/', status=302)
259
        assert resp.location == 'http://testserver/initial-login/'
260

  
261
        # visit again
262
        resp = app.get('/', status=200)
244
-