Projet

Général

Profil

0001-cells-extra_css_class-for-links-in-link-list-cell-61.patch

Lauréline Guérin, 31 janvier 2022 11:15

Télécharger (10,5 ko)

Voir les différences:

Subject: [PATCH] cells: extra_css_class for links in link list cell (#61197)

 combo/apps/wcs/forms.py                       |  6 ++++++
 combo/apps/wcs/models.py                      |  5 +++++
 combo/data/forms.py                           |  6 ++++++
 combo/data/models.py                          |  5 +++++
 .../combo/manager/link-list-cell-form.html    |  3 +++
 combo/manager/static/css/combo.manager.css    |  3 ++-
 combo/manager/views.py                        |  4 ++--
 .../templates/combo/link-list-cell.html       |  2 +-
 tests/test_cells.py                           |  6 +++++-
 tests/test_manager.py                         |  7 +++++++
 tests/test_wcs.py                             | 19 ++++++++++++++++---
 11 files changed, 58 insertions(+), 8 deletions(-)
combo/apps/wcs/forms.py
43 43
        self.fields['formdef_reference'].widget = forms.Select(choices=formdef_references)
44 44

  
45 45

  
46
class WcsFormForLinkListCellForm(WcsFormCellForm):
47
    class Meta:
48
        model = WcsFormCell
49
        fields = ('formdef_reference', 'extra_css_class')
50

  
51

  
46 52
class WcsCardsCellForm(forms.ModelForm):
47 53
    with_user = forms.BooleanField(label=_('Restrict to cards accessible to the user'), required=False)
48 54

  
combo/apps/wcs/models.py
88 88

  
89 89
        return WcsFormCellForm
90 90

  
91
    def get_form_class_for_link_list_cell(self):
92
        from .forms import WcsFormForLinkListCellForm
93

  
94
        return WcsFormForLinkListCellForm
95

  
91 96
    def save(self, *args, **kwargs):
92 97
        if 'update_fields' in kwargs:
93 98
            # don't populate the cache
combo/data/forms.py
49 49
        self.fields['link_page'].widget = forms.Select(choices=[(None, '-----')] + get_page_choices())
50 50

  
51 51

  
52
class LinkCellForLinkListCellForm(LinkCellForm):
53
    class Meta:
54
        model = LinkCell
55
        fields = ('title', 'url', 'link_page', 'anchor', 'extra_css_class')
56

  
57

  
52 58
class LinkListCellForm(forms.ModelForm):
53 59
    class Meta:
54 60
        model = LinkListCell
combo/data/models.py
1514 1514

  
1515 1515
        return LinkCellForm
1516 1516

  
1517
    def get_form_class_for_link_list_cell(self):
1518
        from .forms import LinkCellForLinkListCellForm
1519

  
1520
        return LinkCellForLinkListCellForm
1521

  
1517 1522
    def render_for_search(self):
1518 1523
        return ''
1519 1524

  
combo/data/templates/combo/manager/link-list-cell-form.html
14 14
   <span title="{{ link }}">
15 15
     {{ link|truncatechars:100 }}
16 16
     {% with link.get_invalid_reason as invalid_reason %}
17
     {% if link.extra_css_class %}
18
     <span class="extra-css-class">[{{ link.extra_css_class }}]</span>
19
     {% endif %}
17 20
     {% if invalid_reason %}
18 21
     <span class="invalid">{{ invalid_reason }} -
19 22
       {% if cell.get_validity_info.invalid_datetime|datetime_in_past %}
combo/manager/static/css/combo.manager.css
105 105
	color: #3c3c33;
106 106
}
107 107

  
108
div.cell h3 span.extra-css-class {
108
div.cell h3 span.extra-css-class,
109
ul.list-of-links span.extra-css-class {
109 110
	font-size: 70%;
110 111
	opacity: 0.7;
111 112
}
combo/manager/views.py
874 874
        return super().dispatch(request, *args, **kwargs)
875 875

  
876 876
    def get_form_class(self):
877
        return self.model().get_default_form_class()
877
        return self.model().get_form_class_for_link_list_cell()
878 878

  
879 879
    def get_form_kwargs(self):
880 880
        kwargs = super().get_form_kwargs()
......
923 923
        return self.object
924 924

  
925 925
    def get_form_class(self):
926
        return self.model().get_default_form_class()
926
        return self.model().get_form_class_for_link_list_cell()
927 927

  
928 928
    def form_valid(self, form):
929 929
        if self.request.is_ajax():
combo/public/templates/combo/link-list-cell.html
9 9
  {% block cell-top-content %}{% endblock cell-top-content %}
10 10
  <ul>
11 11
  {% for link in links %}
12
  <li class="{{ link.css_classes|default:""|join:" " }}"><a href="{{ link.url }}">{{ link.title }}</a></li>
12
  <li class="{{ link.css_classes|default:""|join:" " }}{% if link.cell.extra_css_class %} {{ link.cell.extra_css_class }}{% endif %}"><a href="{{ link.url }}">{{ link.title }}</a></li>
13 13
  {% endfor %}
14 14
  {% if more_links %}
15 15
  <li class="add-more-items">
tests/test_cells.py
258 258
        title='Example Site',
259 259
        url='http://example.net/',
260 260
        order=0,
261
        extra_css_class='foobar',
261 262
    )
