tailbone/rattail/pyramid/forms.py
2012-04-10 12:39:30 -05:00

99 lines
2.9 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
from edbob.pyramid import Session
import rattail
__all__ = ['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 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):
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")