Projet

Général

Profil

0001-data-add-site-settings-model-to-configure-welcome-ur.patch

Valentin Deniaud, 25 août 2021 17:57

Télécharger (13,7 ko)

Voir les différences:

Subject: [PATCH] data: add site settings model to configure welcome urls
 (#15846)

 combo/data/migrations/0048_sitesettings.py    | 50 +++++++++++++++++++
 combo/data/models.py                          | 13 +++++
 combo/manager/forms.py                        |  8 ++-
 .../manager/templates/combo/manager_home.html |  1 +
 .../templates/combo/site_settings.html        | 18 +++++++
 combo/manager/urls.py                         |  1 +
 combo/manager/views.py                        | 15 +++++-
 combo/public/views.py                         | 10 ++--
 combo/settings.py                             |  6 ---
 tests/test_manager.py                         | 15 ++++++
 tests/test_public.py                          | 45 ++++++++++-------
 11 files changed, 151 insertions(+), 31 deletions(-)
 create mode 100644 combo/data/migrations/0048_sitesettings.py
 create mode 100644 combo/manager/templates/combo/site_settings.html
combo/data/migrations/0048_sitesettings.py
1
# Generated by Django 2.2.19 on 2021-08-25 14:12
2

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

  
6

  
7
def create_site_settings(apps, schema_editor):
8
    SiteSettings = apps.get_model('data', 'SiteSettings')
9
    site_settings = SiteSettings.objects.create()
10
    if hasattr(settings, 'COMBO_INITIAL_LOGIN_PAGE_PATH'):
11
        site_settings.initial_login_page_path = settings.COMBO_INITIAL_LOGIN_PAGE_PATH
12
    if hasattr(settings, 'COMBO_WELCOME_PAGE_PATH'):
13
        site_settings.welcome_page_path = settings.COMBO_WELCOME_PAGE_PATH
14
    site_settings.save()
15

  
16

  
17
class Migration(migrations.Migration):
18

  
19
    dependencies = [
20
        ('data', '0047_auto_20210723_1318'),
21
    ]
22

  
23
    operations = [
24
        migrations.CreateModel(
25
            name='SiteSettings',
26
            fields=[
27
                (
28
                    'id',
29
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
30
                ),
31
                (
32
                    'initial_login_page_path',
33
                    models.CharField(
34
                        max_length=100,
35
                        verbose_name='Initial login page path',
36
                        help_text='Page to redirect to the first time user logs in.',
37
                    ),
38
                ),
39
                (
40
                    'welcome_page_path',
41
                    models.CharField(
42
                        max_length=100,
43
                        verbose_name='Welcome page path',
44
                        help_text='Page to redirect to on the first visit, to suggest user to log in.',
45
                    ),
46
                ),
47
            ],
48
        ),
49
        migrations.RunPython(create_site_settings),
50
    ]
combo/data/models.py
2134 2134
        page.save(update_fields=['last_update_timestamp'])
2135 2135
        return
2136 2136
    page.build_cell_cache()
2137

  
2138

  
2139
class SiteSettings(models.Model):
2140
    initial_login_page_path = models.CharField(
2141
        _('Initial login page path'),
2142
        help_text=_('Page to redirect to the first time user logs in.'),
2143
        max_length=100,
2144
    )
2145
    welcome_page_path = models.CharField(
2146
        _('Welcome page path'),
2147
        help_text=_('Page to redirect to on the first visit, to suggest user to log in.'),
2148
        max_length=100,
2149
    )
combo/manager/forms.py
24 24
from django.template.loader import TemplateDoesNotExist, get_template
25 25
from django.utils.translation import ugettext_lazy as _
26 26

  
27
from combo.data.models import Page, ParentContentCell, compile_sub_slug
27
from combo.data.models import Page, ParentContentCell, SiteSettings, compile_sub_slug
28 28

  
29 29
from .fields import ImageIncludingSvgField
30 30

  
......
279 279
    assets = forms.BooleanField(label=_('Assets'), required=False, initial=True)
280 280
    asset_files = forms.BooleanField(label=_('Assets Files'), required=False, initial=False)
281 281
    payment = forms.BooleanField(label=_('Online Payment'), required=False, initial=True)
282

  
283

  
284
class SiteSettingsForm(forms.ModelForm):
285
    class Meta:
286
        model = SiteSettings
287
        fields = '__all__'
combo/manager/templates/combo/manager_home.html
10 10
 <li><a href="{% url 'combo-manager-site-export' %}" rel="popup" data-autoclose-dialog="true">{% trans 'Export Site' %}</a></li>
11 11
 <li><a href="{% url 'combo-manager-site-import' %}">{% trans 'Import Site' %}</a></li>
12 12
 <li><a href="{% url 'combo-manager-invalid-cell-report' %}">{% trans 'Anomaly report' %}</a></li>
13
<li><a href="{% url 'combo-manager-site-settings' %}" rel="popup" data-autoclose-dialog="true">{% trans 'Site Settings' %}</a></li>
13 14
 {% for extra_action in extra_actions %}
14 15
 <li><a href="{{ extra_action.href }}">{{ extra_action.text }}</a></li>
15 16
 {% endfor %}
combo/manager/templates/combo/site_settings.html
1
{% extends "combo/manager_base.html" %}
2
{% load i18n %}
3

  
4
{% block appbar %}
5
<h2>{% trans "Site Settings" %}</h2>
6
{% endblock %}
7

  
8
{% block content %}
9

  
10
<form method="post">
11
  {% csrf_token %}
12
  {{ form.as_p }}
13
  <div class="buttons">
14
    <button class="submit-button">{% trans "Save" %}</button>
15
    <a class="cancel" href="{% url 'combo-manager-homepage' %}">{% trans 'Cancel' %}</a>
16
  </div>
17
</form>
18
{% endblock %}
combo/manager/urls.py
29 29
    url(r'^menu.json$', views.menu_json),
30 30
    url(r'^site-export$', views.site_export, name='combo-manager-site-export'),
31 31
    url(r'^site-import$', views.site_import, name='combo-manager-site-import'),
32
    url(r'^site-settings$', views.site_settings, name='combo-manager-site-settings'),
32 33
    url(r'^cells/invalid-report/$', views.invalid_cell_report, name='combo-manager-invalid-cell-report'),
33 34
    url(r'^pages/add/$', views.page_add, name='combo-manager-page-add'),
34 35
    url(r'^pages/(?P<pk>\d+)/$', views.page_view, name='combo-manager-page-view'),
combo/manager/views.py
45 45

  
46 46
from combo import plugins
47 47
from combo.data.library import get_cell_class
48
from combo.data.models import CellBase, LinkListCell, Page, PageSnapshot, ParentContentCell
48
from combo.data.models import CellBase, LinkListCell, Page, PageSnapshot, ParentContentCell, SiteSettings
49 49
from combo.data.utils import ImportSiteError, export_site, export_site_tar, import_site, import_site_tar
50 50

  
51 51
from .forms import (
......
63 63
    PageVisibilityForm,
64 64
    SiteExportForm,
65 65
    SiteImportForm,
66
    SiteSettingsForm,
66 67
)
67 68

  
68 69

  
......
919 920
        PageSnapshot.take(cell.page, request=request, comment=_('reordered cells'))
920 921

  
921 922
    return HttpResponse(status=204)
923

  
924

  
925
class SiteSettingsView(UpdateView):
926
    form_class = SiteSettingsForm
927
    template_name = 'combo/site_settings.html'
928
    success_url = reverse_lazy('combo-manager-homepage')
929

  
930
    def get_object(self):
931
        return SiteSettings.objects.get()
932

  
933

  
934
site_settings = SiteSettingsView.as_view()
combo/public/views.py
62 62
    ParentContentCell,
63 63
    PostException,
64 64
    Redirect,
65
    SiteSettings,
65 66
    TextCell,
66 67
    extract_context_from_sub_slug,
67 68
)
......
463 464
    if not parts:
