HowTo use methods in proteus

Hi,

I write a custom class method on trytond. I would like to try it on proteus but I’m not sure how to use it.

The method is on party.party

@classmethod
def get_party_invoices_last_month(cls, parties):
      # the method is processed

from decimal import Decimal
from proteus import config, Model

from jsonrpclib import Server as ServerProxy
import jsonrpclib
import json
import base64

Tryton server connection settings

HOST = ‘127.0.0.1’
PORT = ‘8000’
DB = ‘testdb’
USER = ‘admin’
PASSWORD = ‘mypass’

if __name__ == '__main__':
    config.set_xmlrpc(
        url='http://{user}:{password}@{host}:{port}/{db}/'.format(
            user=USER, password=PASSWORD, host=HOST, port=PORT, db=DB))

    Party = Model.get('party.party')
    party = Party(name='Customer')
    invoices = Party.get_party_invoices_last_month(party)

With these code I should receive an empty list, but I receive the follow message:
TypeError: cannot marshal <class ‘set’> objects

Thanks in advance!

Proteus emulates Tryton client. So you can only access to model fields and buttons.
To access to this method you must:

  • add modelview.button decorator to the method.
  • add method name to model button’s list.
  • use it in Proteus as party.click(‘get_party_invoices_last_month’).

Or the method must be declared in Model.__rpc__.

You can also add the method to RPC and then it will be possible to call it directly from proteus. This way the method is also avaiable from json-rpc and xml-rpc API calls and also from tryton and sao.

The method suggested by @sergyo works fine, but if the method has extra parameters, in these case selected_date, I can not use click anymore.

@classmethod
def get_party_invoices_last_month(cls, parties, selected_date=None):
      # the method is processed

I added the method to Model.__rpc__ like @ced suggested:

cls.__rpc__.update({
            'get_party_invoices_last_month': RPC(readonly=True, instantiate=0),
        })

I called it in the file with

Sale.get_party_invoices_last_month([party])

But I receive the follow message:
raise TypeError("cannot marshal %s objects" % type(value))
TypeError: cannot marshal <class 'set'> objects

If a use the follow code

Sale.get_party_invoices_last_month([party.id])

The follow message is displayed

Traceback (most recent call last):
File "/opt/lib/python3.6/site-packages/trytond/protocols/dispatcher.py", line 171, in _dispatch = rpc.convert(obj, *args, **kwargs)
File "/opt/lib/python3.6/site-packages/trytond/rpc.py", line 44, in convert for key in list(context.keys()):
`xmlrpc.client.Fault: <Fault 255: "'list' object has no attribute 'keys'">`

I tried the method on tryton and sao an works fine but in proteus doesn’t work.

Can you give me some example of this RPC @pokoli?

When calling directly a method with proteus, you must always add the context as last parameter.
So in your example, it should be:

Sale.get_party_invoices_last_month([party.id], config.context)

Thanks @ced, it works!

I only need to add the follow line
current_config = config.get_config()

And call the method with

Sale.get_party_invoices_last_month([party.id], current_config.context)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.