Projet

Général

Profil

0001-wcs-explicit-id-from-URL-choice-for-card-cell-60957.patch

Lauréline Guérin, 28 janvier 2022 15:56

Télécharger (13,3 ko)

Voir les différences:

Subject: [PATCH] wcs: explicit "id from URL" choice for card cell (#60957)

 combo/apps/wcs/forms.py |  22 ++++++++-
 tests/test_wcs.py       | 101 +++++++++++++++++++++++++++++++++-------
 2 files changed, 103 insertions(+), 20 deletions(-)
combo/apps/wcs/forms.py
98 98
            del self.fields['customize_display']
99 99
            del self.fields['custom_schema']
100 100

  
101
        self.fields['related_card_path'].choices = [
102
            ('', _('Other Card Identifiers'))
101
        self.fields['related_card_path'].choices = []
102
        with_sub_slug = any(p.sub_slug for p in self.instance.page.get_parents_and_self())
103
        if with_sub_slug:
104
            self.fields['related_card_path'].choices += [
105
                ('--', _('Identifier from page URL')),
106
            ]
107
            if not self.instance.card_ids and not self.instance.related_card_path:
108
                self.initial['related_card_path'] = '--'
109
        self.fields['related_card_path'].choices += [
110
            ('', _('Other Card Identifiers')),
103 111
        ] + self.instance.get_related_card_paths()
104 112

  
105 113
    def save(self, *args, **kwargs):
......
108 116
            self.instance.custom_schema = {}
109 117
        if self.instance.related_card_path:
110 118
            self.instance.card_ids = ''
119
        if self.instance.related_card_path == '--':
120
            self.instance.related_card_path = ''
111 121
        self.instance.without_user = not self.cleaned_data['with_user']
112 122
        self.instance.save()
113 123
        return self.instance
114 124

  
125
    def clean(self):
126
        cleaned_data = super().clean()
127

  
128
        if not cleaned_data.get('related_card_path') and not cleaned_data.get('card_ids'):
129
            self.add_error('card_ids', _('This field is required.'))
130

  
131
        return cleaned_data
132

  
115 133

  
116 134
class WcsCategoryCellForm(forms.ModelForm):
117 135
    class Meta:
tests/test_wcs.py
1852 1852

  
1853 1853
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1854 1854
def test_card_cell_setup(mock_send, app, admin_user):
1855
    page = Page.objects.create(title='xxx', slug='test_card_cell_save_cache', template_name='standard')
1855
    page = Page.objects.create(
1856
        title='xxx', slug='test_card_cell_save_cache', template_name='standard', sub_slug='foobar'
1857
    )
1856 1858
    cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
1857 1859
    form_class = cell.get_default_form_class()
1858 1860
    form = form_class(instance=cell)
......
1909 1911
    cell.refresh_from_db()
1910 1912
    assert cell.custom_schema == {}
1911 1913

  
1914
    assert cell.related_card_path == ''
1915
    assert cell.card_ids == ''
1916
    resp = app.get('/manage/pages/%s/' % page.pk)
1917
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value == '--'
1918
    resp.forms[0]['c%s-card_ids' % cell.get_reference()].value = '42'
1919
    resp.forms[0].submit().follow()
1920
    cell.refresh_from_db()
1921
    assert cell.related_card_path == ''
1922
    assert cell.card_ids == ''
1923
    resp = app.get('/manage/pages/%s/' % page.pk)
1924
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value == '--'
1925
    resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value = ''
1926
    resp.forms[0]['c%s-card_ids' % cell.get_reference()].value = '42'
1927
    resp.forms[0].submit().follow()
1928
    cell.refresh_from_db()
1929
    assert cell.related_card_path == ''
1930
    assert cell.card_ids == '42'
1931

  
1932
    # current page has a sub_slug, '--' option is present
1933
    resp = app.get('/manage/pages/%s/' % page.pk)
1934
    assert '--' in [o[0] for o in resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options]
1935

  
1936
    # current_page has no sub_slug, but parent page has one
1937
    parent_page = Page.objects.create(
1938
        title='parent', slug='parent', template_name='standard', sub_slug='foobar'
1939
    )
1940
    page.parent = parent_page
1941
    page.sub_slug = ''
1942
    page.save()
1943
    resp = app.get('/manage/pages/%s/' % page.pk)
1944
    assert '--' in [o[0] for o in resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options]
1945

  
1946
    # no sub_slug
1947
    parent_page.sub_slug = ''
1948
    parent_page.save()
1949
    resp = app.get('/manage/pages/%s/' % page.pk)
1950
    assert '--' not in [o[0] for o in resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options]
1951
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].value == ''
1952
    resp.forms[0]['c%s-card_ids' % cell.get_reference()].value = ''
1953
    resp = resp.forms[0].submit()
1954
    assert resp.context['form'].errors == {'card_ids': ['This field is required.']}
1955

  
1912 1956

  
1913 1957
def test_card_cell_custom_schema_migration():
1914 1958
    cell = WcsCardInfosCell()
......
2092 2136

  
2093 2137
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
2094 2138
def test_manager_card_cell(mock_send, app, admin_user):
2095
    page = Page.objects.create(title='xxx', slug='test_cards', template_name='standard')
2139
    page = Page.objects.create(title='xxx', slug='test_cards', template_name='standard', sub_slug='foobar')
2096 2140
    cell = WcsCardInfosCell.objects.create(page=page, placeholder='content', order=0)
2097 2141

  
2098 2142
    app = login(app)
......
2119 2163
    resp = app.get('/manage/pages/%s/' % page.pk)
2120 2164
    # but only one cell on the page, no relations to follow
2121 2165
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2122
        ('', True, 'Other Card Identifiers')
2166
        ('--', True, 'Identifier from page URL'),
2167
        ('', False, 'Other Card Identifiers'),
2123 2168
    ]
