Added setup_user_in_request
This commit is contained in:
parent
b058cf15f0
commit
58dec5efe5
@ -15,6 +15,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
|
import mongokit
|
||||||
|
|
||||||
def get_jinja_env(user_template_path=None):
|
def get_jinja_env(user_template_path=None):
|
||||||
if user_template_path:
|
if user_template_path:
|
||||||
@ -25,3 +26,27 @@ def get_jinja_env(user_template_path=None):
|
|||||||
loader = jinja2.PackageLoader('mediagoblin', 'templates')
|
loader = jinja2.PackageLoader('mediagoblin', 'templates')
|
||||||
|
|
||||||
return jinja2.Environment(loader=loader, autoescape=True)
|
return jinja2.Environment(loader=loader, autoescape=True)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_user_in_request(request):
|
||||||
|
"""
|
||||||
|
Examine a request and tack on a request.user parameter if that's
|
||||||
|
appropriate.
|
||||||
|
"""
|
||||||
|
if not request.session.has_key('user_id'):
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
user = request.db.User.one({'_id': request.session['user_id']})
|
||||||
|
|
||||||
|
if user:
|
||||||
|
request.user = user
|
||||||
|
else:
|
||||||
|
# Something's wrong... this user doesn't exist? Invalidate
|
||||||
|
# this session.
|
||||||
|
request.session.invalidate()
|
||||||
|
|
||||||
|
except mongokit.MultipleResultsFound:
|
||||||
|
# Something's wrong... we shouldn't have multiple users with
|
||||||
|
# the same user id. Invalidate this session.
|
||||||
|
request.session.invalidate()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user