464 465
        parts = ['index']
465 466

  
467
    site_settings = SiteSettings.objects.get()
466 468
    if (
467 469
        parts == ['index']
468
        and settings.COMBO_INITIAL_LOGIN_PAGE_PATH
470
        and site_settings.initial_login_page_path
469 471
        and (request.user and not request.user.is_anonymous)
470 472
    ):
471 473
        profile, created = Profile.objects.get_or_create(user=request.user)
......
473 475
            # first connection of user, record that and redirect to welcome URL
474 476
            profile.initial_login_view_timestamp = timezone.now()
475 477
            profile.save()
476
            return HttpResponseRedirect(settings.COMBO_INITIAL_LOGIN_PAGE_PATH)
478
            return HttpResponseRedirect(site_settings.initial_login_page_path)
477 479

  
478 480
    if (
479 481
        parts == ['index']
480
        and settings.COMBO_WELCOME_PAGE_PATH
482
        and site_settings.welcome_page_path
481 483
        and (not request.user or request.user.is_anonymous)
482 484
    ):
483 485
        if not request.session.setdefault('visited', False):
484 486
            # first visit, the user is not logged in.
485 487
            request.session['visited'] = True
486
            return HttpResponseRedirect(settings.COMBO_WELCOME_PAGE_PATH)
