Projet

Général

Profil

0001-data-add-max_one_by_page-cell-class-attribute-60547.patch

Valentin Deniaud, 18 janvier 2022 17:09

Télécharger (4,3 ko)

Voir les différences:

Subject: [PATCH 1/5] data: add max_one_by_page cell class attribute (#60547)

 combo/data/models.py   |  2 ++
 combo/manager/views.py | 11 +++++++++--
 tests/test_manager.py  | 21 +++++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
combo/data/models.py
792 792
    visible = True
793 793
    user_dependant = False
794 794
    default_template_name = None
795
    max_one_by_page = False
795 796

  
796 797
    # get_badge(self, context); set to None so cell types can be skipped easily
797 798
    get_badge = None
......
1030 1031
                'group': cls.get_cell_type_group(),
1031 1032
                'variant': 'default',
1032 1033
                'order': 0,
1034
                'max_one_by_page': cls.max_one_by_page,
1033 1035
            }
1034 1036
        ]
1035 1037

  
combo/manager/views.py
372 372

  
373 373
    def get_context_data(self, **kwargs):
374 374
        context = super().get_context_data(**kwargs)
375
        cells = CellBase.get_cells(page=self.object, prefetch_validity_info=True)
376
        existing_cell_types = {cell.get_cell_type_str() for cell in cells}
375 377
        cell_type_groups = {}
376 378
        for cell_type in CellBase.get_all_cell_types():
377 379
            if not cell_type['group'] in cell_type_groups:
378 380
                cell_type_groups[cell_type['group']] = []
381
            if cell_type.get('max_one_by_page') and cell_type['cell_type_str'] in existing_cell_types:
382
                continue
379 383
            cell_type_groups[cell_type['group']].append(cell_type)
380 384
        for cell_group in cell_type_groups.values():
381 385
            cell_group.sort(key=lambda x: (x.get('order'), x.get('name')))
......
386 390
        context['cell_type_groups'].sort(key=lambda x: x[0])
387 391
        context['page_has_subpages'] = self.object.get_children().exists()
388 392

  
389
        cells = CellBase.get_cells(page=self.object, prefetch_validity_info=True)
390 393
        self.object.prefetched_cells = cells
391 394
        placeholders = []
392 395
        optional_placeholders = []
......
604 607
    permanent = False
605 608

  
606 609
    def get_redirect_url(self, page_pk, cell_type, variant, ph_key):
610
        page_cells = CellBase.get_cells(page_id=page_pk)
607 611
        cell_class = get_cell_class(cell_type)
612
        if cell_class.max_one_by_page and any(isinstance(x, cell_class) for x in page_cells):
613
            raise PermissionDenied()
614

  
608 615
        cell = cell_class(page_id=page_pk, placeholder=ph_key)
609 616
        cell.set_variant(variant)
610
        orders = [x.order for x in CellBase.get_cells(page_id=page_pk)]
617
        orders = [x.order for x in page_cells]
611 618
        if orders:
612 619
            cell.order = max(orders) + 1
613 620
        else:
tests/test_manager.py
1249 1249
        resp = resp.follow()
1250 1250

  
1251 1251

  
1252
@mock.patch('combo.data.models.TextCell.max_one_by_page', mock.PropertyMock(return_value=True))
1253
def test_add_cell_max_one_by_page(app, admin_user):
1254
    page = Page.objects.create(title='One', slug='one', template_name='standard')
1255
    app = login(app)
1256
    resp = app.get('/manage/pages/%s/' % page.id)
1257

  
1258
    add_text_cell_url = resp.html.find('option').get('data-add-url')
1259
    resp = app.get(add_text_cell_url)
1260

  
1261
    cells = CellBase.get_cells(page_id=page.id)
1262
    assert len(cells) == 1
1263
    assert isinstance(cells[0], TextCell)
1264
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.id, cells[0].get_reference()))
1265

  
1266
    resp = app.get('/manage/pages/%s/' % page.id)
1267
    assert not add_text_cell_url in resp.text
1268

  
1269
    # try to add cell anyway
1270
    resp = app.get(add_text_cell_url, status=403)
1271

  
1272

  
1252 1273
SEARCH_SERVICES = {
1253 1274
    'search1': {
1254 1275
        'label': 'Search 1',
1255
-