Projet

Général

Profil

0001-public-add-page-selector-in-site-settings-58475.patch

Valentin Deniaud, 10 novembre 2021 16:59

Télécharger (10,5 ko)

Voir les différences:

Subject: [PATCH] public: add page selector in site settings (#58475)

 combo/data/migrations/0049_sitesettings.py    |  8 ++--
 .../migrations/0051_auto_20211110_1521.py     | 40 +++++++++++++++++++
 combo/data/models.py                          | 26 ++++++++++--
 combo/manager/forms.py                        |  6 +++
 .../templates/combo/site_settings.html        | 12 ++++++
 combo/public/views.py                         | 14 +++++--
 tests/test_manager.py                         | 12 ++++++
 tests/test_public.py                          | 23 +++++++++++
 8 files changed, 129 insertions(+), 12 deletions(-)
 create mode 100644 combo/data/migrations/0051_auto_20211110_1521.py
combo/data/migrations/0049_sitesettings.py
21 21
                    'initial_login_page_path',
22 22
                    models.CharField(
23 23
                        max_length=100,
24
                        verbose_name='Initial login page path',
25
                        help_text='Page to redirect to the first time user logs in.',
24
                        verbose_name='',
25
                        help_text='Path or full URL.',
26 26
                        blank=True,
27 27
                    ),
28 28
                ),
......
30 30
                    'welcome_page_path',
31 31
                    models.CharField(
32 32
                        max_length=100,
33
                        verbose_name='Welcome page path',
34
                        help_text='Page to redirect to on the first visit, to suggest user to log in.',
33
                        verbose_name='',
34
                        help_text='Path or full URL.',
35 35
                        blank=True,
36 36
                    ),
37 37
                ),
combo/data/migrations/0051_auto_20211110_1521.py
1
# Generated by Django 2.2.19 on 2021-11-10 14:21
2

  
3
import django.db.models.deletion
4
from django.db import migrations, models
5

  
6

  
7
class Migration(migrations.Migration):
8

  
9
    dependencies = [
10
        ('data', '0050_populate_site_settings'),
11
    ]
12

  
13
    operations = [
14
        migrations.AddField(
15
            model_name='sitesettings',
16
            name='initial_login_page',
17
            field=models.ForeignKey(
18
                blank=True,
19
                null=True,
20
                on_delete=django.db.models.deletion.SET_NULL,
21
                related_name='+',
22
                to='data.Page',
23
                verbose_name='Initial login page',
24
                help_text='Page to redirect to the first time user logs in.',
25
            ),
26
        ),
27
        migrations.AddField(
28
            model_name='sitesettings',
29
            name='welcome_page',
30
            field=models.ForeignKey(
31
                blank=True,
32
                null=True,
33
                on_delete=django.db.models.deletion.SET_NULL,
34
                related_name='+',
35
                to='data.Page',
36
                verbose_name='Welcome page',
37
                help_text='Page to redirect to on the first visit, to suggest user to log in.',
38
            ),
39
        ),
40
    ]
