[Zope3-Users] Re: Function that is called when object is fully instantiated

Florian Lindner mailinglists at xgm.de
Mon Nov 27 15:58:38 EST 2006


Am Dienstag, 21. November 2006 22:45 schrieb Fred Drake:
> On 11/21/06, Florian Lindner <mailinglists at xgm.de> wrote:
> > Ok, I've added set_before_add="some of my schema fields" to the addform
> > directive and it works. But now I get:
>
> Ouch.  My first guess would be that there's some subtle interaction
> between your code and when other aspects of the request are handled,
> but that's not much to go on; sorry.

Mmhh... this is my complete code:


from interfaces import IJabberClient
from zope.interface import implements

from persistent import Persistent
from zope.app.container.contained import Contained

import xmpp


class JabberClient(Persistent, Contained):
    implements(IJabberClient)
    
    JID = u""
    password = u""
    connectOnStartup = True
    
    status = u"offline"

    
    def finishInitialization(self):
        """ Finish the rest of the initialziation that can't be done in 
__init__. """
        if self.connectOnStartup:
            self.setStatus("available")
        else:
            self.setStatus("offline")

    
    def setStatus(self, newStatus):
        if self.status=="offline" and newStatus == "available":
            self.jabberID = xmpp.protocol.JID(self.JID)
            self.client = xmpp.Client(self.jabberID.getDomain(), debug=[])
            self.client.connect()
            self.client.auth(self.jabberID.getNode(), self.password)
            self.client.sendPresence(self.jabberID)
            
        if self.status == "available" and newStatus == "offline":
            # disconnect
            pass
            
        self.status = newStatus
        
    def getStatus(self):
        return self.status
    
    def sendMessage(self, message, toJID):
        if status == "available":
            self.client.send(xmpp.protocol.Message(toJID, message))

finishInitialization is called from the IObjectAdded handler. When I comment 
the setStatus call out it works. But I see nothing that my change the inner 
workings of the object in some bad way... Do you see something?

Below is my interface declaration:

from zope.interface import Interface
from zope.schema import TextLine, Password, Bool

class IJabberClient(Interface):
    JID = TextLine(
                        title = u"Jabber ID",
                        description = u"Jabber ID (user at example.com). You need 
to reconnect in order to take effect.",
                        required = True )
                        
    password = Password(
                        title = u"Password",
                        description = u"Password for the jabber account",
                        required = True )
    
    connectOnStartup = Bool(
                        title = u"Connect on startup",
                        description = u"Automatically connect when Zope starts 
and also when the object is newly added (what you are about to do).",
                        default = True)
                        
    def setStatus(status):
        """ Sets the status. Can be "available" or "offline". """
        
    def getStatus():
        """ Get the status. """

    def sendMessage(message):
        """ Sends a message. """

Thanks,

Florian


More information about the Zope3-users mailing list