2124 2169

  
2125 2170
    # add a second cell, related to the first card model
......
2129 2174
    resp = app.get('/manage/pages/%s/' % page.pk)
2130 2175
    # still no relation to follow
2131 2176
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2132
        ('', True, 'Other Card Identifiers')
2177
        ('--', True, 'Identifier from page URL'),
2178
        ('', False, 'Other Card Identifiers'),
2133 2179
    ]
2134 2180
    # no cell with id and slug
2135 2181
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2136
        ('', True, 'Other Card Identifiers')
2182
        ('--', True, 'Identifier from page URL'),
2183
        ('', False, 'Other Card Identifiers'),
2137 2184
    ]
2138 2185

  
2139 2186
    # set a slug on first cell
......
2142 2189
    resp = app.get('/manage/pages/%s/' % page.pk)
2143 2190
    # still no relation to follow
2144 2191
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2145
        ('', True, 'Other Card Identifiers')
2192
        ('--', True, 'Identifier from page URL'),
2193
        ('', False, 'Other Card Identifiers'),
2146 2194
    ]
2147 2195
    # multiple relations to follow
2148 2196
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2149
        ('', True, 'Other Card Identifiers'),
2197
        ('--', True, 'Identifier from page URL'),
2198
        ('', False, 'Other Card Identifiers'),
2150 2199
        ('sluga/cardb', False, 'sluga/cardb'),
2151 2200
        ('sluga/cardsb', False, 'sluga/cardsb'),
2152 2201
        ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
......
2161 2210
    resp = app.get('/manage/pages/%s/' % page.pk)
2162 2211
    # still no relation to follow
2163 2212
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2164
        ('', True, 'Other Card Identifiers')
2213
        ('--', False, 'Identifier from page URL'),
2214
        ('', True, 'Other Card Identifiers'),
2165 2215
    ]
2166 2216
    # can not user cell with multiple ids as reference
2167 2217
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2168
        ('', True, 'Other Card Identifiers')
2218
        ('--', True, 'Identifier from page URL'),
2219
        ('', False, 'Other Card Identifiers'),
2169 2220
    ]
2170 2221

  
2171 2222
    # define a slug on second cell
......
2176 2227
    resp = app.get('/manage/pages/%s/' % page.pk)
2177 2228
    # multiple relations to follow
2178 2229
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2179
        ('', True, 'Other Card Identifiers'),
2230
        ('--', True, 'Identifier from page URL'),
2231
        ('', False, 'Other Card Identifiers'),
2180 2232
        ('slugb/reverse:cardb', False, 'slugb/cardb (reverse)'),
2181 2233
        ('slugb/reverse:cardsb', False, 'slugb/cardsb (reverse)'),
2182 2234
        ('slugb/reverse:blockb_cardb', False, 'slugb/blockb_cardb (reverse)'),
2183 2235
    ]
2184 2236
    # still multiple relations to follow
2185 2237
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2186
        ('', True, 'Other Card Identifiers'),
2238
        ('--', True, 'Identifier from page URL'),
2239
        ('', False, 'Other Card Identifiers'),
2187 2240
        ('sluga/cardb', False, 'sluga/cardb'),
2188 2241
        ('sluga/cardsb', False, 'sluga/cardsb'),
2189 2242
        ('sluga/blockb_cardb', False, 'sluga/blockb_cardb'),
