diff --git a/frappe/__init__.py b/frappe/__init__.py index add9880306..19cc2945ec 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -952,6 +952,40 @@ def respond_as_web_page(title, html, success=None, http_status_code=None, contex if context: local.response['context'] = context +def redirect_to_message(title, html, http_status_code=None, context=None): + """Redirects to /message?id=random + Similar to respond_as_web_page, but used to 'redirect' and show message pages like success, failure, etc. with a detailed message + + :param title: Page title and heading. + :param message: Message to be shown. + :param http_status_code: HTTP status code. + + Example Usage: + frappe.redirect_to_message(_('Thank you'), "

You will receive an email at test@example.com

") + + """ + + message_id = generate_hash(length=32) + message = { + 'context': context or {}, + 'http_status_code': http_status_code or 200 + } + message['context'].update({ + 'header': title, + 'title': title, + 'message': html + }) + + cache().set_value("message_id:{0}".format(message_id), message, expires_in_sec=60) + location = '/message?id={0}'.format(message_id) + + if not getattr(local, 'is_ajax', False): + local.response["type"] = "redirect" + local.response["location"] = location + + else: + return location + def build_match_conditions(doctype, as_condition=True): """Return match (User permissions) for given doctype as list or SQL.""" import frappe.desk.reportview diff --git a/frappe/templates/pages/message.py b/frappe/templates/pages/message.py index 6bb1c9d8d2..cf57904040 100644 --- a/frappe/templates/pages/message.py +++ b/frappe/templates/pages/message.py @@ -18,4 +18,18 @@ def get_context(context): if hasattr(frappe.local, "message_success"): message_context["success"] = frappe.local.message_success + elif frappe.local.form_dict.id: + message_id = frappe.local.form_dict.id + key = "message_id:{0}".format(message_id) + message = frappe.cache().get_value(key, expires=True) + if message: + message_context.update(message.get('context', {})) + if message.get('http_status_code'): + frappe.local.response['http_status_code'] = message['http_status_code'] + + else: + message_context = { + 'message': '' + } + return message_context