Add subscriber hook for setting inbox count in template context.

This commit is contained in:
Lance Edgar 2016-02-11 18:47:17 -06:00
parent c65bc6f229
commit 7649bf142b

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python # -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar # Copyright © 2010-2016 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -21,20 +20,23 @@
# along with Rattail. If not, see <http://www.gnu.org/licenses/>. # along with Rattail. If not, see <http://www.gnu.org/licenses/>.
# #
################################################################################ ################################################################################
""" """
Event Subscribers Event Subscribers
""" """
from pyramid import threadlocal from __future__ import unicode_literals, absolute_import
import rattail
from . import helpers
from pyramid.security import authenticated_userid import rattail
from .db import Session from rattail import enum
from rattail.db.model import User from rattail.db import model
from rattail.db.auth import has_permission from rattail.db.auth import has_permission
from pyramid import threadlocal
from pyramid.security import authenticated_userid
from tailbone import helpers
from tailbone.db import Session
def add_rattail_config_attribute_to_request(event): def add_rattail_config_attribute_to_request(event):
""" """
@ -78,6 +80,24 @@ def before_render(event):
renderer_globals['rattail'] = rattail renderer_globals['rattail'] = rattail
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
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()
def context_found(event): def context_found(event):
""" """
Attach some goodies to the request object. Attach some goodies to the request object.
@ -96,7 +116,7 @@ def context_found(event):
request.user = None request.user = None
uuid = authenticated_userid(request) uuid = authenticated_userid(request)
if uuid: if uuid:
request.user = Session.query(User).get(uuid) request.user = Session.query(model.User).get(uuid)
if request.user: if request.user:
Session().set_continuum_user(request.user) Session().set_continuum_user(request.user)