tailbone/rattail/pyramid/forms.py

138 lines
4.4 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.forms`` -- Rattail Forms
"""
import formalchemy
# from formalchemy.fields import SelectFieldRenderer
import edbob
from edbob.pyramid import Session
from edbob.pyramid.forms import pretty_datetime
import rattail
__all__ = ['PriceFieldRenderer', 'RegularPriceFieldRenderer', 'UpcFieldRenderer']
class BatchIdFieldRenderer(formalchemy.FieldRenderer):
"""
Renders the :attr:`rattail.Batch.batch_id` field.
"""
def render_readonly(self, **kwargs):
value = self.raw_value
if value is None:
return ''
return '%08u' % int(value)
# class BatchTerminalFieldRenderer(SelectFieldRenderer):
# """
# Renders a field whose value is a relationship to a
# :class:`rattail.BatchTerminal` instance.
# """
# def render(self, options, **kwargs):
class PriceFieldRenderer(formalchemy.FieldRenderer):
"""
Renderer for fields which reference a :class:`ProductPrice` instance.
"""
def render_readonly(self, **kwargs):
price = self.field.raw_value
if price:
if price.price is not None and price.pack_price is not None:
if price.multiple > 1:
return '$ %0.2f / %u&nbsp; ($ %0.2f / %u)' % (
price.price, price.multiple,
price.pack_price, price.pack_multiple)
return '$ %0.2f&nbsp; ($ %0.2f / %u)' % (
price.price, price.pack_price, price.pack_multiple)
if price.price is not None:
if price.multiple > 1:
return '$ %0.2f / %u' % (price.price, price.multiple)
return '$ %0.2f' % price.price
if price.pack_price is not None:
return '$ %0.2f / %u' % (price.pack_price, price.pack_multiple)
return ''
class PriceWithExpirationFieldRenderer(PriceFieldRenderer):
"""
Price field renderer which also displays the expiration date, if present.
"""
def render_readonly(self, **kwargs):
res = super(PriceWithExpirationFieldRenderer, self).render_readonly(**kwargs)
if res:
price = self.field.raw_value
if price.ends:
res += '&nbsp; (%s)' % pretty_datetime(price.ends, from_='utc')
return res
class UpcFieldRenderer(formalchemy.TextFieldRenderer):
"""
Handles rendering for the product UPC field.
"""
def render_readonly(self, **kwargs):
value = self.raw_value
if not value:
return ''
if isinstance(value, basestring):
if value.isdigit():
value = int(value)
if isinstance(value, (int, long)):
return '%013u' % value
return self.stringify_value(value, as_html=True)
def unique_batch_terminal_id(value, field=None):
"""
.. highlight:: python
Validator for the :class:`rattail.BatchTerminal` class to ensure that SIL
IDs are not duplicated. For example::
from rattail.pyramid.forms import unique_batch_terminal_id
# fieldset = some_batch_terminal_fieldset_factory()
fieldset.sil_id.set(validate=unique_batch_terminal_id)
"""
if value:
q = Session.query(rattail.BatchTerminal)
q = q.filter(rattail.BatchTerminal.sil_id == value)
if field.parent.edit:
q = q.filter(rattail.BatchTerminal.uuid != field.parent.model.uuid)
if q.count():
raise formalchemy.ValidationError("SIL ID value must be unique within the system")