From 77c4b53b712e9af2f47389e44a53c86f8ea2bd62 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Tue, 7 Sep 2021 16:30:14 +0530 Subject: [PATCH] feat: added redirect rule to redirect /profile_/foo to the profile url Hack to allow redirecting to profile url from JS as it doesn't know the `profile_url_prefix`. Issue #192 --- community/hooks.py | 1 + community/page_renderers.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/community/hooks.py b/community/hooks.py index 264b1609..7bfb7a70 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -174,6 +174,7 @@ community_markdown_macro_renderers = { # page_renderer to manage profile pages page_renderer = [ + "community.page_renderers.ProfileRedirectPage", "community.page_renderers.ProfilePage" ] diff --git a/community/page_renderers.py b/community/page_renderers.py index c0fa00b6..7cb0c75d 100644 --- a/community/page_renderers.py +++ b/community/page_renderers.py @@ -40,6 +40,20 @@ def get_profile_url_prefix(): RE_USERNAME = re.compile("[a-zA-Z0-9_]{4,}") +class ProfileRedirectPage(BaseRenderer): + """Renderer to redirect /profile_/foo to /foo. + + This is useful to redirect to profile pages from javascript as there is no + easy to find the profile prefix. + """ + def can_render(self): + return self.path.startswith("profile_/") + + def render(self): + username = self.path[len("profile_/"):] + frappe.flags.redirect_location = get_profile_url_prefix() + username + return RedirectPage(self.path).render() + class ProfilePage(BaseRenderer): def __init__(self, path, http_status_code): super().__init__(path, http_status_code)