......
2202 2255
    resp = app.get('/manage/pages/%s/' % page.pk)
2203 2256
    # no more relation to follow
2204 2257
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2205
        ('', True, 'Other Card Identifiers')
2258
        ('--', True, 'Identifier from page URL'),
2259
        ('', False, 'Other Card Identifiers'),
2206 2260
    ]
2207 2261
    # still multiple relations to follow
2208 2262
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2263
        ('--', False, 'Identifier from page URL'),
2209 2264
        ('', False, 'Other Card Identifiers'),
2210 2265
        ('sluga/cardb', True, 'sluga/cardb'),
2211 2266
        ('sluga/cardsb', False, 'sluga/cardsb'),
......
2214 2269
        ('sluga/cardc/cardsb', False, 'sluga/cardc/cardsb'),
2215 2270
        ('sluga/cardc/blockb_cardb', False, 'sluga/cardc/blockb_cardb'),
2216 2271
    ]
2272
    resp.forms[1].submit()
2273
    cell2.refresh_from_db()
2274
    assert cell2.related_card_path == 'sluga/cardb'
2275
    assert cell2.card_ids == ''
2217 2276

  
2218 2277
    # check circular relations
2219 2278
    cell.slug = 'sluge'
......
2225 2284
    cell2.save()
2226 2285
    resp = app.get('/manage/pages/%s/' % page.pk)
2227 2286
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2228
        ('', True, 'Other Card Identifiers'),
2287
        ('--', True, 'Identifier from page URL'),
2288
        ('', False, 'Other Card Identifiers'),
2229 2289
        ('slugd/cardd-foo/carde-foo', False, 'slugd/cardd-foo/carde-foo'),
2230 2290
        ('slugd/carde-foo', False, 'slugd/carde-foo'),
2231 2291
    ]
2232 2292
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2233
        ('', True, 'Other Card Identifiers'),
2293
        ('--', True, 'Identifier from page URL'),
2294
        ('', False, 'Other Card Identifiers'),
2234 2295
        ('sluge/cardd-bar', False, 'sluge/cardd-bar'),
2235 2296
        ('sluge/reverse:carde-foo', False, 'sluge/carde-foo (reverse)'),
2236 2297
    ]
......
2244 2305
    cell2.save()
2245 2306
    resp = app.get('/manage/pages/%s/' % page.pk)
2246 2307
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2247
        ('', True, 'Other Card Identifiers'),
2308
        ('--', True, 'Identifier from page URL'),
2309
        ('', False, 'Other Card Identifiers'),
2248 2310
        ('slugd-bis/cardd-foo', False, 'slugd-bis/cardd-foo'),
2249 2311
        ('slugd-bis/reverse:cardd-foo', False, 'slugd-bis/cardd-foo (reverse)'),
2250 2312
        ('slugd-bis/carde-foo/cardd-bar', False, 'slugd-bis/carde-foo/cardd-bar'),
2251 2313
        ('slugd-bis/carde-foo/reverse:carde-foo', False, 'slugd-bis/carde-foo/carde-foo (reverse)'),
2252 2314
    ]
2253 2315
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2254
        ('', True, 'Other Card Identifiers'),
2316
        ('--', True, 'Identifier from page URL'),
2317
        ('', False, 'Other Card Identifiers'),
2255 2318
        ('slugd/cardd-foo', False, 'slugd/cardd-foo'),
2256 2319
        ('slugd/reverse:cardd-foo', False, 'slugd/cardd-foo (reverse)'),
2257 2320
        ('slugd/carde-foo/cardd-bar', False, 'slugd/carde-foo/cardd-bar'),
......
2267 2330
    cell2.save()
2268 2331
    resp = app.get('/manage/pages/%s/' % page.pk)
2269 2332
    assert resp.forms[0]['c%s-related_card_path' % cell.get_reference()].options == [
2270
        ('', True, 'Other Card Identifiers'),
2333
        ('--', True, 'Identifier from page URL'),
2334
        ('', False, 'Other Card Identifiers'),
2271 2335
        ('sluge-bis/cardd-bar/carde-foo', False, 'sluge-bis/cardd-bar/carde-foo'),
2272 2336
    ]
2273 2337
    assert resp.forms[1]['c%s-related_card_path' % cell2.get_reference()].options == [
2274
        ('', True, 'Other Card Identifiers'),
2338
        ('--', True, 'Identifier from page URL'),
2339
        ('', False, 'Other Card Identifiers'),
2275 2340
        ('sluge/cardd-bar/carde-foo', False, 'sluge/cardd-bar/carde-foo'),
2276 2341
    ]
2277 2342

  
2278
-