488
            return HttpResponseRedirect(site_settings.welcome_page_path)
487 489

  
488 490
    pages = {}
489 491
    for page in Page.objects.filter(slug__in=parts):
combo/settings.py
267 267

  
268 268
JSON_CELL_TYPES = {}
269 269

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

  
273
# page to redirect on the first visit, to suggest user to log in.
274
COMBO_WELCOME_PAGE_PATH = None
275

  
276 270
# dashboard support
277 271
COMBO_DASHBOARD_ENABLED = False
278 272

  
tests/test_manager.py
40 40
    Page,
41 41
    PageSnapshot,
42 42
    ParentContentCell,
43
    SiteSettings,
43 44
    TextCell,
44 45
    ValidityInfo,
45 46
)
......
2482 2483
    for i, item in enumerate(items):
2483 2484
        item.refresh_from_db()
2484 2485
        assert item.order == new_order[i]
2486

  
2487

  
2488
def test_site_settings(app, admin_user):
2489
    app = login(app)
2490

  
2491
    resp = app.get('/manage/')
2492
    resp = resp.click('Site Settings')
2493
    resp.form['welcome_page_path'] = '/welcome/'
2494
    resp.form['initial_login_page_path'] = '/initial-login/'
2495
    resp.form.submit()
2496

  
2497
    site_settings = SiteSettings.objects.get()
2498
    assert site_settings.welcome_page_path == '/welcome/'
2499
    assert site_settings.initial_login_page_path == '/initial-login/'
tests/test_public.py
34 34
    Page,
35 35
    ParentContentCell,
36 36
    Redirect,
37
    SiteSettings,
37 38
    TextCell,
38 39
)
39 40
from combo.wsgi import application
......
659 660
    page = Page(title='Initial Login', slug='initial-login', template_name='standard', public=False)
660 661
    page.save()
661 662

  
662
    with override_settings(COMBO_INITIAL_LOGIN_PAGE_PATH='/initial-login/'):
663
        resp = app.get('/', status=200)
663
    site_settings = SiteSettings.objects.get()
664
    site_settings.initial_login_page_path = '/initial-login/'
665
    site_settings.save()
664 666

  
665
        # first visit
666
        app = login(app)
667
        resp = app.get('/', status=302)
668
        assert urlparse.urlparse(resp.location).path == '/initial-login/'
667
    resp = app.get('/', status=200)
668

  
669
    # first visit
670
    app = login(app)
671
    resp = app.get('/', status=302)
672
    assert urlparse.urlparse(resp.location).path == '/initial-login/'
669 673

  
670
        # visit again
671
        resp = app.get('/', status=200)
674
    # visit again
675
    resp = app.get('/', status=200)
672 676

  
673 677

  
674 678
def test_welcome_page(app, admin_user):
......
678 682
    page = Page(title='Welcome', slug='welcome', template_name='standard')
679 683
    page.save()
680 684

  
681
    with override_settings(COMBO_WELCOME_PAGE_PATH='/welcome/'):
682
        app.cookiejar.clear()
683
        resp = app.get('/', status=302)
684
        assert urlparse.urlparse(resp.location).path == '/welcome/'
685
    site_settings = SiteSettings.objects.get()
686
    site_settings.welcome_page_path = '/welcome/'
687
    site_settings.save()
685 688

  
686
        resp = app.get('/', status=200)
689
    app.cookiejar.clear()
690
    resp = app.get('/', status=302)
691
    assert urlparse.urlparse(resp.location).path == '/welcome/'
687 692

  
688
        app.cookiejar.clear()
689
        resp = app.get('/', status=302)
690
        assert urlparse.urlparse(resp.location).path == '/welcome/'
693
    resp = app.get('/', status=200)
694

  
695
    app.cookiejar.clear()
696
    resp = app.get('/', status=302)
697
    assert urlparse.urlparse(resp.location).path == '/welcome/'
691 698

  
692
        app.cookiejar.clear()
693
        app = login(app)
694
        resp = app.get('/', status=200)
699
    app.cookiejar.clear()
700
    app = login(app)
701
    resp = app.get('/', status=200)
695 702

  
696 703

  
697 704
def test_post_cell(app):
698
-