Projet

Général

Profil

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

Nicolas Roche, 07 avril 2020 19:17

Télécharger (4,18 ko)

Voir les différences:

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

 hobo/middleware/seo.py | 29 +++++++++++++++++++++++++++++
 hobo/settings.py       |  1 +
 tests/test_seo.py      | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 hobo/middleware/seo.py
 create mode 100644 tests/test_seo.py
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.http import HttpResponse
18
from django.utils.deprecation import MiddlewareMixin
19

  
20
from hobo.environment.utils import get_variable
21

  
22

  
23
class RobotMiddleware(MiddlewareMixin):
24
    def process_request(self, request):
25
        if request.method == 'GET' and request.path == '/robots.txt':
26
            return HttpResponse(
27
                get_variable('robots_txt').value,
28
                content_type='text/plain')
29
        return None
hobo/settings.py
47 47
    'hobo.matomo',
48 48
    'hobo.profile',
49 49
    'hobo.theme',
50 50
    'hobo.emails',
51 51
    'hobo.deploy',
52 52
)
53 53

  
54 54
MIDDLEWARE_CLASSES = (
55
    'hobo.middleware.seo.RobotMiddleware',
55 56
    'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
56 57
    'django.contrib.sessions.middleware.SessionMiddleware',
57 58
    'django.middleware.locale.LocaleMiddleware',
58 59
    'django.middleware.common.CommonMiddleware',
59 60
    'django.middleware.csrf.CsrfViewMiddleware',
60 61
    'django.contrib.auth.middleware.AuthenticationMiddleware',
61 62
    'django.contrib.messages.middleware.MessageMiddleware',
62 63
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
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
import pytest
17

  
18
from hobo.environment.utils import get_variable
19
from hobo.seo.views import ALLOW, DISALLOW
20

  
21

  
22
pytestmark = pytest.mark.django_db
23

  
24
def test_middleware(app):
25
    variable = get_variable('robots_txt')
26
    variable.value = DISALLOW
27
    variable.save()
28
    resp = app.get('/robots.txt', status=200)
29
    assert resp.text == DISALLOW
30

  
31
    variable = get_variable('robots_txt')
32
    variable.value = ALLOW
33
    variable.save()
34
    resp = app.get('/robots.txt', status=200)
35
    assert resp.text == ALLOW
36

  
37
    variable = get_variable('robots_txt')
38
    variable.delete()
39
    resp = app.get('/robots.txt', status=200)
40
    assert resp.text == ALLOW
0
-