tailbone/rattail/pyramid/util.py
2012-04-16 00:51:12 -05:00

97 lines
3 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``rattail.pyramid.util`` -- Utilities
"""
from pyramid import threadlocal
from webhelpers.html import tags
import edbob
from edbob.pyramid import Session
import rattail
def get_column(sil_name):
"""
Returns the :class:`rattail.SilColumn` instance with the given SIL name.
"""
q = Session.query(rattail.SilColumn)
q = q.filter(rattail.SilColumn.sil_name == sil_name)
if q.count() == 1:
return q.one()
def get_dictionary(name, flash=False):
"""
Returns the :class:`rattail.BatchDictionary` instance with the given name.
"""
q = Session.query(rattail.BatchDictionary)
q = q.filter(rattail.BatchDictionary.name == name)
if q.count() == 1:
return q.one()
if flash:
request = threadlocal.get_current_request()
dct = tags.link_to("Batch Dictionary", request.route_url('batch_dictionaries'))
request.session.flash("Hm, I couldn't find the '%s' %s." % (name, dct))
def get_terminal(key=None, default='rattail', title="Rattail"):
"""
Returns the :class:`rattail.BatchTerminal` instance with the given SIL ID.
If ``key`` is specified, it will be used to obtain the SIL ID from config.
If no key is given, or config contains no appropriate value, then
``default`` will be used as the SIL ID.
``title`` is used for a flash message, should no such terminal be found.
.. highlight:: ini
Given a ``key`` value of ``'products'``, a SIL ID of ``'rattail.locsms'``
should be configured like this::
[rattail.pyramid]
batch_terminal.products = rattail.locsms
"""
if key:
sil_id = edbob.config.get('rattail.pyramid', 'batch_terminal.%s' % key,
default=default)
assert sil_id
q = Session.query(rattail.BatchTerminal)
q = q.filter(rattail.BatchTerminal.sil_id == sil_id)
if q.count() == 1:
return q.one()
request = threadlocal.get_current_request()
terminal = tags.link_to("Batch Terminal", request.route_url('batch_terminals'))
request.session.flash("Hm, I couldn't find a %s for %s." % (terminal, title))