Projet

Général

Profil

0001-wcs-option-to-get-cards-only-for-current-user-48190.patch

Lauréline Guérin, 09 novembre 2020 09:07

Télécharger (5,34 ko)

Voir les différences:

Subject: [PATCH] wcs: option to get cards only for current user (#48190)

 combo/apps/wcs/forms.py                      |  2 +-
 combo/apps/wcs/migrations/0022_cards_user.py | 19 +++++++++++
 combo/apps/wcs/models.py                     | 15 ++++++--
 tests/test_wcs.py                            | 36 ++++++++++++++++++++
 4 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 combo/apps/wcs/migrations/0022_cards_user.py
combo/apps/wcs/forms.py
40 40
class WcsCardsCellForm(forms.ModelForm):
41 41
    class Meta:
42 42
        model = WcsCardsCell
43
        fields = ('carddef_reference',)
43
        fields = ('carddef_reference', 'only_for_user')
44 44

  
45 45
    def __init__(self, *args, **kwargs):
46 46
        super().__init__(*args, **kwargs)
combo/apps/wcs/migrations/0022_cards_user.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.db import migrations, models
5

  
6

  
7
class Migration(migrations.Migration):
8

  
9
    dependencies = [
10
        ('wcs', '0021_card'),
11
    ]
12

  
13
    operations = [
14
        migrations.AddField(
15
            model_name='wcscardscell',
16
            name='only_for_user',
17
            field=models.BooleanField(default=False, verbose_name='Limit to cards linked to the logged-in user'),
18
        ),
19
    ]
combo/apps/wcs/models.py
727 727
class WcsCardsCell(CardMixin, WcsBlurpMixin, CellBase):
728 728
    carddef_reference = models.CharField(_('Card Model'), max_length=150)
729 729
    cached_title = models.CharField(_('Title'), max_length=150)
730
    only_for_user = models.BooleanField(_('Limit to cards linked to the logged-in user'), default=False)
730 731

  
731 732
    template_name = 'combo/wcs/cards.html'
732 733
    variable_name = 'cards'
......
789 790

  
790 791
        populate_cache()
791 792

  
793
    def is_visible(self, **kwargs):
794
        user = kwargs.get('user')
795
        if self.only_for_user and (not user or user.is_anonymous):
796
            return False
797
        return super().is_visible(**kwargs)
798

  
792 799
    def get_api_url(self, context):
793 800
        parts = self.carddef_reference.split(':')
801
        url = 'api/cards/%s/list' % parts[1]
794 802
        if len(parts) > 2:
795
            return 'api/cards/%s/list/%s' % (parts[1], parts[2])
796
        return 'api/cards/%s/list' % parts[1]
803
            url = '%s/%s' % (url, parts[2])
804
        user = self.get_concerned_user(context)
805
        if self.only_for_user and user and user.get_name_id():
806
            url = '%s?filter-user-uuid=%s' % (url, user.get_name_id())
807
        return url
797 808

  
798 809
    def get_cell_extra_context(self, context):
799 810
        extra_context = super().get_cell_extra_context(context)
tests/test_wcs.py
167 167
class MockUser(object):
168 168
    email = 'foo@example.net'
169 169
    is_authenticated = True
170
    is_anonymous = False
170 171

  
171 172
    def get_name_id(self):
172 173
        return None
......
175 176
class MockUserWithNameId(object):
176 177
    email = 'foo@example.net'
177 178
    is_authenticated = True
179
    is_anonymous = False
178 180

  
179 181
    def get_name_id(self):
180 182
        return 'xyz'
......
1202 1204
    assert '<a href="/foo/13"><span class="card-title">cc</span></a>' not in result
1203 1205

  
1204 1206

  
1207
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1208
def test_cards_cell_only_for_user(mock_send, context):
1209
    page = Page.objects.create(title='xxx', template_name='standard')
1210
    cell = WcsCardsCell(page=page, placeholder='content', order=0)
1211
    cell.carddef_reference = 'default:card_model_1'
1212
    cell.only_for_user = False
1213
    cell.save()
1214

  
1215
    assert cell.is_visible(user=None) is True
1216
    assert cell.is_visible(user=MockUserWithNameId()) is True
1217

  
1218
    cell.only_for_user = True
1219
    cell.save()
1220
    assert cell.is_visible(user=None) is False
1221
    assert cell.is_visible(user=MockUserWithNameId()) is True
1222

  
1223
    cache.clear()
1224
    context['synchronous'] = True  # to get fresh content
1225

  
1226
    mock_send.reset_mock()
1227
    cell.render(context)
1228
    assert 'filter-user-uuid' not in mock_send.call_args_list[0][0][0].url
1229

  
1230
    context['request'].user = MockUser()
1231
    mock_send.reset_mock()
1232
    cell.render(context)
1233
    assert 'filter-user-uuid' not in mock_send.call_args_list[0][0][0].url
1234

  
1235
    context['request'].user = MockUserWithNameId()
1236
    mock_send.reset_mock()
1237
    cell.render(context)
1238
    assert 'filter-user-uuid=xyz' in mock_send.call_args_list[0][0][0].url
1239

  
1240

  
1205 1241
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1206 1242
def test_card_cell_setup(mock_send):
1207 1243
    cell = WcsCardInfosCell()
1208
-