Le 12/07/2011 15:15, Damien Accorsi a écrit :
Le 12/07/2011 15:13, Alessandro Molina a écrit :
On Tue, Jul 12, 2011 at 2:52 PM, Damien Accorsi <[email protected]> wrote:
But... I'd like to decorrelate SQLA queries from my forms. What type of
object is to be sent
As I suggested on the previous mail inheriting from validators.OneOf
and overriding the list property should work

class CallableOneOf(validators.OneOf):
    @property
    def list(self):
        return self._list()

    @list.setter
    def list(self, callable):
        self._list = callable

this way you should be able to pass a callable to the validator.
Actually I have not tested the code itself, consider it more pseudocode ;)
Ok, I didn't understand what you were suggesting. It looks smart.
For those who'd be interested in the code for that dynamic validator, you'll find the code below.

Damien

---

class DynamicIntSelectFieldValidator(twfv.OneOf):
  messages = dict(
    invalid=('Invalid value'),
    notIn=('Please choose a value from the list.'))

  def _to_python(self, value, state):
    try:
      return int(value)
    except (ValueError, TypeError):
      raise Invalid(self.message('integer', state), value, state) # This is related to the int ids I manage with my select field

    _from_python = _to_python

  def __init__(self, *args, **kw):
    self._hDynamicListCallable = None # This attribute is the reference to the callable to be used for allowed values populate operation
    super(DynamicIntSelectFieldValidator, self ).__init__(*args, **kw)

  @property
  def list(self):
    return self._hDynamicListCallable()

  @list.setter
  def list(self, phDynamicListCallable):
    if hasattr(phDynamicListCallable, '__call__')==False:
      # We raise an exception here in order to be sure to pass callable and not lists
      # of values. This is due to the API I used which is something like getIdListBlablabla()
      # and I wanted to be sure to have updated the code (replacing all instances of
      # "getIdListBlablabla()" by "getIdListBlablabla"
      raise TypeError('%s is not callable [raised in IntSelectFieldValidator]'%phDynamicListCallable.__class__.__name__)
    self._hDynamicListCallable = phDynamicListCallable

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.

Reply via email to