Projet

Général

Profil

0001-seo-add-middleware-for-robots.txt-url-20263.patch

Nicolas Roche, 13 avril 2020 15:57

Télécharger (5,66 ko)

Voir les différences:

Subject: [PATCH 1/3] seo: add middleware for /robots.txt url (#20263)

 debian/debian_config_common.py |  4 ++++
 hobo/middleware/__init__.py    |  1 +
 hobo/middleware/seo.py         | 28 ++++++++++++++++++++++++++++
 tests/settings.py              |  2 ++
 tests/test_seo.py              | 30 ++++++++++++++++++++++++++++++
 5 files changed, 65 insertions(+)
 create mode 100644 hobo/middleware/seo.py
 create mode 100644 tests/test_seo.py
debian/debian_config_common.py
240 240
# make it easier to use runserver behind nginx as reverse proxy
241 241
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
242 242

  
243 243
if 'MIDDLEWARE' not in globals():
244 244
    MIDDLEWARE = global_settings.MIDDLEWARE
245 245

  
246 246
if 'MIDDLEWARE_CLASSES' in globals():
247 247
    MIDDLEWARE_CLASSES = (
248
        'hobo.middleware.RobotsTxtMiddleware',
248 249
        'hobo.middleware.VersionMiddleware',  # /__version__
249 250
        'hobo.middleware.cors.CORSMiddleware',
250 251
    ) + MIDDLEWARE_CLASSES
251 252

  
252 253
    if PROJECT_NAME != 'wcs' and 'authentic2' not in INSTALLED_APPS:
253 254
        MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
254 255
            'mellon.middleware.PassiveAuthenticationMiddleware',
255 256
        )
......
259 260
            'hobo.agent.authentic2.middleware.ProvisionningMiddleware',
260 261
        )
261 262
else:
262 263
    MIDDLEWARE = (
263 264
        'hobo.middleware.VersionMiddleware',  # /__version__
264 265
        'hobo.middleware.cors.CORSMiddleware',
265 266
    ) + MIDDLEWARE
266 267

  
268
    if PROJECT_NAME != 'wcs':
269
        MIDDLEWARE = ('hobo.middleware.RobotsTxtMiddleware',) + MIDDLEWARE
270

  
267 271
    if PROJECT_NAME != 'wcs' and 'authentic2' not in INSTALLED_APPS:
268 272
        MIDDLEWARE = MIDDLEWARE + (
269 273
            'mellon.middleware.PassiveAuthenticationMiddleware',
270 274
        )
271 275

  
272 276
    if 'authentic2' in INSTALLED_APPS:
273 277
        MIDDLEWARE = MIDDLEWARE + (
274 278
            'hobo.agent.authentic2.middleware.ProvisionningMiddleware',
hobo/middleware/__init__.py
1 1
from .version import VersionMiddleware
2 2
from .cors import CORSMiddleware
3
from .seo import RobotsTxtMiddleware
3 4
from .stats import PrometheusStatsMiddleware
hobo/middleware/seo.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2020  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf import settings
18
from django.http import HttpResponse
19
from django.utils.deprecation import MiddlewareMixin
20

  
21

  
22
class RobotsTxtMiddleware(MiddlewareMixin):
23
    def process_request(self, request):
24
        if request.method == 'GET' and request.path == '/robots.txt' and hasattr(settings, 'TEMPLATE_VARS'):
25
            return HttpResponse(
26
                settings.TEMPLATE_VARS.get('robots_txt', ''),
27
                content_type='text/plain')
28
        return None
tests/settings.py
3 3
OZWILLO_SECRET = 'secret'
4 4

  
5 5
INSTALLED_APPS += ('hobo.contrib.ozwillo',)
6 6

  
7 7
ALLOWED_HOSTS.append('localhost')
8 8

  
9 9
TEMPLATES[0]['OPTIONS']['debug'] = True
10 10

  
11
MIDDLEWARE_CLASSES = ('hobo.middleware.RobotsTxtMiddleware', ) + MIDDLEWARE_CLASSES
12

  
11 13
HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url'
tests/test_seo.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2020  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.import pytest
16

  
17
import pytest
18

  
19
from django.test import override_settings
20

  
21

  
22
pytestmark = pytest.mark.django_db
23

  
24
def test_middleware(app):
25
    with override_settings(TEMPLATE_VARS={'robots_txt': 'xxx'}):
26
        resp = app.get('/robots.txt', status=200)
27
        assert resp.text == 'xxx'
28

  
29
    with override_settings():
30
        resp = app.get('/robots.txt', status=404)
0
-