Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion feature discussion: before_exec() Instance event

Received: by 10.100.253.1 with SMTP id a1mr69480ani.10.1331676667496;
        Tue, 13 Mar 2012 15:11:07 -0700 (PDT)
X-BeenThere: sqlalchemy-devel@googlegroups.com
Received: by 10.236.207.71 with SMTP id m47ls1096324yho.6.gmail; Tue, 13 Mar
 2012 15:11:07 -0700 (PDT)
Received: by 10.236.192.164 with SMTP id i24mr332449yhn.8.1331676667006;
        Tue, 13 Mar 2012 15:11:07 -0700 (PDT)
Received: by 10.236.192.164 with SMTP id i24mr332448yhn.8.1331676666986;
        Tue, 13 Mar 2012 15:11:06 -0700 (PDT)
Return-Path: <rsyr...@gmail.com>
Received: from mail-gy0-f179.google.com (mail-gy0-f179.google.com [209.85.160.179])
        by gmr-mx.google.com with ESMTPS id t62si1020138yhj.2.2012.03.13.15.11.06
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 13 Mar 2012 15:11:06 -0700 (PDT)
Received-SPF: pass (google.com: domain of rsyr...@gmail.com designates 209.85.160.179 as permitted sender) client-ip=209.85.160.179;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of rsyr...@gmail.com designates 209.85.160.179 as permitted sender) smtp.mail=rsyr...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-gy0-f179.google.com with SMTP id g19so1400416ghb.24
        for <sqlalchemy-devel@googlegroups.com>; Tue, 13 Mar 2012 15:11:06 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=message-id:date:from:user-agent:mime-version:to:subject
         :content-type;
        bh=N12qyllSkg1/hFrbMGOEN9yP96kyEyjPb1H+GREVKso=;
        b=YV1XrP3b1b3OWl3a1PgChOauXoz4bmWWP2iNGEAsHjVMfEex+Vzsn/i5VaUhDSmNH2
         x8KLbVEbk7674TTsJdzSYhISIhDpsByPG6PDa4WUhlsve4nbr84UoIOYdPyMX/ylXRjH
         P9oCb5qQ6IXkqBFmCrlU2LojI9lpEvrbFLBNk30xQS5tZi0byUljjGlvb5c3e3fHAatZ
         G/DPypdvw8Veb4z7bjIrreKvCYhTEJRo9ICX0YNlOLwso+YBvETACnCluu5TwZZxLM3P
         TKkgeNrOVr4Hvsn7V5gO4cbFdJn46yKMtdA5qsmomjEVVmnh9PJfKa7ZtjDYZGuKBQxs
         auXQ==
Received: by 10.236.170.134 with SMTP id p6mr210558yhl.81.1331676666871;
        Tue, 13 Mar 2012 15:11:06 -0700 (PDT)
Return-Path: <rsyr...@gmail.com>
Received: from [192.168.200.22] (74-128-244-206.dhcp.insightbb.com. [74.128.244.206])
        by mx.google.com with ESMTPS id v27sm4068950yhi.22.2012.03.13.15.11.05
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 13 Mar 2012 15:11:06 -0700 (PDT)
Message-ID: <4F5FC5BB.1060...@gmail.com>
Date: Tue, 13 Mar 2012 18:10:03 -0400
From: Randy Syring <rsyr...@gmail.com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
MIME-Version: 1.0
To: sqlalchemy-devel@googlegroups.com
Subject: feature discussion: before_exec() Instance event
Content-Type: multipart/alternative;
 boundary="------------020906080702030807040207"

This is a multi-part message in MIME format.
--------------020906080702030807040207
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

I'm wondering what the chances are of getting some kind of instance 
event that would send INSERT/UPDATE values back to it right before 
sess.exec() is called for an INSERT or UPDATE operation.  I'd imagine 
something like:

@event.listen_for(Car, 'before_exec'):
def validate_entity(car, values, type):
     assert type in ('insert', 'update')
     if 'active_flag' in values:
         values['active_flag'] = 1 if values['active_flag'] else 0
     if values['make'] not in ('Ford', 'Chevy'):
         raise ValueError('make not recognized')
     if 'mileage' not in values:
         values['mileage'] = 'unknown'
     return values

I currently have a validation library 
(https://bitbucket.org/rsyring/sqlalchemy-validation/) that uses the 
before_flush session event to try and do validation, but I run into 
problems with validating values that come from column defaults and other 
more "low level" places.

I realize there are different ways to do validation and there are 
arguments to be made against ever needing to validate a default value.  
However, I'd like to be able to do validation of an entity in as 
low-level of a place as possible so as to guarauntee catching as many 
exceptions as I can without relying on form libraries to do input 
validation on my model.  I want the model to be self-validating so that 
I can use it in a web application, GUI, or console app and not have to 
write validation code in three places.

If this is possible, I'm willing to work on providing a patch.  I just 
want to make sure its possible and would be considered for acceptance 
before I go down that route.

Thanks.

---------------------------------------------
Randy Syring
Development&  Executive Director
Level 12 Technologies  <https://www.lev12.com/>  (formerly Intelicom)
Direct: 502-276-0459
Office: 502-212-9913

Intelicom is now Level 12 Technologies,learn more about our name change  <https://www.lev12.com/our-history>.
Please update your address book with my new email address.

Principled People, Technology that Works


--------------020906080702030807040207
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    I'm wondering what the chances are of getting some kind of instance
    event that would send INSERT/UPDATE values back to it right before
    sess.exec() is called for an INSERT or UPDATE operation.  I'd
    imagine something like:<br>
    <br>
    <tt>@event.listen_for(Car, 'before_exec'):<br>
      def validate_entity(car, values, type):<br>
          assert type in ('insert', 'update')<br>
          if 'active_flag' in values:<br>
              values['active_flag'] = 1 if values['active_flag'] else 0<br>
          if values['make'] not in ('Ford', 'Chevy'):<br>
              raise ValueError('make not recognized')<br>
          if 'mileage' not in values:<br>
              values['mileage'] = 'unknown'<br>
          return values</tt><br>
    <br>
    I currently have a validation library
    (<a class="moz-txt-link-freetext" href="https://bitbucket.org/rsyring/sqlalchemy-validation/">https://bitbucket.org/rsyring/sqlalchemy-validation/</a>) that uses the
    before_flush session event to try and do validation, but I run into
    problems with validating values that come from column defaults and
    other more "low level" places.  <br>
    <br>
    I realize there are different ways to do validation and there are
    arguments to be made against ever needing to validate a default
    value.  However, I'd like to be able to do validation of an entity
    in as low-level of a place as possible so as to guarauntee catching
    as many exceptions as I can without relying on form libraries to do
    input validation on my model.  I want the model to be
    self-validating so that I can use it in a web application, GUI, or
    console app and not have to write validation code in three places.<br>
    <br>
    If this is possible, I'm willing to work on providing a patch.  I
    just want to make sure its possible and would be considered for
    acceptance before I go down that route.<br>
    <br>
    Thanks.<br>
    <div class="moz-signature">
      <pre>---------------------------------------------
Randy Syring
Development &amp; Executive Director
<a href="https://www.lev12.com/">Level 12 Technologies</a> (formerly Intelicom)
Direct: 502-276-0459
Office: 502-212-9913

Intelicom is now Level 12 Technologies, <a href="https://www.lev12.com/our-history">learn more about our name change</a>.
Please update your address book with my new email address.

Principled People, Technology that Works
</pre>
    </div>
  </body>
</html>

--------------020906080702030807040207--