Commit 44560e76 authored by Robin Sonnabend's avatar Robin Sonnabend
Browse files

Replace request.args.get("next") by redirect back

More or less directly taken from http://flask.pocoo.org/snippets/120/
parent bf4c10f4
# This snippet is in public domain.
# However, please retain this link in your sources:
# http://flask.pocoo.org/snippets/120/
# Danya Alexeyevsky
import functools
from flask import session, request, redirect as flask_redirect, url_for
import config
cookie = getattr(config, "REDIRECT_BACK_COOKIE", "back")
default_view = getattr(config, "REDIRECT_BACK_DEFAULT", "index")
def anchor(func, cookie=cookie):
@functools.wraps(func)
def result(*args, **kwargs):
session[cookie] = request.url
return func(*args, **kwargs)
return result
def url(default=default_view, cookie=cookie, **url_args):
return session.get(cookie, url_for(default, **url_args))
def redirect(default=default_view, cookie=cookie, **url_args):
return flask_redirect(url(default, cookie, **url_args))
...@@ -4,6 +4,7 @@ from functools import wraps ...@@ -4,6 +4,7 @@ from functools import wraps
from models.database import ALL_MODELS from models.database import ALL_MODELS
from shared import db, current_user from shared import db, current_user
import back
ID_KEY = "id" ID_KEY = "id"
KEY_NOT_PRESENT_MESSAGE = "Missing {}_id." KEY_NOT_PRESENT_MESSAGE = "Missing {}_id."
...@@ -12,10 +13,10 @@ OBJECT_DOES_NOT_EXIST_MESSAGE = "There is no {} with id {}." ...@@ -12,10 +13,10 @@ OBJECT_DOES_NOT_EXIST_MESSAGE = "There is no {} with id {}."
MISSING_VIEW_RIGHT = "Dir fehlenden die nötigen Zugriffsrechte." MISSING_VIEW_RIGHT = "Dir fehlenden die nötigen Zugriffsrechte."
def default_redirect(): def default_redirect():
return redirect(request.args.get("next") or url_for("index")) return back.redirect()
def login_redirect(): def login_redirect():
return redirect(request.args.get("next") or url_for("login")) return back.redirect("login")
def db_lookup(*models, check_exists=True): def db_lookup(*models, check_exists=True):
def _decorator(function): def _decorator(function):
......
This diff is collapsed.
...@@ -5,6 +5,8 @@ import re ...@@ -5,6 +5,8 @@ import re
from functools import wraps from functools import wraps
from enum import Enum from enum import Enum
import back
import config import config
db = SQLAlchemy() db = SQLAlchemy()
...@@ -110,7 +112,7 @@ def login_required(function): ...@@ -110,7 +112,7 @@ def login_required(function):
if check_login(): if check_login():
return function(*args, **kwargs) return function(*args, **kwargs)
else: else:
return redirect(url_for("login", next=request.url)) return redirect(url_for("login"))
return decorated_function return decorated_function
def group_required(group): def group_required(group):
...@@ -121,7 +123,7 @@ def group_required(group): ...@@ -121,7 +123,7 @@ def group_required(group):
return function(*args, **kwargs) return function(*args, **kwargs)
else: else:
flash("You do not have the necessary permissions to view this page.") flash("You do not have the necessary permissions to view this page.")
return redirect(request.args.get("next") or url_for("index")) return back.redirect()
return decorated_function return decorated_function
return decorator return decorator
......
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
{% block content %} {% block content %}
<div class="container"> <div class="container">
{{render_form(form, action_url=url_for("edit_document", document_id=document.id, next=request.args.get("next") or url_for("show_protocol", protocol_id=document.protocol.id)), action_text="Ändern")}} {{render_form(form, action_url=url_for("edit_document", document_id=document.id), action_text="Ändern")}}
</div> </div>
{% endblock %} {% endblock %}
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
{% block content %} {% block content %}
<div class="container"> <div class="container">
<h3>{{localtop.defaulttop.name}}</h3> <h3>{{localtop.defaulttop.name}}</h3>
{{render_form(form, action_url=url_for("edit_localtop", localtop_id=localtop.id, next=request.args.get("next") or url_for("show_protocol", protocol_id=localtop.protocol.id)), action_text="Ändern", textarea_rows=5)}} {{render_form(form, action_url=url_for("edit_localtop", localtop_id=localtop.id), action_text="Ändern", textarea_rows=5)}}
</div> </div>
{% endblock %} {% endblock %}
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
{% block content %} {% block content %}
<div class="container"> <div class="container">
{{render_form(form, action_url=url_for("edit_top", top_id=top.id, next=request.args.get("next") or url_for("show_protocol", protocol_id=top.protocol.id)), action_text="Ändern", textarea_rows=5)}} {{render_form(form, action_url=url_for("edit_top", top_id=top.id), action_text="Ändern", textarea_rows=5)}}
</div> </div>
{% endblock %} {% endblock %}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment