# Add a button to sort invoice lines

**URL:** https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153
**Category:** Developer
**Created:** [April 3, 2024, 2:48pm UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153 "2024-04-03T14:48:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mstma](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@mstma](https://discuss.tryton.org/u/mstma)
#### Post date: [April 3, 2024, 2:48pm UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153/1 "2024-04-03T14:48:01Z")

</div>

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_

```auto
<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_

```auto
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))

```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [April 3, 2024, 2:55pm UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153/2 "2024-04-03T14:55:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mstma](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@mstma](https://discuss.tryton.org/u/mstma)
#### Post date: [April 3, 2024, 4:21pm UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153/3 "2024-04-03T16:21:50Z")

</div>

I followed the example “Extend model” in the modul tutorial ([Extend model — Tryton server](https://docs.tryton.org/latest/server/tutorial/module/extend.html#extend-model)). Here is my _ **init**.py_ :

```auto
from trytond.pool import Pool
from . import invoice

__all__ = ['register']

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

```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [April 3, 2024, 4:22pm UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153/4 "2024-04-03T16:22:56Z")

</div>

> [@mstma](#):
>
> `'sort-invoice-grouping'`

This does not seem to be a valid module name.

---

<div class="post-metadata">

### Author: ![mstma](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/m/d78d45/32.png) [@mstma](https://discuss.tryton.org/u/mstma)
#### Post date: [April 4, 2024, 5:23pm UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153/5 "2024-04-04T17:23:29Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [April 5, 2024, 5:23am UTC](https://discuss.tryton.org/t/add-a-button-to-sort-invoice-lines/7153/6 "2024-04-05T05:23:30Z")

</div>

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