262 263

  
263 264
    ctx = {'page_cells': [item]}
264
    assert '<ul><li class=""><a href="http://example.net/">Example Site</a></li></ul>' in cell.render(ctx)
265
    assert '<ul><li class=" foobar"><a href="http://example.net/">Example Site</a></li></ul>' in cell.render(
266
        ctx
267
    )
265 268

  
266 269
    item.title = ''
270
    item.extra_css_class = ''
267 271
    assert '<ul><li class=""><a href="http://example.net/">http://example.net/</a></li></ul>' in cell.render(
268 272
        ctx
269 273
    )
tests/test_manager.py
2467 2467
    assert JsonCell.objects.first().template_string == '{{ ok }}'
2468 2468

  
2469 2469

  
2470
def test_link_cell_setup():
2471
    form = LinkCellForm()
2472
    assert 'extra_css_class' not in form.fields
2473

  
2474

  
2470 2475
def test_link_cell_validation():
2471 2476
    form = LinkCellForm(data={'url': 'http://example.com'})
2472 2477
    assert form.is_valid() is True
......
2490 2495
    resp = resp.click(href='.*/add-link/link$')
2491 2496
    resp.forms[0]['title'] = 'Hello world'
2492 2497
    resp.forms[0]['url'] = 'http://example.com'
2498
    resp.forms[0]['extra_css_class'] = 'foobar'
2493 2499
    resp = resp.forms[0].submit()
2494 2500
    assert resp.status_int == 302
2495 2501
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
......
2499 2505
    assert item.url == 'http://example.com'
2500 2506
    assert item.page == page
2501 2507
    assert item.placeholder == cell.link_placeholder
2508
    assert item.extra_css_class == 'foobar'
2502 2509
    assert PageSnapshot.objects.count() == 1
2503 2510

  
2504 2511
    resp = resp.follow()
tests/test_wcs.py
468 468
        ('other:form-title', 'test2 : form title'),
469 469
        ('other:third-form-title', 'test2 : Third form title'),
470 470
    ]
471
    assert 'extra_css_class' not in form.fields
471 472

  
472 473

  
473 474
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
......
3933 3934

  
3934 3935
    resp = resp.click(href='.*/add-link/form-link$')
3935 3936
    resp.forms[0]['formdef_reference'] = 'default:form-title'
3937
    resp.forms[0]['extra_css_class'] = 'foobar'
3936 3938
    resp = resp.forms[0].submit()
3937 3939
    assert resp.status_int == 302
3938 3940
    assert resp.location.endswith('/manage/pages/%s/#cell-%s' % (page.pk, cell.get_reference()))
......
3941 3943
    assert item.formdef_reference == 'default:form-title'
3942 3944
    assert item.page == page
3943 3945
    assert item.placeholder == cell.link_placeholder
3946
    assert item.extra_css_class == 'foobar'
3944 3947

  
3945 3948
    resp = resp.follow()
3946 3949
    resp = resp.click(href='.*/link/%s/$' % item.get_reference())
......
3993 3996
    page.save()
3994 3997

  
3995 3998
    cell = LinkListCell.objects.create(order=0, placeholder='content', page=page)
3996
    WcsFormCell.objects.create(
3999
    link = WcsFormCell.objects.create(
3997 4000
        page=page,
3998 4001
        placeholder=cell.link_placeholder,
3999 4002
        cached_title='A title',
4000 4003
        cached_url='http://example.com/',
4001
        cached_json={'keywords': ['bar']},
4002 4004
        order=0,
4005
        extra_css_class='foobar',
4003 4006
    )
4004 4007

  
4005 4008
    resp = app.get('/test_list_of_links_with_form_render/')
4006 4009
    assert PyQuery(resp.text).find('.links-list a').text() == 'A title'
4007
    assert PyQuery(resp.text).find('.links-list li').attr('class') == 'keyword-bar'
4010
    assert PyQuery(resp.text).find('.links-list li').attr('class') == ' foobar'
4008 4011
    assert (
4009 4012
        PyQuery(resp.text).find('.links-list a').attr('href')
4010 4013
        == 'http://example.com/tryauth?cancelurl=http%3A//testserver/test_list_of_links_with_form_render/'
4011 4014
    )
4012 4015

  
4016
    link.cached_json = {'keywords': ['bar']}
4017
    link.save()
4018
    resp = app.get('/test_list_of_links_with_form_render/')
4019
    assert PyQuery(resp.text).find('.links-list li').attr('class') == 'keyword-bar foobar'
4020

  
4021
    link.extra_css_class = ''
4022
    link.save()
4023
    resp = app.get('/test_list_of_links_with_form_render/')
4024
    assert PyQuery(resp.text).find('.links-list li').attr('class') == 'keyword-bar'
4025

  
4013 4026

  
4014 4027
def test_view_page_with_wcs_cells_num_queries(app, admin_user):
4015 4028
    page = Page.objects.create(title='bar', slug='index', order=1)
4016
-