Add helper function, get_csrf_token()

This commit is contained in:
Lance Edgar 2020-03-24 18:19:05 -05:00
parent febe651e31
commit c14ecd2948
2 changed files with 13 additions and 4 deletions

View file

@ -36,7 +36,7 @@ from rattail.util import (pretty_quantity, pretty_hours, hours_as_decimal,
from webhelpers2.html import *
from webhelpers2.html.tags import *
from tailbone.util import csrf_token, pretty_datetime, raw_datetime
from tailbone.util import csrf_token, get_csrf_token, pretty_datetime, raw_datetime
def pretty_date(date):

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2019 Lance Edgar
# Copyright © 2010-2020 Lance Edgar
#
# This file is part of Rattail.
#
@ -40,13 +40,22 @@ from pyramid.renderers import get_renderer
from webhelpers2.html import HTML, tags
def csrf_token(request, name='_csrf'):
def get_csrf_token(request):
"""
Convenience function. Returns CSRF hidden tag inside hidden DIV.
Convenience function to retrieve the effective CSRF token for the given
request.
"""
token = request.session.get_csrf_token()
if token is None:
token = request.session.new_csrf_token()
return token
def csrf_token(request, name='_csrf'):
"""
Convenience function. Returns CSRF hidden tag inside hidden DIV.
"""
token = get_csrf_token(request)
return HTML.tag("div", tags.hidden(name, value=token), style="display:none;")