Skip to content

Commit

Permalink
fix black
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-bellenghi committed Oct 31, 2023
1 parent 6df850b commit 8e00fbc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/redturtle/volto/setuphandlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from Products.CMFPlone.interfaces import INonInstallable
from zope.interface import implementer
from redturtle.volto.upgrades import upgrade_robots_txt


@implementer(INonInstallable)
Expand All @@ -12,6 +13,7 @@ def getNonInstallableProfiles(self):

def post_install(context):
"""Post install script"""
upgrade_robots_txt(context)


def uninstall(context):
Expand Down
22 changes: 11 additions & 11 deletions src/redturtle/volto/upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,22 @@ def to_4200(context):


def upgrade_robots_txt(context):
robots = api.portal.get_registry_record('plone.robots_txt')
robots = api.portal.get_registry_record("plone.robots_txt")
lines = robots.splitlines()

googlebot_user_agent = 'User-Agent: Googlebot'
googlebot_user_agent = "User-Agent: Googlebot"
# I saw this writed also as googlebot, so:
normalized_google_bot_user_agent = googlebot_user_agent.lower().replace(' ','')
normalized_google_bot_user_agent = googlebot_user_agent.lower().replace(" ","")

useragent_string = 'User-Agent: '
normalized_useragent_string = useragent_string.lower().replace(' ', '')
useragent_string = "User-Agent: "
normalized_useragent_string = useragent_string.lower().replace(" ", "")

googlebot_allow_rule = 'Allow: /*?expand*'
googlebot_allow_rule = "Allow: /*?expand*"

googlebot_index = -1
allow_rule_present = False
for i, line in enumerate(lines):
if line.lower().replace(' ','') == normalized_google_bot_user_agent:
if line.lower().replace(" ","") == normalized_google_bot_user_agent:
googlebot_index = i
elif (googlebot_index != -1 and
line.strip().lower() == googlebot_allow_rule.lower()):
Expand All @@ -493,17 +493,17 @@ def upgrade_robots_txt(context):
end_googlebot_index = googlebot_index + 1
while (end_googlebot_index < len(lines) and
not lines[end_googlebot_index]
.lower().replace(' ', '')
.lower().replace(" ", "")
.startswith(normalized_useragent_string)):
end_googlebot_index += 1

# Aggiungi Allow: /*?expand* alla fine della sezione User-Agent: Googlebot
if lines[end_googlebot_index -1] == '':
if lines[end_googlebot_index -1] == "":
end_googlebot_index -= 1
lines.insert(end_googlebot_index, googlebot_allow_rule)

lines = '\n'.join(lines)
api.portal.set_registry_record('plone.robots_txt', lines)
lines = "\n".join(lines)
api.portal.set_registry_record("plone.robots_txt", lines)
logger.info("Upgrade robots.txt with rule for googlebot")
else:
logger.info("Rule for Googlebot already present in robots.txt, no action needed")
Expand Down

0 comments on commit 8e00fbc

Please sign in to comment.