combo/data/models.py
2207 2207

  
2208 2208

  
2209 2209
class SiteSettings(models.Model):
2210
    welcome_page_path = models.CharField(
2211
        _('Welcome page path'),
2210
    welcome_page = models.ForeignKey(
2211
        to=Page,
2212
        verbose_name=_('Welcome page'),
2213
        on_delete=models.SET_NULL,
2214
        null=True,
2215
        blank=True,
2216
        related_name='+',
2212 2217
        help_text=_('Page to redirect to on the first visit, to suggest user to log in.'),
2218
    )
2219
    welcome_page_path = models.CharField(
2220
        verbose_name='',
2221
        help_text=_('Path or full URL.'),
2213 2222
        max_length=100,
2214 2223
        blank=True,
2215 2224
    )
2216
    initial_login_page_path = models.CharField(
2217
        _('Initial login page path'),
2225
    initial_login_page = models.ForeignKey(
2226
        to=Page,
2227
        verbose_name=_('Initial login page'),
2218 2228
        help_text=_('Page to redirect to the first time user logs in.'),
2229
        on_delete=models.SET_NULL,
2230
        null=True,
2231
        blank=True,
2232
        related_name='+',
2233
    )
2234
    initial_login_page_path = models.CharField(
2235
        verbose_name='',
2236
        help_text=_('Path or full URL.'),
2219 2237
        max_length=100,
2220 2238
        blank=True,
2221 2239
    )
combo/manager/forms.py
323 323
    class Meta:
324 324
        model = SiteSettings
325 325
        fields = '__all__'
326

  
327
    def __init__(self, *args, **kwargs):
328
        super().__init__(*args, **kwargs)
329
        self.fields['welcome_page'].empty_label = _('Custom URL')
330
        self.fields['initial_login_page'].empty_label = _('Custom URL')
331
        self.fields['welcome_page'].queryset = self.fields['welcome_page'].queryset.filter(public=True)
combo/manager/templates/combo/site_settings.html
14 14
    <button class="submit-button">{% trans "Save" %}</button>
15 15
    <a class="cancel" href="{% url 'combo-manager-homepage' %}">{% trans 'Cancel' %}</a>
16 16
  </div>
17

  
18
  <script>
19
  $('select').change(function(){
20
    field_id = $(this).attr('id')
21
    custom_url_field_id = '#' + field_id + '_path'
22
    if ($(this).val() == '')
23
      $(custom_url_field_id).parent('p').show();
24
    else
25
      $(custom_url_field_id).parent('p').hide();
26
  });
27
  $('select').trigger('change');
28
  </script>
17 29
</form>
18 30
{% endblock %}
combo/public/views.py
463 463
    site_settings = SiteSettings.objects.get()
464 464
    if (
465 465
        parts == ['index']
466
        and site_settings.initial_login_page_path
466
        and (site_settings.initial_login_page or site_settings.initial_login_page_path)
467 467
        and (request.user and not request.user.is_anonymous)
468 468
    ):
469 469
        profile, dummy = Profile.objects.get_or_create(user=request.user)
......
471 471
            # first connection of user, record that and redirect to welcome URL
472 472
            profile.initial_login_view_timestamp = timezone.now()
473 473
            profile.save()
474
            page_path = utils.get_templated_url(site_settings.initial_login_page_path)
474
            if site_settings.initial_login_page:
475
                page_path = site_settings.initial_login_page.get_online_url()
476
            else:
477
                page_path = utils.get_templated_url(site_settings.initial_login_page_path)
475 478
            return HttpResponseRedirect(page_path)
476 479

  
477 480
    if (
478 481
        parts == ['index']
479
        and site_settings.welcome_page_path
482
        and (site_settings.welcome_page or site_settings.welcome_page_path)
480 483
        and (not request.user or request.user.is_anonymous)
481 484
    ):
482 485
        if not request.session.setdefault('visited', False):
483 486
            # first visit, the user is not logged in.
484 487
            request.session['visited'] = True
485
            page_path = utils.get_templated_url(site_settings.welcome_page_path)
488
            if site_settings.welcome_page:
489
                page_path = site_settings.welcome_page.get_online_url()
490
            else:
491
                page_path = utils.get_templated_url(site_settings.welcome_page_path)
486 492
            return HttpResponseRedirect(page_path)
487 493

  
488 494
    try:
tests/test_manager.py
2633 2633

  
2634 2634

  
2635 2635
def test_site_settings(app, admin_user):
2636
    public_page = Page.objects.create(title='Public', slug='public')
2637
    private_page = Page.objects.create(title='Private', slug='private', public=False)
2636 2638
    app = login(app)
2637 2639

  
2638 2640
    resp = app.get('/manage/')
......
2652 2654
    site_settings.refresh_from_db()
2653 2655
    assert site_settings.welcome_page_path == ''
2654 2656
    assert site_settings.initial_login_page_path == ''
2657

  
2658
    assert list(resp.context['form'].fields['welcome_page'].queryset) == [public_page]
2659
    assert list(resp.context['form'].fields['initial_login_page'].queryset) == [public_page, private_page]
2660
    resp.form['welcome_page'].select(text='Public')
2661
    resp.form['initial_login_page'].select(text='Private')
2662
    resp.form.submit()
2663

  
2664
    site_settings.refresh_from_db()
2665
    assert site_settings.welcome_page == public_page
2666
    assert site_settings.initial_login_page == private_page
tests/test_public.py
696 696
        resp = app.get('/', status=302)
697 697
        assert resp.location == 'https://authentic.example.org/bar/'
698 698

  
699
    profile.initial_login_view_timestamp = None
700
    profile.save(update_fields=['initial_login_view_timestamp'])
701

  
702
    site_settings.initial_login_page = page
703
    site_settings.save()
704

  
705
    # first visit
706
    app = login(app)
707
    resp = app.get('/', status=302)
708
    assert urllib.parse.urlparse(resp.location).path == '/initial-login/'
709

  
710
    # visit again
711
    resp = app.get('/', status=200)
712

  
699 713

  
700 714
def test_welcome_page(app, admin_user):
701 715
    Page.objects.all().delete()
......
744 758
        resp = app.get('/', status=302)
745 759
        assert resp.location == 'https://authentic.example.org/bar/'
746 760

  
761
    site_settings.welcome_page = page
762
    site_settings.save()
763

  
764
    app.cookiejar.clear()
765
    resp = app.get('/', status=302)
766
    assert urllib.parse.urlparse(resp.location).path == '/welcome/'
767

  
768
    resp = app.get('/', status=200)
769

  
747 770

  
748 771
def test_post_cell(app):
749 772
    Page.objects.all().delete()
750
-