Discussion:
[nagare-users:611] updating rows obtained via database.session.query()
Terrence Brannon
2015-05-12 20:46:43 UTC
Permalink
The NewContent.data() method retrieves database rows. The render
method for this class creates a form out of each row. When the form is
submitted the user can input a new value for the Content field of the
database row.

In ConfigureContent.answer() I attempt to update the database
row. However even though the database row is updated in memory, (1)
database.session does not know that the session is dirty and should be
commited (2) the database does not get updated.

How can I alter this code to update the database? I'm aware that I
could query the database and update, but that seems absolutely
wasteful as I already have the object here.

class NewContent(object):

def __init__(self):
pass

def data(self):
return database.session.query(
models.OBFL_Log_ParsingErrors).filter(
models.OBFL_Log_ParsingErrors.Type_Of_Record == 'Error')


def config(self, comp, dbrow):
print "Calling config"
comp.becomes(ConfigureContent(dbrow))
#r = comp.call(util.Ask("does she love me?"))
#r = comp.becomes(util.Ask("does she love me?"))
print "ask does finish?. "

@presentation.render_for(NewContent)
def render(self, h, comp, *args):

for le in self.data()[:100]:
#print "rowdict={0}".format(row)
with h.div:
with h.form.post_action(self.config, comp, le):
h << h.input(type='Submit', value='Delete')
h << h.input(value=le.File_ID, disabled=True)
h << h.input(value=le.File_Name, disabled=True)
h << h.input(value=le.Line_Start, disabled=True)
h << h.input(value=le.Content, disabled=True)
h << h.input(type='Submit', value='Configure')

return h.root


class ConfigureContent(object):
"""Ask the user to enter a line of text

The text entered is answered
"""
def __init__(self, database_row):
"""Initialization

In:
- ``msg`` -- message to display
"""
self.dbrow = database_row
print "Config init complete"

@staticmethod
def cleancheck():
print "Dirty check {0}".format(database.session.dirty)

def answer(self, comp, r):
self.cleancheck()
print "R={0}. dbrowdict={1}".format(
r(), self.dbrow.__dict__)
self.dbrow.Content = r()
print "R={0}. dbrowdict={1}".format(
r(), self.dbrow.__dict__)
self.cleancheck()
--
You received this message because you are subscribed to the Google Groups "Nagare users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nagare-users+***@googlegroups.com.
To post to this group, send email to nagare-***@googlegroups.com.
Visit this group at http://groups.google.com/group/nagare-users.
For more options, visit https://groups.google.com/d/optout.
Alain Poirier
2015-05-16 14:05:56 UTC
Permalink
Post by Terrence Brannon
The NewContent.data() method retrieves database rows. The render
method for this class creates a form out of each row. When the form is
submitted the user can input a new value for the Content field of the
database row.
In ConfigureContent.answer() I attempt to update the database
row. However even though the database row is updated in memory, (1)
database.session does not know that the session is dirty and should be
commited (2) the database does not get updated.
How can I alter this code to update the database? I'm aware that I
could query the database and update, but that seems absolutely
wasteful as I already have the object here.
I found the hooks to pickle the SQLAlchemy entities have changed in version >=0.9

So, to have your code running fine with a new version of SQLAlchemy you can:

- downgrade the version of SQLAlchemy: easy_install -U 'sqlalchemy<0.9'

or

- upgrade Nagare dev. to fetch my latest commit: easy_install -U nagare
Post by Terrence Brannon
pass
return database.session.query(
models.OBFL_Log_ParsingErrors).filter(
models.OBFL_Log_ParsingErrors.Type_Of_Record == 'Error')
print "Calling config"
comp.becomes(ConfigureContent(dbrow))
#r = comp.call(util.Ask("does she love me?"))
#r = comp.becomes(util.Ask("does she love me?"))
print "ask does finish?. "
@presentation.render_for(NewContent)
#print "rowdict={0}".format(row)
h << h.input(type='Submit', value='Delete')
h << h.input(value=le.File_ID, disabled=True)
h << h.input(value=le.File_Name, disabled=True)
h << h.input(value=le.Line_Start, disabled=True)
h << h.input(value=le.Content, disabled=True)
h << h.input(type='Submit', value='Configure')
return h.root
"""Ask the user to enter a line of text
The text entered is answered
"""
"""Initialization
- ``msg`` -- message to display
"""
self.dbrow = database_row
print "Config init complete"
@staticmethod
print "Dirty check {0}".format(database.session.dirty)
self.cleancheck()
print "R={0}. dbrowdict={1}".format(
r(), self.dbrow.__dict__)
self.dbrow.Content = r()
print "R={0}. dbrowdict={1}".format(
r(), self.dbrow.__dict__)
self.cleancheck()
--
You received this message because you are subscribed to the Google Groups "Nagare users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nagare-users+***@googlegroups.com.
To post to this group, send email to nagare-***@googlegroups.com.
Visit this group at http://groups.google.com/group/nagare-users.
For more options, visit https://groups.google.com/d/optout.
Loading...