Projet

Général

Profil

« Précédent | Suivant » 

Révision 31c5db7b

Ajouté par Frédéric Péters il y a plus d'un an

Revert "misc: remove redirection to /category/formdef, handled by w.c.s. (#69546)"

This reverts commit efe6c53faffafd77527caf89833e6139cae6963b.

Voir les différences:

auquotidien/modules/root.py
288 288
        if not self.backoffice:
289 289
            self.backoffice = get_publisher().backoffice_directory_class()
290 290

  
291
        return super()._q_traverse(path)
291
        try:
292
            output = Directory._q_traverse(self, path)
293
            if path and path[0] == 'saml':
294
                return output
295
            return self.automatic_sso(output)
296
        except errors.TraversalError as e:
297
            try:
298
                f = FormDef.get_by_urlname(path[0])
299
            except KeyError:
300
                pass
301
            else:
302
                base_url = get_publisher().get_root_url()
303

  
304
                uri_rest = get_request().environ.get('REQUEST_URI')
305
                if not uri_rest:
306
                    # REQUEST_URI doesn't exist when using internal HTTP server
307
                    # (--http)
308
                    uri_rest = urllib.parse.quote(get_request().get_path())
309
                    if get_request().get_query():
310
                        uri_rest += '?' + get_request().get_query()
311
                if uri_rest.startswith(base_url):
312
                    uri_rest = uri_rest[len(base_url) :]
313
                if f.category:
314
                    if f.category.url_name == f.url_name:
315
                        return FormsRootDirectory(f.category)._q_traverse(path[1:])
316
                    return redirect('%s%s/%s' % (base_url, f.category.url_name, uri_rest))
317

  
318
            try:
319
                cat = Category.get_by_urlname(path[0])
320
            except KeyError:
321
                pass
322
            else:
323
                if len(path) == 1:
324
                    # category with missing trailing slash, redirect.
325
                    path = quixote.get_path()
326
                    return quixote.redirect(path + "/", permanent=True)
327
                return FormsRootDirectory(cat)._q_traverse(path[1:])
328

  
329
            raise e
330

  
331
    def _q_lookup(self, component):
332
        # is this a category ?
333
        try:
334
            category = Category.get_by_urlname(component)
335
        except KeyError:
336
            category = None
337

  
338
        # is this a formdef ?
339
        try:
340
            formdef = FormDef.get_by_urlname(component)
341
        except KeyError:
342
            if category:
343
                return FormsRootDirectory(category)
344
        else:
345
            # if the form has no category, or the request is a POST, or the
346
            # slug matches both a category and a formdef, directly call
347
            # into FormsRootDirectory.
348
            if formdef.category_id is None or get_request().get_method() == 'POST' or (formdef and category):
349
                get_response().filter['bigdiv'] = 'rub_service'
350
                return FormsRootDirectory()._q_lookup(component)
351

  
352
            # if there is category, let it fall back to raise TraversalError,
353
            # it will get caught in _q_traverse that will redirect it to an
354
            # URL embedding the category
355

  
356
        return None
292 357

  
293 358
    def json(self):
294 359
        return FormsRootDirectory().json()
tests/test_user_pages.py
159 159
    assert resp.location.endswith('/baz/')
160 160
    resp = get_app(pub).get('/baz?test=1')
161 161
    assert resp.location.endswith('/baz/')
162

  
163

  
164
def test_form_and_category_same_slug():
165
    Category.wipe()
166
    cat = Category(name='foobar')
167
    cat.store()
168

  
169
    FormDef.wipe()
170
    formdef = FormDef()
171
    formdef.name = 'foobar'
172
    formdef.category_id = cat.id
173
    formdef.fields = []
174
    formdef.store()
175

  
176
    # check we get to the form, not the category
177
    resp = get_app(pub).get('/foobar/')
178
    assert resp.form
179

  
180

  
181
def test_form_and_category_same_slug2():
182
    Category.wipe()
183
    cat = Category(name='test')
184
    cat.store()
185

  
186
    FormDef.wipe()
187
    formdef = FormDef()
188
    formdef.name = 'foobar'
189
    formdef.category_id = cat.id
190
    formdef.fields = []
191
    formdef.store()
192

  
193
    formdef = FormDef()
194
    formdef.name = 'test'
195
    formdef.fields = []
196
    formdef.store()
197

  
198
    # check we get to the foobar form
199
    resp = get_app(pub).get('/foobar/')
200
    assert resp.location == 'http://example.net/test/foobar/'
201
    resp = resp.follow()
202
    assert resp.form
203
    assert '<h1>foobar</h1>' in resp
204

  
205
    # check we get to the test form
206
    resp = get_app(pub).get('/test/')
207
    assert resp.form
208
    assert '<h1>test</h1>' in resp

Formats disponibles : Unified diff