cd34 schrieb:
> While writing a quick TG2 app, I ran into an error while doing a
> redirect:
>
> KeyError: 'ticket_id'
>
> The code in question is:
>
> @expose('cp.templates.template')
> def ticketupdate(self, **kw):
> ticket_id = kw['ticket_id']
> provider.add('cp_ticket_detail', values=kw)
> provider.edit('cp_ticket',values=kw)
> redirect('/tech/ticket/%d' % kw['ticket_id']);
>
> both the add and edit run properly, however, if I alter the code to:
>
> @expose('cp.templates.template')
> def ticketupdate(self, **kw):
> ticket_id = kw['ticket_id']
> provider.add('cp_ticket_detail', values=kw)
> print kw
> provider.edit('cp_ticket',values=kw)
> print kw
> redirect('/tech/ticket/%d' % kw['ticket_id']);
>
> I get the following in the debug log:
>
> 10:52:32,237 INFO [sqlalchemy.engine.base.Engine.0x...b28c] INSERT
> INTO cp_ticket_detail (ticket_id, detail) VALUES (%s, %s)
> 10:52:32,238 INFO [sqlalchemy.engine.base.Engine.0x...b28c] [u'4008',
> u'asdfasfasf']
> 10:52:32,239 INFO [sqlalchemy.engine.base.Engine.0x...b28c] COMMIT
> {'status': u'P', 'detail': u'asdfasfasf', 'submit': u'UPDATE TICKET
> INFO', 'ticket_id': u'4008'}
> 10:52:32,240 INFO [sqlalchemy.engine.base.Engine.0x...b28c] UPDATE
> cp_ticket SET status=%s WHERE cp_ticket.ticket_id = %s
> 10:52:32,240 INFO [sqlalchemy.engine.base.Engine.0x...b28c] [u'P',
> u'4008']
> 10:52:32,241 INFO [sqlalchemy.engine.base.Engine.0x...b28c] COMMIT
> {'status': u'P', 'detail': u'asdfasfasf', 'submit': u'UPDATE TICKET
> INFO'}
>
> If we look at the contents of kw prior to the update, ticket_id is
> present. After the update, ticket_id is gone.
>
> ticket_id is a primary key on cp_ticket and it does properly update
> the ticket.
>
> I've worked around it by saving kw['ticket_id'] ahead of time, but, is
> this a bug or intended behavior?
I'd say it's a bug. A function/method shouldn't mess with passed
mutables unless that's somehow documented or justified.
I presume all that happens is something like this:
id = values.pop("id", None)
if id is not None:
...
which is of course very convenient to write - but modifies the passed
object.
As a workaround, I suggest using copy.copy(kw) when passing the values.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---