Tim Black schrieb:
> I'm trying to use TurboGears' scheduler to automate sending bills by 
> email.  I have this code as a controller method:
> 
>     def send1MonthBills(self):
>         '''Email bills for projects marked billable where frequency is 1 
> Month'''
>         projects = model.Project.select(model.Project.q.billable==True)
>         for p in projects:
>             if p.billFrequency.frequency=='1 Month':
>                 self.emailBill(p.id)
>                 print "emailed bill for %s" % p.name
>     scheduler.add_monthday_task(action=send1MonthBills, 
> taskname='send1MonthBills', monthdays=[1], timeonday=(9,0))
> ##    scheduler.add_interval_task(action=send1MonthBills, 
> taskname='send1MonthBillsTest', initialdelay=10, interval=60)
> 
> Since it's a controller method, send1MonthBills() requires 1 argument:  
> self, and it references the controller class (Root()) when it calls 
> self.emailBill(), which is @exposed in order to use a template to make 
> the email pretty.
> 
> The problem is, scheduler.add_monthday_task(action=send1MonthBills... 
> doesn't let me enter "self" as an argument to send1MonthBills, so I get 
> this error:
> 
> ERROR DURING TASK EXECUTION send1MonthBills() takes exactly 1 argument 
> (0 given)
> ...
> TypeError: send1MonthBills() takes exactly 1 argument (0 given)
> 
> So, is there a way to either:
> 
> - give send1MonthBills() the "self" argument? I don't think there is.
> - or to call self.emailBill() from outside the controller class, but 
> within controllers.py?  I think this is what I need to do, but can't 
> find documentation on it.  I tried importing cherrypy then calling 
> cherrypy.root.emailBill() and it didn't work.

There are ways to accomplish that. You actually should be able to call a 
method on the root controller, or pass a callback-method that has self 
already attached (google bound method). If cherrypy.root is the 
root-controlle-instance, you should get a hold on the method with a simple

cherrypy.root.send1MonthBills

which you could

However, this is not going to work for other reasons: your exposed 
emailBill-method won't render because calling it won't invoke the whole 
template rendering mechanism.

I did this once like this:

         message = 
turbomail.Message(turbogears.config.get('events.from'), customer.email, 
subject)

         kid_engine = turbogears.view.engines['kid']
         message.rich = unicode(kid_engine.render(params, 
template='varms.templates.sales.events_email'), 'utf-8')

         turbomail.enqueue(message)


HTH,

Diez



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to