Projet

Général

Profil

0001-wcs-add-new-card-cell-model-68140.patch

Lauréline Guérin, 29 août 2022 14:39

Télécharger (10,4 ko)

Voir les différences:

Subject: [PATCH 1/5] wcs: add new card cell model (#68140)

 combo/apps/wcs/forms.py                       |   2 +-
 .../apps/wcs/migrations/0051_new_card_cell.py | 108 ++++++++++++++++++
 combo/apps/wcs/models.py                      |  41 +++++++
 tests/test_manager.py                         |   8 +-
 tests/test_search.py                          |   2 +-
 5 files changed, 155 insertions(+), 6 deletions(-)
 create mode 100644 combo/apps/wcs/migrations/0051_new_card_cell.py
combo/apps/wcs/forms.py
77 77
    with_user = forms.BooleanField(
78 78
        label=_('Restrict to cards accessible to the user'), required=False, initial=True
79 79
    )
80
    related_card_path = forms.ChoiceField(label=_('Card Identifier'), required=False)
80
    related_card_path = forms.ChoiceField(label=_('Card(s) to display'), required=False)
81 81

  
82 82
    class Meta:
83 83
        model = WcsCardInfosCell
combo/apps/wcs/migrations/0051_new_card_cell.py
1
import django.contrib.postgres.fields.jsonb
2
import django.db.models.deletion
3
from django.db import migrations, models
4

  
5
import combo.apps.wcs.models
6

  
7

  
8
class Migration(migrations.Migration):
9

  
10
    dependencies = [
11
        ('auth', '0011_update_proxy_permissions'),
12
        ('data', '0057_pagesnapshot_label'),
13
        ('wcs', '0050_card_display_mode'),
14
    ]
15

  
16
    operations = [
17
        migrations.CreateModel(
18
            name='WcsCardCell',
19
            fields=[
20
                (
21
                    'id',
22
                    models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
23
                ),
24
                ('placeholder', models.CharField(max_length=20)),
25
                ('order', models.PositiveIntegerField()),
26
                ('slug', models.SlugField(blank=True, verbose_name='Slug')),
27
                (
28
                    'extra_css_class',
29
                    models.CharField(
30
                        blank=True, max_length=100, verbose_name='Extra classes for CSS styling'
31
                    ),
32
                ),
33
                (
34
                    'template_name',
35
                    models.CharField(blank=True, max_length=50, null=True, verbose_name='Cell Template'),
36
                ),
37
                (
38
                    'condition',
39
                    models.CharField(
40
                        blank=True, max_length=1000, null=True, verbose_name='Display condition'
41
                    ),
42
                ),
43
                ('public', models.BooleanField(default=True, verbose_name='Public')),
44
                (
45
                    'restricted_to_unlogged',
46
                    models.BooleanField(default=False, verbose_name='Restrict to unlogged users'),
47
                ),
48
                ('last_update_timestamp', models.DateTimeField(auto_now=True)),
49
                ('carddef_reference', models.CharField(max_length=150, verbose_name='Card Model')),
50
                (
51
                    'related_card_path',
52
                    models.CharField(blank=True, max_length=1000, verbose_name='Card(s) to display'),
53
                ),
54
                (
55
                    'card_ids',
56
                    models.CharField(blank=True, max_length=1000, verbose_name='Other Card Identifiers'),
57
                ),
58
                (
59
                    'only_for_user',
60
                    models.BooleanField(
61
                        default=False, verbose_name='Limit to cards linked to the logged-in user'
62
                    ),
63
                ),
64
                (
65
                    'without_user',
66
                    models.BooleanField(default=False, verbose_name='Ignore the logged-in user'),
67
                ),
68
                (
69
                    'limit',
70
                    models.PositiveSmallIntegerField(
71
                        blank=True, null=True, verbose_name='Number of cards per page (default 10)'
72
                    ),
73
                ),
74
                ('custom_schema', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict)),
75
                (
76
                    'display_mode',
77
                    models.CharField(
78
                        choices=[('card', 'Card'), ('table', 'Table')],
79
                        default='card',
80
                        max_length=10,
81
                        verbose_name='Display mode',
82
                    ),
83
                ),
84
                (
85
                    'title_type',
86
                    models.CharField(
87
                        choices=[
88
                            ('auto', 'Default Title (Card Label)'),
89
                            ('manual', 'Custom Title'),
90
                            ('empty', 'No Title'),
91
                        ],
92
                        default='auto',
93
                        max_length=20,
94
                        verbose_name='Title',
95
                    ),
96
                ),
97
                ('custom_title', models.CharField(blank=True, max_length=150, verbose_name='Custom Title')),
98
                ('cached_title', models.CharField(max_length=150, verbose_name='Title')),
99
                ('cached_json', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict)),
100
                ('groups', models.ManyToManyField(blank=True, to='auth.Group', verbose_name='Groups')),
101
                ('page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='data.Page')),
102
            ],
103
            options={
104
                'verbose_name': 'Card(s)',
105
            },
106
            bases=(combo.apps.wcs.models.CardMixin, models.Model),
107
        ),
108
    ]
