[web2py] Computed field not computed at INSERT ?

2014-12-19 Thread Mirek Zvolský
Web2py book says
>> When a new record is modified, including both insertions and updates, if 
a value for the field is not provided, web2py tries to compute 

For me this works well after UPDATE from SQLFORM.
However, when I use

db.table.insert(sourcefieldforcomputed=value)

computed field remains None ??


What to do to get the expected behaviour?
Should I call the compute function explicitly? Or is it web2py bug? Or...?

Thanks for help. Mirek

Version 2.9.11-stable+timestamp.2014.09.15.23.35.11
Debian/Jessie
SQLite

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] computed field and reference

2013-05-27 Thread Matteo Luperto
Hi, I want to compute a field based on the value of another field, which is 
a foreign key, in a way similar to: 

db.define_table('language',
Field('language', 'string', required = True, unique = True, length = 64
),
format='%(language)s'
)

db.define_table('problemsHere',
Field('language', 'reference language', required = True, unique = False
),
Field('otherField', 'integer'),
Field('computedField', 
length=32,
unique=True,
writable=False,
readable=False,
compute=lambda row: db.language(row['language']).
language)
)
I've tried both syntaxes from this similar 
questionbut
 none of them seems to work for me. The computedField is always set to 
"None". 
Does anybody knows a way to access a foreign key in a computed field, 
solving my problem?

Thank you in advance,
~Matteo

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Computed field date difference as integer

2013-05-19 Thread Zsolt Szabó
I like to store the difference as integer not char(512)

db.define_table(
'mytable',
Field('begin_date', type='date', requires=IS_DATE(format='%Y.%m.%d.')),
Field('end_date', type='date', requires=IS_DATE(format='%Y.%m.%d.')),
Field('difference', compute=lambda r: r['end_date']-r['begin_date']))

begin_date =  2013.05.18
end_date=  2013.05.27

i wanna get only 
   difference: 9
  not: 9 days, 0:00:00
Anybody can help me?

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Computed field does not get updated when creating database entry

2012-08-22 Thread Daniel Gonzalez
Hi,

I have defined a database table which has some fields which must be 
computed:

agent_table = 'agent'
db.define_table(
agent_table,
Field('email',unique=True),
Field('agent_doc_id', length=128, compute=create_new_agent),
Field('password', 'password', length=512, 
compute=automatic_password,readable
=False, label='Password'),
format = '%(email)s %(agent_doc_id)s %(password)s')

# Add constraints to the agent table
agent = db[agent_table]
agent.email.requires = 
[IS_EMAIL(error_message=auth.messages.invalid_email), IS_NOT_IN_DB(db, 
'%s.email' % (agent_table))]

The functions create_new_agent and automatic_password are part of my 
libraries. They are returning the expected types (strings in both cases)

I am rendering the form with:

def register_agent():
# create an insert form from the table
form = SQLFORM(db[agent_table])

# Pre-populate the email with the input in the signup page
form.vars.email = request.vars.email

# if form is correct perform the insert
if form.process().accepted:
response.flash = 'Agent created'
else:
response.flash = 'Please fill in the form'

# and get a list of all agents
records = SQLTABLE(db().select(db.agent.ALL),headers=
'fieldname:capitalize')

return dict(form=form, records=records)

I was assuming that form.process() will take care of updating the fields 
which have a compute parameter, and then insert the resulting data in the 
database.
But this is not what is happening: the 'compute' fields are empty.

How can I force the 'compute' fields to be updated?

Thanks,
Daniel

-- 





Re: [web2py] computed field

2012-04-20 Thread Richard Vézina
http://web2py.com/books/default/chapter/29/6

{{extend 'layout.html'}}
Records
{{=SQLTABLE(rows,
 headers='fieldname:capitalize',
 truncate=100,
 upload=URL('download'))
}}


Richard

On Fri, Apr 20, 2012 at 11:42 AM, Richard Vézina <
ml.richard.vez...@gmail.com> wrote:

