[Zope] Re: How can I import this module in python script ?????

Tres Seaver tseaver at palladion.com
Mon Feb 26 09:41:29 EST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ian at inpa.gov.br wrote:

This issue is more on-topic for the 'zope-cmf at zope.org' list.

> I am playing around trying to make something happen on a workflow state
> change. Beacause I cannot import the module I need in the python script I
> am getting an error:
> global name 'getMemberById' is not defined
> how can I import this module or better still code this so I am not
> importing anything at all (i imagine the restrictions on importing modules
> was done for a reason).

Yes, but that's a decoy here:  you don't really need to import a module;
 you just need to get an *instance* which happens to have its class
defined in that module.  In fact, you already *have* got hold of that
instance (see below).

The reason imports are restricted in "untrusted" code is that the
standard library includes modules whose APIs are dangerous (e.g.,
imaging 'shutils.rmtree').  So, in order to use a filesystem module in
TTW code, you must first make a declaration in Zope filesystem code
which enables it.

> #import the module we need
> from Products.CMFCore.utils import getToolByName
> 
> # Get the content object we're publishing
> review_state.object
> 
> # How can I import this module?????
> #from Products.CMFDefault.MembershipTool import MembershipTool
> 
> mt = getToolByName(context, 'portal_membership')
> 
> listed_ids = mt.getRoster()
> 
> contentObject = review_state
> 
> 
> # Start with an empty list, ready to be filled with the addressed
> # of people we're dispatching to
> mailList=[]
> 
> 
> mailhost = context.MailHost
> mailhost = getattr(context, 'MailHost', None)
> 
> 
> # Iterate through all the site's users
> # context.portal_membership.listMembers():
> for id in listed_ids:
>     member = getMemberById(id)
      member = mt.getMemberById(id) # use the tool

>     email = member.getProperty('email')
> 
>     # add them to the list of people we're emailing
>     mailList.append(email)
> 
>     mMsg = 'Crop notification sent'
>     mTo = email
>     # and send it
>     mailhost.send(mMsg, mTo, 'someone at somewhere.com', 'New Crops available')
> 
> # The change in indentation signals the end of the loop, so we've
> # now sent all the emails. Let's now send a confirmation that we've done it.
> 
> # We'll be building the email as a string again, but we have to convert our
> # list data elements into a string before we can append the information
> 
> recipients = string.join(mailList)


Tres.
- --
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4vGZ+gerLs4ltQ4RAk9TAKC65LdOamnCo9skSiBM+MZBpo7pZgCggCi5
hKOvEX/Q5QIBPeTXTlSVGuc=
=d/V9
-----END PGP SIGNATURE-----



More information about the Zope mailing list