Add a button to sort invoice lines

I have the same problem as @shafi12 when trying to add an action “sort_lines” to the account_invoice module. Please have a look at my code. I get the error “Forbidden 403” when selecting the button in the Tryton client (first I just tried to make log entry when selecting the action button).

invoice.xml

<tryton>
  <data>
    <record model="ir.ui.view" id="invoice_view_form">
      <field name="model">account.invoice</field>
      <field name="inherit" ref="account_invoice.invoice_view_form" />
      <field name="name">invoice_form</field>
    </record>
    <record model="ir.model.button" id="invoice_sort_lines">
      <field name="name">sort_lines</field>
      <field name="string">Sort lines</field>
      <field name="model" search="[('model', '=', 'account.invoice')]" />
    </record>
  </data>
</tryton>

invoice.py

from trytond.model import fields, ModelView, Workflow
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval

import logging

logger = logging.getLogger(__name__)


class Invoice(metaclass=PoolMeta):
    __name__ = 'account.invoice'

    sort_state = fields.Selection([
        ('unsorted', "Unsorted"),
        ('sorted', "Sorted")
        ],
        "Sort state", required=True,
        readonly=True, sort=False
        )

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls._transitions.update({
            ('unsorted', 'sorted'),
            })
        cls._buttons.update({
           'sort_lines': {
                'readonly': Eval('sort_state') == 'sorted',
                'depends': ['sort_state']
                }
        })

    @classmethod
    def default_sort_state(cls):
        return 'unsorted'

    @classmethod
    @ModelView.button
    def sort_lines(cls, invoices):
        for invoice in invoices:
            logger.info("sorting invoice: id {}: recepient {}".format(invoice.id, invoice.party.name))

For me you Invoice class is not correctly registered in the Pool so the final account.invoice object does not contain the button which raises FORBIDDEN when being called.

I followed the example “Extend model” in the modul tutorial (Extend model — Tryton server). Here is my init.py :

from trytond.pool import Pool
from . import invoice

__all__ = ['register']

def register():
    Pool.register(
        invoice.Invoice,
        module='sort-invoice-grouping', type_='model')

This does not seem to be a valid module name.

Thanks, that was the error. I changed the module name from sort-invoice-grouping to sort_invoice_groupint and it works; so no dashes, only underscore in the module name! The register call is now:

def register():
    Pool.register(
        invoice.Invoice,
        module='sort_invoice_grouping', type_='model')

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