2017-07-07 09:13:53 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2012-04-10 12:39:30 -05:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2017-02-02 21:44:13 -06:00
|
|
|
# Copyright © 2010-2017 Lance Edgar
|
2012-04-10 12:39:30 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2012-04-10 12:39:30 -05:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2012-04-10 12:39:30 -05:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-10 12:39:30 -05:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
2013-09-01 09:27:47 -05:00
|
|
|
Event Subscribers
|
2012-04-10 12:39:30 -05:00
|
|
|
"""
|
|
|
|
|
2016-02-11 18:47:17 -06:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
2017-07-07 09:13:53 -05:00
|
|
|
import six
|
2017-11-02 23:38:16 -05:00
|
|
|
import json
|
2018-01-25 17:02:53 -06:00
|
|
|
import datetime
|
2017-07-07 09:13:53 -05:00
|
|
|
|
2012-04-10 12:39:30 -05:00
|
|
|
import rattail
|
2016-02-11 18:47:17 -06:00
|
|
|
from rattail.db import model
|
2017-02-02 21:44:13 -06:00
|
|
|
from rattail.db.auth import has_permission
|
2012-04-10 12:39:30 -05:00
|
|
|
|
2016-02-11 18:47:17 -06:00
|
|
|
from pyramid import threadlocal
|
|
|
|
|
2016-10-07 19:33:41 -05:00
|
|
|
import tailbone
|
2016-02-11 18:47:17 -06:00
|
|
|
from tailbone import helpers
|
|
|
|
from tailbone.db import Session
|
2013-09-01 17:31:50 -05:00
|
|
|
|
2012-04-10 12:39:30 -05:00
|
|
|
|
2014-02-21 12:10:10 -06:00
|
|
|
def add_rattail_config_attribute_to_request(event):
|
|
|
|
"""
|
|
|
|
Add a ``rattail_config`` attribute to a request object.
|
|
|
|
|
|
|
|
This function is really just a matter of convenience, but it should help to
|
|
|
|
make other code more terse (example below). It is designed to act as a
|
|
|
|
subscriber to the Pyramid ``NewRequest`` event.
|
|
|
|
|
|
|
|
A global Rattail ``config`` should already be present within the Pyramid
|
|
|
|
application registry's settings, which would normally be accessed via::
|
|
|
|
|
|
|
|
request.registry.settings['rattail_config']
|
|
|
|
|
|
|
|
This function merely "promotes" this config object so that it is more
|
|
|
|
directly accessible, a la::
|
|
|
|
|
|
|
|
request.rattail_config
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
All this of course assumes that a Rattail ``config`` object *has* in
|
|
|
|
fact already been placed in the application registry settings. If this
|
|
|
|
is not the case, this function will do nothing.
|
|
|
|
"""
|
|
|
|
request = event.request
|
|
|
|
rattail_config = request.registry.settings.get('rattail_config')
|
|
|
|
if rattail_config:
|
|
|
|
request.rattail_config = rattail_config
|
|
|
|
|
|
|
|
|
2012-04-10 12:39:30 -05:00
|
|
|
def before_render(event):
|
|
|
|
"""
|
2013-09-01 17:31:50 -05:00
|
|
|
Adds goodies to the global template renderer context.
|
2012-04-10 12:39:30 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
request = event.get('request') or threadlocal.get_current_request()
|
|
|
|
|
|
|
|
renderer_globals = event
|
2013-05-03 01:12:03 -05:00
|
|
|
renderer_globals['h'] = helpers
|
2013-09-01 17:31:50 -05:00
|
|
|
renderer_globals['url'] = request.route_url
|
2012-04-10 12:39:30 -05:00
|
|
|
renderer_globals['rattail'] = rattail
|
2016-10-07 19:33:41 -05:00
|
|
|
renderer_globals['tailbone'] = tailbone
|
2016-11-21 01:07:35 -06:00
|
|
|
renderer_globals['enum'] = request.rattail_config.get_enum()
|
2017-07-07 09:13:53 -05:00
|
|
|
renderer_globals['six'] = six
|
2017-11-02 23:38:16 -05:00
|
|
|
renderer_globals['json'] = json
|
2018-01-25 17:02:53 -06:00
|
|
|
renderer_globals['datetime'] = datetime
|
2012-04-10 12:39:30 -05:00
|
|
|
|
|
|
|
|
2016-02-11 18:47:17 -06:00
|
|
|
def add_inbox_count(event):
|
|
|
|
"""
|
|
|
|
Adds the current user's inbox message count to the global renderer context.
|
|
|
|
|
|
|
|
Note that this is not enabled by default; to turn it on you must do this:
|
|
|
|
|
|
|
|
config.add_subscriber('tailbone.subscribers.add_inbox_count', 'pyramid.events.BeforeRender')
|
|
|
|
"""
|
|
|
|
request = event.get('request') or threadlocal.get_current_request()
|
|
|
|
if request.user:
|
|
|
|
renderer_globals = event
|
2016-11-21 01:07:35 -06:00
|
|
|
enum = request.rattail_config.get_enum()
|
2016-02-11 18:47:17 -06:00
|
|
|
renderer_globals['inbox_count'] = Session.query(model.Message)\
|
|
|
|
.outerjoin(model.MessageRecipient)\
|
|
|
|
.filter(model.MessageRecipient.recipient == Session.merge(request.user))\
|
|
|
|
.filter(model.MessageRecipient.status == enum.MESSAGE_STATUS_INBOX)\
|
|
|
|
.count()
|
|
|
|
|
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
def context_found(event):
|
|
|
|
"""
|
|
|
|
Attach some goodies to the request object.
|
|
|
|
|
|
|
|
The following is attached to the request:
|
|
|
|
|
|
|
|
* The currently logged-in user instance (if any), as ``user``.
|
|
|
|
|
2016-10-18 16:59:38 -05:00
|
|
|
* ``is_admin`` flag indicating whether user has the Administrator role.
|
|
|
|
|
|
|
|
* ``is_root`` flag indicating whether user is currently elevated to root.
|
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
* A shortcut method for permission checking, as ``has_perm()``.
|
|
|
|
|
|
|
|
* A shortcut method for fetching the referrer, as ``get_referrer()``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
request = event.request
|
|
|
|
|
|
|
|
request.user = None
|
2016-10-18 17:54:41 -05:00
|
|
|
uuid = request.authenticated_userid
|
2013-09-01 17:31:50 -05:00
|
|
|
if uuid:
|
2016-02-11 18:47:17 -06:00
|
|
|
request.user = Session.query(model.User).get(uuid)
|
2015-02-09 15:33:16 -06:00
|
|
|
if request.user:
|
|
|
|
Session().set_continuum_user(request.user)
|
2013-09-01 17:31:50 -05:00
|
|
|
|
2017-02-02 21:44:13 -06:00
|
|
|
request.is_admin = bool(request.user) and request.user.is_admin()
|
2016-10-18 16:59:38 -05:00
|
|
|
request.is_root = request.is_admin and request.session.get('is_root', False)
|
|
|
|
|
|
|
|
def has_perm(name):
|
|
|
|
if has_permission(Session(), request.user, name):
|
|
|
|
return True
|
|
|
|
return request.is_root
|
2013-09-01 17:31:50 -05:00
|
|
|
request.has_perm = has_perm
|
|
|
|
|
2016-10-18 16:59:38 -05:00
|
|
|
def has_any_perm(*names):
|
|
|
|
for name in names:
|
|
|
|
if has_perm(name):
|
2013-09-01 17:31:50 -05:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
request.has_any_perm = has_any_perm
|
|
|
|
|
|
|
|
def get_referrer(default=None):
|
|
|
|
if request.params.get('referrer'):
|
|
|
|
return request.params['referrer']
|
|
|
|
if request.session.get('referrer'):
|
|
|
|
return request.session.pop('referrer')
|
|
|
|
referrer = request.referrer
|
2015-03-05 16:55:09 -06:00
|
|
|
if (not referrer or referrer == request.current_route_url()
|
|
|
|
or not referrer.startswith(request.host_url)):
|
|
|
|
referrer = default or request.route_url('home')
|
2013-09-01 17:31:50 -05:00
|
|
|
return referrer
|
|
|
|
request.get_referrer = get_referrer
|
|
|
|
|
2017-02-16 13:32:20 -06:00
|
|
|
def get_session_timeout():
|
|
|
|
"""
|
|
|
|
Returns the timeout in effect for the current session
|
|
|
|
"""
|
|
|
|
return request.session.get('_timeout')
|
|
|
|
request.get_session_timeout = get_session_timeout
|
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
|
2012-04-10 12:39:30 -05:00
|
|
|
def includeme(config):
|
2014-02-21 12:10:10 -06:00
|
|
|
config.add_subscriber(add_rattail_config_attribute_to_request, 'pyramid.events.NewRequest')
|
2013-09-01 17:31:50 -05:00
|
|
|
config.add_subscriber(before_render, 'pyramid.events.BeforeRender')
|
|
|
|
config.add_subscriber(context_found, 'pyramid.events.ContextFound')
|