Skip to content

Commit

Permalink
fix: validate website settings (#18446) (#18469)
Browse files Browse the repository at this point in the history
(cherry picked from commit c2f43c4)

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Oct 19, 2022
1 parent cb4d825 commit 9e29a94
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
31 changes: 29 additions & 2 deletions frappe/website/doctype/website_settings/test_website_settings.py
@@ -1,8 +1,35 @@
# Copyright (c) 2020, Frappe Technologies and Contributors
# License: MIT. See LICENSE
# import frappe

import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.website.doctype.website_settings.website_settings import get_website_settings


class TestWebsiteSettings(FrappeTestCase):
pass
def test_child_items_in_top_bar(self):
ws = frappe.get_doc("Website Settings")
ws.append(
"top_bar_items",
{"label": "Parent Item"},
)
ws.append(
"top_bar_items",
{"parent_label": "Parent Item", "label": "Child Item"},
)
ws.save()

context = get_website_settings()

for item in context.top_bar_items:
if item.label == "Parent Item":
self.assertEqual(item.child_items[0].label, "Child Item")
break
else:
self.fail("Child items not found")

def test_redirect_setups(self):
ws = frappe.get_doc("Website Settings")

ws.append("route_redirects", {"source": "/engineering/(*.)", "target": "/development/(*.)"})
self.assertRaises(frappe.ValidationError, ws.validate)
12 changes: 12 additions & 0 deletions frappe/website/doctype/website_settings/website_settings.py
@@ -1,5 +1,6 @@
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import re
from urllib.parse import quote

import frappe
Expand All @@ -16,6 +17,7 @@ def validate(self):
self.validate_footer_items()
self.validate_home_page()
self.validate_google_settings()
self.validate_redirects()

def validate_home_page(self):
if frappe.flags.in_install:
Expand Down Expand Up @@ -72,6 +74,16 @@ def validate_google_settings(self):
if self.enable_google_indexing and not frappe.db.get_single_value("Google Settings", "enable"):
frappe.throw(_("Enable Google API in Google Settings."))

def validate_redirects(self):
for idx, row in enumerate(self.route_redirects):
try:
source = row.source.strip("/ ") + "$"
re.compile(source)
re.sub(source, row.target, "")
except Exception as e:
if not frappe.flags.in_migrate:
frappe.throw(_("Invalid redirect regex in row #{}: {}").format(idx, str(e)))

def on_update(self):
self.clear_cache()

Expand Down

0 comments on commit 9e29a94

Please sign in to comment.