email question, and mysterious variable behavior

17 views
Skip to first unread message

joe

unread,
Jul 25, 2012, 3:29:57 PM7/25/12
to web...@googlegroups.com
Hello

Two problems.  First, I have no experience whatsoever with the mail system, but I need to use it for a request/notification system.  (when the status of a run changes to what a user wanted it to change to, the user gets an email).  The email is not being sent.  I set up email like this:

from gluon.tools import Mail
mail = Mail()
mail.settings.server = 'http://127.0.0.1:8000'
mail.settings.sender = 'mye...@emailclient.com'

Then I call this method in my index function, which is being accessed every time a run is updated, to check for updates, and wee if they match:

def check_reqnot():
    for row in db().select(db.request_notification.ALL):
        if not row.notified:
            if row.notify_when == db.run[row.run].status:
                row.notified = True
                mail.send('test...@gmail.com',
                    'WOW!',
                    'It Worked! (HOLY CRAP)')
                response.flash = 'It should have worked'

It should be working, because when the status matches the request, I see the flash response, but the email is not sent (yes, I checked my junk mail folder).

The other odd thing is that notified stays false.

For reference here is the request_notification table:

db.define_table('request_notification',
Field('run', db.run, requires = IS_NOT_EMPTY()),
Field('notify_when', requires = IS_IN_SET(['Requested', 'In Proccess', 'Completed'])),
Field('notified', 'boolean', default = False, readable = False),
Field('user_signature', db.auth_user, compute = lambda row: auth.user_id))

And the relevant field form the run table:
Field('status', requires = IS_EMPTY_OR(IS_IN_SET(['Requested', 'In Proccess', 'Completed']))),

Niphlod

unread,
Jul 25, 2012, 3:42:42 PM7/25/12
to web...@googlegroups.com
mail.settings.server needs to be ('logging' for debugging purposes or) an ip(:port) where a mail daemon is listening, not the http address of a web2py instance......

And again (this rises up quite a bit): DAL is not an ORM. DAL is a Database Abstraction Layer, not an Object Relational Mapper.
With this statement I mean "your modifications to objects doesn't get automatically propagated to the database"

db(something).select() return a Rows() object.

This is a list of  dictionaries accessible both as db['tablename']['field'] AND by db.tablename.field.

When you change a value in this dict, you changed it ONLY in the object itself. This doesn't save the results in the db automatically.

This is why you end up having "notified" always false on the next select.

If you want to do that you have to call it like this:

row.update_record(notified=True).

Actually, I'd put it AFTER the mail is sent, so you update the notified field only in the case the email is actually sent (queued to the mail daemon)

Anthony

unread,
Jul 25, 2012, 3:44:03 PM7/25/12
to web...@googlegroups.com
from gluon.tools import Mail
mail = Mail()
mail.settings.server = 'http://127.0.0.1:8000'
mail.settings.sender = 'mye...@emailclient.com'

You also need:

mail.settings.login = 'username:password'

                row.notified = True
 
The other odd thing is that notified stays false.

Changing row.notified only changes it in the Row object -- it does not update the record in the database. You have two options:

row.notified = True
row
.update_record()

or:

row.update_record(notified=True)


Anthony

joe

unread,
Jul 25, 2012, 4:03:23 PM7/25/12
to web...@googlegroups.com
Thanks, the variable thing works now.  Also, if the server is 'logging', will the email be sent?  Sorry, I have never used this before.

Thanks for the help
-Joe peacock

Niphlod

unread,
Jul 25, 2012, 4:10:33 PM7/25/12
to web...@googlegroups.com
'logging' gets the raw email printed on the console. it is only for debugging purposes.

BTW (and I mean no harm on that): I get that asking on such an active list is "speedier" than having to read documentation, but since you are going to use the feature, why don't you just go and read about it on http://web2py.com/books/default/chapter/29/8 ?

Reply all
Reply to author
Forward
0 new messages