Error trying to modify data with python code in plone

Hi,
I am new to Plone. I have some python code written to modify data and I am getting errors.
The line that produces the error is

obj.setLast_name2()

The error is
• <PythonScript at /yc1/test2>
Line 38
• Module AccessControl.ImplPython, line 675, in guarded_getattr
AttributeError: setLast_name2

I have been googling this and I came across something that said you have to use an external method to have full permissions to run certain code. Later I came across another posting that said external methods are obsolete and one should use views. I am quite confused right now. Can someone point me in the right direction and how I should go about doing it?

Full Python code:
from AccessControl import getSecurityManager
from StringIO import StringIO
from Products.CMFCore.utils import getToolByName
membership = getToolByName(context, 'portal_membership')
username = context.portal_membership.getAuthenticatedMember().getUserName()
print username
buf = StringIO()
itemp = context.portal_catalog.queryCatalog({
"portal_type":("Staff","Faculty"),
"path":"/yc1/portal_college/rnunez",
"sort_on" : "created" })

items = list(itemp)
context.manage_setLocalRoles(username, ['Manager', 'Owner','Editor'])
#context.manage_permission('Edit', roles=['Manager', 'Owner','Editor'], acquire=True)
print >> buf, "Found %d items to be Updated" % len(items)
getSecurityManager().checkPermission('View', context)
roles2 = [r['name'] for r in context.rolesOfPermission('View') if r['selected']]
#context.portal.rolesOfPermission('Modify portal content')
count = 0
for b in items:
count += 1
obj = b.getObject()
#obj.declareObjectPublic() # like roles = ACCESS_PUBLIC
#print >> buf, "updating:" + obj.absolute_url()
#obj.at_post_edit_script
m=obj.getLast_name()
print m
obj.setLast_name2()
m=obj.getLast_name()
print m

return buf.getvalue(), printed

Full Error:

We’re sorry, but there seems to be an error…
Here is the full error message:
Display traceback as text
Traceback (innermost last):
• Module ZPublisher.Publish, line 138, in publish
• Module ZPublisher.mapply, line 77, in mapply
• Module ZPublisher.Publish, line 48, in call_object
• Module Shared.DC.Scripts.Bindings, line 322, in call
• Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
• Module Products.PythonScripts.PythonScript, line 344, in _exec
• Module script, line 38, in test2
<PythonScript at /yc1/test2>
Line 38
• Module AccessControl.ImplPython, line 675, in guarded_getattr
AttributeError: setLast_name2

Hi,

the code formatting here is a bit broken, so maybe you can also provide a pastie.org link with the relevant code.

But anyway, as far as I see, you have no permission problem. You just want to call a method on an object, that doesn't exisst. Check the schema definition of your contentn type you want to modify, if it includes a field "last_name2" with no custom mutator attribute set.

Hth, Jörg

Some of us still use external methods because they're much easier to create than browser views.

Thank you everyone for your helpl. I finally got it to work.

Could you post your solution for future reference?

Here is what I did from the Linux command prompt

I created a Python program UpdateCVPY.py and placed it in the directory that contained the Faculty Object code.

from zope import interface
from zope import component
import DateTime
import zExceptions
import transactions
class updateCVClass(object):
def_init_(self, context, request):
self.context=context
self.request=request
def updateCV(self):
context=self.context.aq_inner
count=0
items = list(context.portal_catalog.queryCatalog({
"portal_type":("Faculty"),
"path":"/yc1/portal_college/",
"sort_on" : "created" }))
for b in items

		obj=b.getObject()
		d='dummyvalue'
			try:
			transaction.begin()
                            #the follwing are methods that I have in the Faculty Object Class that I am calling in a file
                            #called Faculty.py that is located in the same directory.
  				print obj.getADStatus()
  				obj.setFirst_name(d)
  				obj.setLast_name(d)
  				obj.setTeachingTitle(d)
 				obj.setOtherTitle(d)
 				obj.setEmail(d);
 				obj.setOfficePhone(d)
  				obj.setOfficeLocation(d)
  				obj.setDept(d);
  				obj.setEmployeeType(d)
  				obj.setDivision(d)
  				obj.setCurrentStatus(d)
  				obj.setOrgStatus(d)
  				print obj.getFirst_name()
  				print '\n'
  				print obj.getLast_name()
  				obj.setLast_namek('budwa')
  				print "obj lastname="+obj.getLast_name()
				count=count+1
			print count
            	        transaction.commit()
			except Exception:
  				#pass
  				print Exception

		return "Done"
def__call__(self):
		return self.updateCV()

I modified the configure.zcml file located in the directory that contained the Faculty Object code to add the following:

<browser:page
name="updateCV" #name to use to call script from Plone web site
for="Products.CMFCore.interfaces.ISiteRoot"
permission="cmf.ManagePortal"
class=."updateCVPY.updateCVClass" #name of .py file + name of class
/>

I restarted my zeoserver client (./client1 fg)

From the web browser, I went to my site and logged in and then typed the following to call the python code:

http://mywebsite:8080/yc1/UpdateCV

1 Like