combo/apps/wcs/models.py
940 940
        return ''
941 941

  
942 942

  
943
@register_cell_class
944
class WcsCardCell(CardMixin, CellBase):
945
    carddef_reference = models.CharField(_('Card Model'), max_length=150)
946
    related_card_path = models.CharField(_('Card(s) to display'), max_length=1000, blank=True)
947
    card_ids = models.CharField(_('Other Card Identifiers'), max_length=1000, blank=True)
948
    only_for_user = models.BooleanField(_('Limit to cards linked to the logged-in user'), default=False)
949
    without_user = models.BooleanField(_('Ignore the logged-in user'), default=False)
950
    limit = models.PositiveSmallIntegerField(
951
        _('Number of cards per page (default 10)'), null=True, blank=True
952
    )
953
    custom_schema = JSONField(blank=True, default=dict)
954
    display_mode = models.CharField(
955
        _('Display mode'),
956
        max_length=10,
957
        default='card',
958
        choices=[
959
            ('card', _('Card')),
960
            ('table', _('Table')),
961
        ],
962
    )
963

  
964
    title_type = models.CharField(
965
        _('Title'),
966
        max_length=20,
967
        default='auto',
968
        blank=False,
969
        choices=[
970
            ('auto', _('Default Title (Card Label)')),
971
            ('manual', _('Custom Title')),
972
            ('empty', _('No Title')),
973
        ],
974
    )
975
    custom_title = models.CharField(_('Custom Title'), max_length=150, blank=True)
976

  
977
    cached_title = models.CharField(_('Title'), max_length=150)
978
    cached_json = JSONField(blank=True, default=dict)
979

  
980
    class Meta:
981
        verbose_name = _('Card(s)')
982

  
983

  
943 984
@register_cell_class
944 985
class WcsCardInfosCell(CardMixin, CellBase):
945 986
    carddef_reference = models.CharField(_('Card Model'), max_length=150)
tests/test_manager.py
1040 1040
    resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
1041 1041
    with CaptureQueriesContext(connection) as ctx:
1042 1042
        resp = resp.form.submit()
1043
        assert len(ctx.captured_queries) in [309, 310]
1043
        assert len(ctx.captured_queries) in [314, 315]
1044 1044
    assert Page.objects.count() == 4
1045 1045
    assert PageSnapshot.objects.all().count() == 4
1046 1046

  
......
1051 1051
    resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json')
1052 1052
    with CaptureQueriesContext(connection) as ctx:
1053 1053
        resp = resp.form.submit()
1054
        assert len(ctx.captured_queries) == 278
1054
        assert len(ctx.captured_queries) == 282
1055 1055
    assert set(Page.objects.get(slug='one').related_cells['cell_types']) == {'data_textcell', 'data_linkcell'}
1056 1056
    assert Page.objects.count() == 4
1057 1057
    assert LinkCell.objects.count() == 2
......
2447 2447

  
2448 2448
    with CaptureQueriesContext(connection) as ctx:
2449 2449
        resp2 = resp.click('view', index=1)
2450
        assert len(ctx.captured_queries) == 71
2450
        assert len(ctx.captured_queries) == 72
2451 2451
    assert Page.snapshots.latest('pk').related_cells == {'cell_types': ['data_textcell']}
2452 2452
    assert resp2.text.index('Hello world') < resp2.text.index('Foobar3')
2453 2453

  
......
2508 2508
    resp = resp.click('restore', index=6)
2509 2509
    with CaptureQueriesContext(connection) as ctx:
2510 2510
        resp = resp.form.submit().follow()
2511
        assert len(ctx.captured_queries) == 155
2511
        assert len(ctx.captured_queries) == 157
2512 2512

  
2513 2513
    resp2 = resp.click('See online')
2514 2514
    assert resp2.text.index('Foobar1') < resp2.text.index('Foobar2') < resp2.text.index('Foobar3')
tests/test_search.py
1432 1432
    assert IndexedCell.objects.count() == 50
1433 1433
    with CaptureQueriesContext(connection) as ctx:
1434 1434
        index_site()
1435
        assert len(ctx.captured_queries) in (175, 225)  # (3.2, 2.2)
1435
        assert len(ctx.captured_queries) in (176, 226)  # (3.2, 2.2)
1436 1436

  
1437 1437
    SearchCell.objects.create(
1438 1438
        page=page, placeholder='content', order=0, _search_services={'data': ['search1']}
1439
-