Calling method of Browserview from a zmi script

Hi,
I have the following browserview

from Products.Five.browser import BrowserView

class hashView(BrowserView):
def init(self,context,request):
self.context=context
self.request=request
#return "hi"
self.avariable="hello, i worked"
#def call(self):
# return "Hash without arguments work and no self"
def getHash(self,param):
return param+"test param"
def getHash2(self):
return "getHash 2 worked"

I would like to call the getHash and getHash2 method from a script in the zmi
If I try this
retval = context.unrestrictedTraverse('@@hashview/getHash2')

I get Insufficient Priviledges

I am able to obtain the values of getHash2 from a page template with the following

method with no parameters works

But, If i try to obtain the values of getHash, which takes a parameter using the following, nothing is returned.

< tal:content="python:hashview.getHash("passing parameter")" />

My configure.zcml has the following

<browser:page
name="hashview"
for="*"
class=".views.hashView"
permission="zope2.Public"
/>

Your help will be appreciated

Format your code properly, plz

I have a feeling that this is a weird approach.
Why / what are you trying to do this in a script?

Not what you asked for, but in the browserview.pt you get the result with:

 <div tal:content="view/getHash" /> 

... or

<div>${view/getHash}</div>

It is also possible to define a view for the getHash, too (by using the attribute:

   <browser:page
            name="gethash"
	for="*"
	class=".views.hashView"
	attribute="getHash"

which should return what you ask for with /@@gethash

PS: taken from memory

Thank you for responding. I want to call it from a script, because the script will do some calculations and pass parameters to getHash. Also, If I choose to call getHash from the pagetemplate, How would I pass parameters to it?

I had this function as an External Method that I can call from various zmi scripts. External Methods are now deprecated and everything I read seem to indicate that you have to use a Browserview instead.
So, I have the code setup in a browserview. I can call it from a page template, but I do not know how to pass parameters to it from a page template. I also do not know how to call it from a zmi script.

It might have been better if you explained WHAT you are trying to do instead of HOW you are planning to do it.

  1. A browser view can (probably) do all the calculations that a script can do

  2. You can have several browserviews for the same content

  3. "With Urls" you can pass parameters to a browserview like this http://path/to/content?parameter1=something&parameter2=something_else

  4. Browserviews can be templates that returns some html where

    ${view/getHash}

would render whaterver you have defined in

def getHash(self, something=None):
      do_some_calculations.....
      return something
  1. Browserviews can be "python classes" that just returns something, maybe this can be useful:

https://docs.plone.org/develop/plone/misc/context.html

@kamarjit you can use this I think

view = request.restrictedTraverse('@@myview')
result = view()

I think you can also do the multiadapter call yourself

I am trying that, but I am getting insufficient priviledges

For which are you getting insufficient privileges on restrictedTraverse(), or on call of a method of your view? In either case, you might need to make adjustments to your ZCML (either to permission or to allowed_attributes).

Sorry for repeating myself, but could you maybe try to explain what you are trying to do?

(what is your script / browserview supposed to do ?)

My script is supposed to take a few parameters and return a dictionary that contains a hashvalue and a timestamp. I cannot write the script in the zmi itself because it is importing hmac and time.

I am getting insufficient priviledge on the method call.

You probably need to add security annotations to the methods you want to call.
see https://docs.zope.org/zope2/zdgbook/Security.html and https://docs.plone.org/develop/plone/security/sandboxing.html

Or you white list the functions you want to use in collective.trustedimports then use those imports directly in your restrictedpython.

I am probably misunderstanding something here: why can you not do this in the browserview and let that return the dictionary.

... or is this unsafe ?

Because once the dictionary is returned. The zmi script that is calling the browserview has to act on the returned data before passing it to the pagetemplate.

I already have everything in place and working where the code is an external method. I don't want to rebuild everything. I just want to convert the external method to a BrowserView and call the various methods in it. We have many clients running Plone, an external method has to be copied to each everytime there is a change.

what do you have inside the __init__() method? read this:

https://docs.plone.org/develop/plone/views/browserviews.html#view-init-method-special-cases