* fix(login): redirect user from login page if already logged in the user should not be able to access the login page if a user session already exists. closes #6500. Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com> * fix(test-website): fix website test what is the point in writing tests if they don't really work/function as intended? Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com> * fix(regex): do not replace '\' in rules that defeats the entire purpose of creating rules, wtf? Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com> * fix(test_website): change user using set_user() Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com> * redirect: prefix string with r to escape string literals Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
39 lines
994 B
Python
39 lines
994 B
Python
from __future__ import unicode_literals
|
|
|
|
import re, frappe
|
|
|
|
def resolve_redirect(path):
|
|
'''
|
|
Resolve redirects from hooks
|
|
|
|
Example:
|
|
|
|
website_redirect = [
|
|
# absolute location
|
|
{"source": "/from", "target": "https://mysite/from"},
|
|
|
|
# relative location
|
|
{"source": "/from", "target": "/main"},
|
|
|
|
# use regex
|
|
{"source": r"/from/(.*)", "target": r"/main/\1"}
|
|
# use r as a string prefix if you use regex groups or want to escape any string literal
|
|
]
|
|
'''
|
|
redirects = frappe.get_hooks('website_redirects')
|
|
if not redirects: return
|
|
|
|
redirect_to = frappe.cache().hget('website_redirects', path)
|
|
|
|
if redirect_to:
|
|
frappe.flags.redirect_location = redirect_to
|
|
raise frappe.Redirect
|
|
|
|
for rule in redirects:
|
|
pattern = rule['source'].strip('/ ') + '$'
|
|
if re.match(pattern, path):
|
|
redirect_to = re.sub(pattern, rule['target'], path)
|
|
frappe.flags.redirect_location = redirect_to
|
|
frappe.cache().hset('website_redirects', path, redirect_to)
|
|
raise frappe.Redirect
|
|
|