> Also, you could maybe search the book with "truncate" keyword, there is
> already a utility if you just want to control the lenght of free text field
> or other string field.
>
> Richard
>
>
> On Fri, Apr 20, 2012 at 11:35 AM, Richard Vézina <
> ml.richard.vez...@gmail.com> wrote:
>
>> I think you can't use compute since you modify a field where the user as
>> to enter something base on the len of user input, so I think you should
>> transform the code below into function and use onvalidation=function
>>
>>
>> x=len(form.vars.description)
>> if x>128:
>> i=128
>> dots="..."
>> else:
>> i=x-1
>> dots=""
>> s=form.vars.description[0:i] + dots
>>
>> Richard
>>
>>
>>
>>
>> On Fri, Apr 20, 2012 at 11:26 AM, Annet  wrote:
>>
>>> I defined a table eventList:
>>>
>>> db.define_table('EventList',
>>>...
>>> Field('description',type='text'),
>>> Field('shortdescr',length=128,writable=False,readable=False),
>>> 
>>> migrate=False)
>>>
>>> db.EventList.description.requires=IS_LENGTH(1536,error_message='length
>>> exceeds 1536 characters')
>>> db.EventList.shortdescr.requires=IS_LENGTH(128,error_message='length
>>> exceeds 128 characters')
>>>
>>> shortdescr should be the first 128 characters of description followed by
>>> three dots. In a function this works:
>>>
>>> x=len(form.vars.description)
>>> if x>128:
>>> i=128
>>> dots="..."
>>> else:
>>> i=x-1
>>> dots=""
>>> s=form.vars.description[0:i] + dots
>>>
>>> ... but I'd like to add it to the model file. I had a look at the
>>> compute option but wasn't able to figure out how to get it syntactically
>>> correct.
>>>
>>>
>>> Kind regards,
>>>
>>> Annet.
>>>
>>
>>
>


Re: [web2py] computed field

2012-04-20 Thread Richard Vézina
Also, you could maybe search the book with "truncate" keyword, there is
already a utility if you just want to control the lenght of free text field
or other string field.

Richard

On Fri, Apr 20, 2012 at 11:35 AM, Richard Vézina <
ml.richard.vez...@gmail.com> wrote:

> I think you can't use compute since you modify a field where the user as
> to enter something base on the len of user input, so I think you should
> transform the code below into function and use onvalidation=function
>
>
> x=len(form.vars.description)
> if x>128:
> i=128
> dots="..."
> else:
> i=x-1
> dots=""
> s=form.vars.description[0:i] + dots
>
> Richard
>
>
>
>
> On Fri, Apr 20, 2012 at 11:26 AM, Annet  wrote:
>
>> I defined a table eventList:
>>
>> db.define_table('EventList',
>>...
>> Field('description',type='text'),
>> Field('shortdescr',length=128,writable=False,readable=False),
>> 
>> migrate=False)
>>
>> db.EventList.description.requires=IS_LENGTH(1536,error_message='length
>> exceeds 1536 characters')
>> db.EventList.shortdescr.requires=IS_LENGTH(128,error_message='length
>> exceeds 128 characters')
>>
>> shortdescr should be the first 128 characters of description followed by
>> three dots. In a function this works:
>>
>> x=len(form.vars.description)
>> if x>128:
>> i=128
>> dots="..."
>> else:
>> i=x-1
>> dots=""
>> s=form.vars.description[0:i] + dots
>>
>> ... but I'd like to add it to the model file. I had a look at the compute
>> option but wasn't able to figure out how to get it syntactically correct.
>>
>>
>> Kind regards,
>>
>> Annet.
>>
>
>


Re: [web2py] computed field

2012-04-20 Thread Richard Vézina
I think you can't use compute since you modify a field where the user as to
enter something base on the len of user input, so I think you should
transform the code below into function and use onvalidation=function

x=len(form.vars.description)
if x>128:
i=128
dots="..."
else:
i=x-1
dots=""
s=form.vars.description[0:i] + dots

Richard




On Fri, Apr 20, 2012 at 11:26 AM, Annet  wrote:

> I defined a table eventList:
>
> db.define_table('EventList',
>...
> Field('description',type='text'),
> Field('shortdescr',length=128,writable=False,readable=False),
> 
> migrate=False)
>
> db.EventList.description.requires=IS_LENGTH(1536,error_message='length
> exceeds 1536 characters')
> db.EventList.shortdescr.requires=IS_LENGTH(128,error_message='length
> exceeds 128 characters')
>
> shortdescr should be the first 128 characters of description followed by
> three dots. In a function this works:
>
> x=len(form.vars.description)
> if x>128:
> i=128
> dots="..."
> else:
> i=x-1
> dots=""
> s=form.vars.description[0:i] + dots
>
> ... but I'd like to add it to the model file. I had a look at the compute
> option but wasn't able to figure out how to get it syntactically correct.
>
>
> Kind regards,
>
> Annet.
>


[web2py] computed field

2012-04-20 Thread Annet
I defined a table eventList:

db.define_table('EventList',
   ...
Field('description',type='text'),
Field('shortdescr',length=128,writable=False,readable=False),

migrate=False)

db.EventList.description.requires=IS_LENGTH(1536,error_message='length 
exceeds 1536 characters')
db.EventList.shortdescr.requires=IS_LENGTH(128,error_message='length 
exceeds 128 characters')

shortdescr should be the first 128 characters of description followed by 
three dots. In a function this works:

x=len(form.vars.description)
if x>128:
i=128
dots="..."
else:
i=x-1
dots=""
s=form.vars.description[0:i] + dots

... but I'd like to add it to the model file. I had a look at the compute 
option but wasn't able to figure out how to get it syntactically correct.


Kind regards,

Annet.


[web2py] computed field question

2012-04-04 Thread Richard
Hello,

I notice that in the book the example shown don't define a field type for 
compute. In that case web2py default seems to create a char var (512) type 
field. I had no problem with this for storing the computation of decimal 
result. It also works well if I define a proper field type, in my case 
decimal... But I would like to know what the best pratice here... Should I 
set field type or sotre result as string, is it only an oversight when 
writing the book?

Thanks

Richard


Re: [web2py] Computed field not working

2012-02-29 Thread Tito Garrido
Thanks a lot Richard! you were right! I have put the wrong field on the
calculation! You are the best!

Thank you!

On Wed, Feb 29, 2012 at 6:25 PM, Richard Vézina  wrote:

> You valor_do_dolar field is still a problem in the model down here...
> Since you can't or your lambda can't accès valor_do_dolar in that example.
> The field don't exist in the form so the key not exist so web2py return
> none and custo_em_dolar*none will return none I guest.
>
> try this :
>
> db.define_table('compra_produto',
> Field('compra', 'reference compra'),
> Field('produto','reference produto'),
> Field('quantidade','integer'),
> Field('custo_em_dolar','double'),
> Field('cotacao_do_dolar','double'),
> Field('custo_em_real', compute=lambda cr: cr['custo_em_dolar']*cr['*
> cotacao_do_dolar*']),
> )
>
> And provide input data for both field and it should works, not like you
> want it to works, but you will make sure that it could works.
>
> Also, notice that if you set a field to writable and/or readable = False,
> they will not be in the form and not provide any input data to your lambda
> so it will failed.
>
> I don't see what you do in your controller here that why I notice you
> about that. It is a common pit fall into which I run often when set new
> computed field on existing table because I kind of filter the field I show
> to user with writable and readable = False often and forget about those
> rules.
>
> :)
>
> Richard
>
>
> On Wed, Feb 29, 2012 at 4:16 PM, Tito Garrido wrote:
>
>> Nice Catch, but I have commented the last field to apply what you
>> suggested and this also doesn't work:
>>
>> db.define_table('compra_produto',
>> Field('compra', 'reference compra'),
>> Field('produto','reference produto'),
>> Field('quantidade','integer'),
>> Field('custo_em_dolar','double'),
>> Field('cotacao_do_dolar','double'),
>> Field('custo_em_real', compute=lambda cr:
>> cr['custo_em_dolar']*cr['valor_do_dolar']),
>>  )
>>
>>
>> On Wed, Feb 29, 2012 at 6:06 PM, Richard Vézina <
>> ml.richard.vez...@gmail.com> wrote:
>>
>>> You can't access valor_do_dolar from custo_em_real lambda computed since
>>> it is a computed field that get computed un submit... If you want to do
>>> that you will have to make a real function that get process on submit with
>>> form.process().accepted and the argument onsuccess or onvalidation
>>>
>>> See chapter 7 with both keyword here a link for the first keyword :
>>>
>>> http://web2py.com/books/default/chapter/29/7?search=onsuccess
>>>
>>> Richard
>>>
>>> On Wed, Feb 29, 2012 at 3:51 PM, Tito Garrido wrote:
>>>
 Hi Folks,

 I am trying a simple example using sqlite:

 db.define_table('compra_produto',
 Field('compra', 'reference compra'),
 Field('produto','reference produto'),
 Field('quantidade','integer'),
 Field('custo_em_dolar','double'),
 Field('cotacao_do_dolar','double'),
 Field('custo_em_real', compute=lambda cr:
 cr['custo_em_dolar']*cr['valor_do_dolar']),
 Field('valor_total', compute=lambda vt:
 vt['custo_em_real']*vt['quantidade'])

 But both computed fields are always set as "None"... I am trying it
 from administrative database screen...

 Am I doing anything wrong?

 --

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___

>>>
>>>
>>
>>
>> --
>>
>> Linux User #387870
>> .
>>  _/_õ|__|
>> ..º[ .-.___.-._| . . . .
>> .__( o)__( o).:___
>>
>
>


-- 

Linux User #387870
.
 _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:___


Re: [web2py] Computed field not working

2012-02-29 Thread Richard Vézina
You valor_do_dolar field is still a problem in the model down here... Since
you can't or your lambda can't accès valor_do_dolar in that example. The
field don't exist in the form so the key not exist so web2py return none
and custo_em_dolar*none will return none I guest.

try this :

db.define_table('compra_produto',
Field('compra', 'reference compra'),
Field('produto','reference produto'),
Field('quantidade','integer'),
Field('custo_em_dolar','double'),
Field('cotacao_do_dolar','double'),
Field('custo_em_real', compute=lambda cr: cr['custo_em_dolar']*cr['*
cotacao_do_dolar*']),
)

And provide input data for both field and it should works, not like you
want it to works, but you will make sure that it could works.

Also, notice that if you set a field to writable and/or readable = False,
they will not be in the form and not provide any input data to your lambda
so it will failed.

I don't see what you do in your controller here that why I notice you about
that. It is a common pit fall into which I run often when set new computed
field on existing table because I kind of filter the field I show to user
with writable and readable = False often and forget about those rules.

:)

Richard

On Wed, Feb 29, 2012 at 4:16 PM, Tito Garrido  wrote:

> Nice Catch, but I have commented the last field to apply what you
> suggested and this also doesn't work:
>
> db.define_table('compra_produto',
> Field('compra', 'reference compra'),
> Field('produto','reference produto'),
> Field('quantidade','integer'),
> Field('custo_em_dolar','double'),
> Field('cotacao_do_dolar','double'),
> Field('custo_em_real', compute=lambda cr:
> cr['custo_em_dolar']*cr['valor_do_dolar']),
> )
>
>
> On Wed, Feb 29, 2012 at 6:06 PM, Richard Vézina <
> ml.richard.vez...@gmail.com> wrote:
>
>> You can't access valor_do_dolar from custo_em_real lambda computed since
>> it is a computed field that get computed un submit... If you want to do
>> that you will have to make a real function that get process on submit with
>> form.process().accepted and the argument onsuccess or onvalidation
>>
>> See chapter 7 with both keyword here a link for the first keyword :
>>
>> http://web2py.com/books/default/chapter/29/7?search=onsuccess
>>
>> Richard
>>
>> On Wed, Feb 29, 2012 at 3:51 PM, Tito Garrido wrote:
>>
>>> Hi Folks,
>>>
>>> I am trying a simple example using sqlite:
>>>
>>> db.define_table('compra_produto',
>>> Field('compra', 'reference compra'),
>>> Field('produto','reference produto'),
>>> Field('quantidade','integer'),
>>> Field('custo_em_dolar','double'),
>>> Field('cotacao_do_dolar','double'),
>>> Field('custo_em_real', compute=lambda cr:
>>> cr['custo_em_dolar']*cr['valor_do_dolar']),
>>> Field('valor_total', compute=lambda vt:
>>> vt['custo_em_real']*vt['quantidade'])
>>>
>>> But both computed fields are always set as "None"... I am trying it from
>>> administrative database screen...
>>>
>>> Am I doing anything wrong?
>>>
>>> --
>>>
>>> Linux User #387870
>>> .
>>>  _/_õ|__|
>>> ..º[ .-.___.-._| . . . .
>>> .__( o)__( o).:___
>>>
>>
>>
>
>
> --
>
> Linux User #387870
> .
>  _/_õ|__|
> ..º[ .-.___.-._| . . . .
> .__( o)__( o).:___
>


Re: [web2py] Computed field not working

2012-02-29 Thread Tito Garrido
Nice Catch, but I have commented the last field to apply what you suggested
and this also doesn't work:
db.define_table('compra_produto',
Field('compra', 'reference compra'),
Field('produto','reference produto'),
Field('quantidade','integer'),
Field('custo_em_dolar','double'),
Field('cotacao_do_dolar','double'),
Field('custo_em_real', compute=lambda cr:
cr['custo_em_dolar']*cr['valor_do_dolar']),
)

On Wed, Feb 29, 2012 at 6:06 PM, Richard Vézina  wrote:

> You can't access valor_do_dolar from custo_em_real lambda computed since
> it is a computed field that get computed un submit... If you want to do
> that you will have to make a real function that get process on submit with
> form.process().accepted and the argument onsuccess or onvalidation
>
> See chapter 7 with both keyword here a link for the first keyword :
>
> http://web2py.com/books/default/chapter/29/7?search=onsuccess
>
> Richard
>
> On Wed, Feb 29, 2012 at 3:51 PM, Tito Garrido wrote:
>
>> Hi Folks,
>>
>> I am trying a simple example using sqlite:
>>
>> db.define_table('compra_produto',
>> Field('compra', 'reference compra'),
>> Field('produto','reference produto'),
>> Field('quantidade','integer'),
>> Field('custo_em_dolar','double'),
>> Field('cotacao_do_dolar','double'),
>> Field('custo_em_real', compute=lambda cr:
>> cr['custo_em_dolar']*cr['valor_do_dolar']),
>> Field('valor_total', compute=lambda vt:
>> vt['custo_em_real']*vt['quantidade'])
>>
>> But both computed fields are always set as "None"... I am trying it from
>> administrative database screen...
>>
>> Am I doing anything wrong?
>>
>> --
>>
>> Linux User #387870
>> .
>>  _/_õ|__|
>> ..º[ .-.___.-._| . . . .
>> .__( o)__( o).:___
>>
>
>


-- 

Linux User #387870
.
 _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:___


Re: [web2py] Computed field not working

2012-02-29 Thread Richard Vézina
You can't access valor_do_dolar from custo_em_real lambda computed since it
is a computed field that get computed un submit... If you want to do that
you will have to make a real function that get process on submit with
form.process().accepted and the argument onsuccess or onvalidation

See chapter 7 with both keyword here a link for the first keyword :

http://web2py.com/books/default/chapter/29/7?search=onsuccess

Richard

On Wed, Feb 29, 2012 at 3:51 PM, Tito Garrido  wrote:

> Hi Folks,
>
> I am trying a simple example using sqlite:
>
> db.define_table('compra_produto',
> Field('compra', 'reference compra'),
> Field('produto','reference produto'),
> Field('quantidade','integer'),
> Field('custo_em_dolar','double'),
> Field('cotacao_do_dolar','double'),
> Field('custo_em_real', compute=lambda cr:
> cr['custo_em_dolar']*cr['valor_do_dolar']),
> Field('valor_total', compute=lambda vt:
> vt['custo_em_real']*vt['quantidade'])
>
> But both computed fields are always set as "None"... I am trying it from
> administrative database screen...
>
> Am I doing anything wrong?
>
> --
>
> Linux User #387870
> .
>  _/_õ|__|
> ..º[ .-.___.-._| . . . .
> .__( o)__( o).:___
>


Re: [web2py] Computed field

2011-12-21 Thread Jim Steil

Here's what I would try:

Field('difference', compute=lambda u: (u['DT_FECHAMENTO '] - 
u['DT_ABERTURA ']), label='Difference')


This is untested.  I haven't used compute fields based on a math 
function before, but like I said, this is what I'd try first.


Hope this helps.

-Jim

On 12/21/2011 1:43 PM, Jose Carlos Junior wrote:

People,


   How can I make the field NR_INDISPONIBILIDADE the table below
(chamado) in a computed field with the difference between the field
dates DT_FECHAMENTO and DT_ABERTURA.
 Thank you,

db.define_table("chamados"
 ,Field('ID_CHAMADOS',type='id',readable=False)
 ,Field('ID_TECNICOS',type='integer',notnull=True,label='Técnico')
 ,Field('ID_CIRCUITOS',type='integer',notnull=True,label='Circuito')
 ,Field('NR_CHAMADO',type='string',notnull=False,label='Chamado')
 ,Field('DT_ABERTURA',type='datetime',notnull=True,label='Abertura')
 ,Field('DT_FECHAMENTO',type='datetime',notnull=False,label='Fechamento')
 
,Field('NR_INDISPONIBILIDADE',type='double',notnull=False,label='Indisponibilidade')
 ,Field('FL_OFENSOR',type='boolean',label='Ofensor',default=True)
 ,migrate=False
)


[web2py] Computed field

2011-12-21 Thread Jose Carlos Junior
People,


  How can I make the field NR_INDISPONIBILIDADE the table below
(chamado) in a computed field with the difference between the field
dates DT_FECHAMENTO and DT_ABERTURA.
Thank you,

db.define_table("chamados"
,Field('ID_CHAMADOS',type='id',readable=False)
,Field('ID_TECNICOS',type='integer',notnull=True,label='Técnico')
,Field('ID_CIRCUITOS',type='integer',notnull=True,label='Circuito')
,Field('NR_CHAMADO',type='string',notnull=False,label='Chamado')
,Field('DT_ABERTURA',type='datetime',notnull=True,label='Abertura')
,Field('DT_FECHAMENTO',type='datetime',notnull=False,label='Fechamento')

,Field('NR_INDISPONIBILIDADE',type='double',notnull=False,label='Indisponibilidade')
,Field('FL_OFENSOR',type='boolean',label='Ofensor',default=True)
,migrate=False
)


[web2py] computed field or is_in_db(query...

2011-12-19 Thread Nik Go
commas at the beginning of a line is not a common practice, but not
necessarily a bad one.  I use it myself, having been bitten by too many
missing commas too often. ;)

On Tuesday, December 20, 2011, Richard Vézina wrote:

> I have difficulty to understand your models because they are not in
> english (sorry about that)... I will try to help anyway...
>
> By the way you should put "," at the end of your lines like this :
>
> Field('CD_CIDADAO',type='**string',notnull=True,label='**Matrícula')*,*
>
> instead of :
>
> *,*Field('CD_CIDADAO',type='**string',notnull=True,label='**Matrícula')
>
> Because python will not understand, maybe web2py does, but it is not a
> good pratice...
>
> You use format =... But you don't use web2py fk fearture so format= can't
> manage your reprensentation...
>
> For example :
>
> representante table field : CD_CIDADAO field definition should be :
>
>
> Field('CD_CIDADAO',db.cidadao)
>
>  So CD_CIDADAO will be of type integer and store the id of the cidadao
> table... That way web2py should generate a dropbox and use "format="
> of cidadao table to  avoid displaying the id of the field that has no
> meaning for end user... Then you will have to use represente=
> for CD_CIDADAO field to make sure that on retreiving of the result you not
> having the id instead of what you want the user to see...
>
>
> db.representante.CD_CIDADAO.represent=lambda id, row:
> db.cidadao(id).NM_CIDADAO
>
>
> Hope it helps
>
> Richard
>
>
> On Mon, Dec 19, 2011 at 10:10 AM, Jose Carlos Junior 
> wrote:
>
>> Please...I have some dificult in do this in web2py...as follow
>>
>> db.define_table("cidadao"
>> ,Field('CD_CIDADAO',type='id',
>> **label='Cidadão(PK)')
>> ,Field('CD_MATRICULA',type='**integer',notnull=True,label='**
>> Matrícula')
>> ,Field('NM_CIDADAO',type='**string',notnull=True,label='**Nome')
>> ,format = '%(NM_CIDADAO)s'
>> ,singular = 'cidadao'
>> ,plural = 'cidadaos'
>> ,migrate=False)
>>
>>
>> db.define_table("**representante"
>> ,Field('ID_REPRESENTANTE',**type='id',readable=False)
>> ,Field('ID_CARGOS',type='**integer',notnull=True,label='**Cargo')
>> ,Field('CD_CIDADAO',type='**string',notnull=True,label='**Matrícula')
>> ,Field('DT_INCLUSAO',type='**datetime',notnull=True,label='**
>> Inclusão')
>> ,Field('DT_EXCLUSAO',type='**datetime',notnull=False,label=**
>> 'Exclusão')
>> ,Field('CD_TELEFONE',type='**string',notnull=True,label='**Telefone')
>> ,Field('CD_EMAIL',type='**string',notnull=True,label='**Email')
>> ,Field('CD_CELULAR',type='**string',notnull=False,label='**Celular')
>> ,migrate=False)
>>
>> db.representante.CD_CIDADAO.**requires = IS_IN_DB(db,
>> 'cidadao.CD_CIDADAO', '%(CD_MATRICULA)s - %(NM_CIDADAO)s')
>> db.representante.ID_CARGOS.**requires = IS_IN_DB(db, 'cargos.ID_CARGOS',
>> '%(NOME)s')
>>
>> db.define_table("gtils_**representante"
>> ,Field('ID',type='id',**readable=False)
>> ,Field('ID_GTIL',readable=**True,label='GTIL')
>> ,Field('ID_REPRESENTANTE',**readable=True,label='**Representante')
>> ,Field('DT_INICIO',type='**datetime',notnull=True,label='**Início')
>> ,Field('DT_TERMINO',type='**datetime',notnull=False,label=**
>> 'Término')
>> ,migrate=False)
>>
>> db.gtils_representante.ID_**GTIL.requires = IS_IN_DB(db,
>> 'gtils.ID_GTIL', '%(NOME)s')
>>
>> How can i fill  ID_REPRESENTANTE but show NM_CIDADAO (table cidadao) once
>> i don't have this field in "representante" table...??  or how can i put a
>> computed field in 'representant' table filled with the result  choice of
>> the CD_CIDADAO. Thanks alot
>>
>
>


Re: [web2py] computed field or is_in_db(query...

2011-12-19 Thread Richard Vézina
I have difficulty to understand your models because they are not in english
(sorry about that)... I will try to help anyway...

By the way you should put "," at the end of your lines like this :

Field('CD_CIDADAO',type='**string',notnull=True,label='**Matrícula')*,*

instead of :

*,*Field('CD_CIDADAO',type='**string',notnull=True,label='**Matrícula')

Because python will not understand, maybe web2py does, but it is not a good
pratice...

You use format =... But you don't use web2py fk fearture so format= can't
manage your reprensentation...

For example :

representante table field : CD_CIDADAO field definition should be :


Field('CD_CIDADAO',db.cidadao)

So CD_CIDADAO will be of type integer and store the id of the cidadao
table... That way web2py should generate a dropbox and use "format="
of cidadao table to  avoid displaying the id of the field that has no
meaning for end user... Then you will have to use represente=
for CD_CIDADAO field to make sure that on retreiving of the result you not
having the id instead of what you want the user to see...


db.representante.CD_CIDADAO.represent=lambda id, row:
db.cidadao(id).NM_CIDADAO


Hope it helps

Richard


On Mon, Dec 19, 2011 at 10:10 AM, Jose Carlos Junior wrote:

> Please...I have some dificult in do this in web2py...as follow
>
> db.define_table("cidadao"
> ,Field('CD_CIDADAO',type='id',
> **label='Cidadão(PK)')
> ,Field('CD_MATRICULA',type='**integer',notnull=True,label='**
> Matrícula')
> ,Field('NM_CIDADAO',type='**string',notnull=True,label='**Nome')
> ,format = '%(NM_CIDADAO)s'
> ,singular = 'cidadao'
> ,plural = 'cidadaos'
> ,migrate=False)
>
>
> db.define_table("**representante"
> ,Field('ID_REPRESENTANTE',**type='id',readable=False)
> ,Field('ID_CARGOS',type='**integer',notnull=True,label='**Cargo')
> ,Field('CD_CIDADAO',type='**string',notnull=True,label='**Matrícula')
> ,Field('DT_INCLUSAO',type='**datetime',notnull=True,label='**
> Inclusão')
> ,Field('DT_EXCLUSAO',type='**datetime',notnull=False,label=**
> 'Exclusão')
> ,Field('CD_TELEFONE',type='**string',notnull=True,label='**Telefone')
> ,Field('CD_EMAIL',type='**string',notnull=True,label='**Email')
> ,Field('CD_CELULAR',type='**string',notnull=False,label='**Celular')
> ,migrate=False)
>
> db.representante.CD_CIDADAO.**requires = IS_IN_DB(db,
> 'cidadao.CD_CIDADAO', '%(CD_MATRICULA)s - %(NM_CIDADAO)s')
> db.representante.ID_CARGOS.**requires = IS_IN_DB(db, 'cargos.ID_CARGOS',
> '%(NOME)s')
>
> db.define_table("gtils_**representante"
> ,Field('ID',type='id',**readable=False)
> ,Field('ID_GTIL',readable=**True,label='GTIL')
> ,Field('ID_REPRESENTANTE',**readable=True,label='**Representante')
> ,Field('DT_INICIO',type='**datetime',notnull=True,label='**Início')
> ,Field('DT_TERMINO',type='**datetime',notnull=False,label=**'Término')
> ,migrate=False)
>
> db.gtils_representante.ID_**GTIL.requires = IS_IN_DB(db, 'gtils.ID_GTIL',
> '%(NOME)s')
>
> How can i fill  ID_REPRESENTANTE but show NM_CIDADAO (table cidadao) once
> i don't have this field in "representante" table...??  or how can i put a
> computed field in 'representant' table filled with the result  choice of
> the CD_CIDADAO. Thanks alot
>


[web2py] computed field or is_in_db(query...

2011-12-19 Thread Jose Carlos Junior
Please...I have some dificult in do this in web2py...as follow

db.define_table("cidadao" 
,Field('CD_CIDADAO',type='id',
label='Cidadão(PK)')
,Field('CD_MATRICULA',type='integer',notnull=True,label='Matrícula')
,Field('NM_CIDADAO',type='string',notnull=True,label='Nome')
,format = '%(NM_CIDADAO)s'
,singular = 'cidadao'
,plural = 'cidadaos'
,migrate=False)


db.define_table("representante" 
,Field('ID_REPRESENTANTE',type='id',readable=False)
,Field('ID_CARGOS',type='integer',notnull=True,label='Cargo')
,Field('CD_CIDADAO',type='string',notnull=True,label='Matrícula')
,Field('DT_INCLUSAO',type='datetime',notnull=True,label='Inclusão')
,Field('DT_EXCLUSAO',type='datetime',notnull=False,label='Exclusão')
,Field('CD_TELEFONE',type='string',notnull=True,label='Telefone')
,Field('CD_EMAIL',type='string',notnull=True,label='Email')
,Field('CD_CELULAR',type='string',notnull=False,label='Celular')
,migrate=False)

db.representante.CD_CIDADAO.requires = IS_IN_DB(db, 'cidadao.CD_CIDADAO', 
'%(CD_MATRICULA)s - %(NM_CIDADAO)s') 
db.representante.ID_CARGOS.requires = IS_IN_DB(db, 'cargos.ID_CARGOS', 
'%(NOME)s') 

db.define_table("gtils_representante" 
,Field('ID',type='id',readable=False)
,Field('ID_GTIL',readable=True,label='GTIL')
,Field('ID_REPRESENTANTE',readable=True,label='Representante')
,Field('DT_INICIO',type='datetime',notnull=True,label='Início')
,Field('DT_TERMINO',type='datetime',notnull=False,label='Término')
,migrate=False)

db.gtils_representante.ID_GTIL.requires = IS_IN_DB(db, 'gtils.ID_GTIL', 
'%(NOME)s')

How can i fill  ID_REPRESENTANTE but show NM_CIDADAO (table cidadao) once i 
don't have this field in "representante" table...??  or how can i put a 
computed field in 'representant' table filled with the result  choice of 
the CD_CIDADAO. Thanks alot


[web2py] computed field: update and images

2011-11-28 Thread Pepe Araya
Hi!!

I have 3 computed fields that call a "resize_image" function to make 3 
versions of a uploaded image.
The definition of one of these fields:

Field('foto_portada_detalle', 'upload',
uploadfolder=request.folder+'static/uploads/actividades',
compute=lambda r: resize_image(r['foto_portada'], (200,200), 'detalle', 
'actividades'),
autodelete=True,
readable=False)


When I delete the record, all is ok and the images are deleted but when I 
update the record with another image one good thing occur: all the other 3 
versions get updated BUT one bad thing occur too: the old images stay on 
the disk although "autodelete=True"

I think that the resize_image function need to manage the delete of old 
images but I don't know how. can you help me? Thank a lot!

The function:

def resize_image(image, size, path, folder, rotate=0):
import os.path

from PIL import Image, ImageOps

if image:
try:
img = Image.open('%sstatic/uploads/%s/%s' % (request.folder, 
folder, image))
img = img.convert("RGB")
img = ImageOps.fit(img, size, Image.ANTIALIAS, centering=(0.5, 
0.5))
#img.resize(size, Image.ANTIALIAS)
img = img.rotate(rotate)
root, ext = os.path.splitext(image)
filename = '%s_%s%s' %(root, path, ext)
img.save('%sstatic/uploads/%s/%s' % (request.folder, folder, 
filename))
return filename
except Exception, e:
return e
else:
return None


[web2py] computed field won't hide when select...

2011-07-11 Thread Richard Vézina
Hello,

I have this code in a select function that work in command line to display
or not a computed field, so I can't figure out why it's not working in
app...


The computed field in red should not appear in grid view...

CONTROLLER :
elif auth.has_membership(auth.id_group('admin')):
fieldsBlackList=[db[request.args(0)].seizure.name,
db[request.args(0)].seizure_date.name,
db[request.args(0)].last_action_by.name,
db[request.args(0)].sdate.name,
db[request.args(0)].merged_for_validation.name] # Ex.:
db[request.args(0)].id.name
field2Show=[ request.args(0)+'.%s'%f for f \
in db[request.args(0)].fields if f not in fieldsBlackList ]
rows=db().select(*field2Show)
table=SQLTABLE(rows,headers=headersRepresent,_class='sortable',
truncate=None)

Any idea?


Richard