cundi / blog

push issues as a blog
2 stars 0 forks source link

创建和管理Odoo中的状态栏(下拉框)[翻译] #42

Open cundi opened 8 years ago

cundi commented 8 years ago

原文链接: http://www.odoo.yenthevg.com/creating-and-managing-statusbars-selections-in-odoo/

Creating and managing statusbars (selections) in Odoo

OCTOBER 21, 2015YENTHE666

Hi guys,

In this tutorial I will learn you how to create and manage statusbars. Statusbars are selections (on database level) which are visually respresented by Odoo. They give you the ability to follow a specific flow and can be very handy to show you the progress that has been made. After this tutorial you can create and manage your own statusbars.

In this example I will create a new form view which contains a statusbar and I’ll add buttons on the form which change the state of the record.

1. Creating the model and fields

The first step is to create a model (if you don’t have one yet) and to add a selection field:

class statusbar(models.Model):
    _name = 'statusbar.demo'
    name = fields.Char('Name', required=True)
    """
    This selection field contains all the possible values for the statusbar.
    The first part is the database value, the second is the string that is showed. Example:
    ('finished','Done'). 'finished' is the database key and 'Done' the value shown to the user
    """
    state = fields.Selection([
            ('concept', 'Concept'),
            ('started', 'Started'),
            ('progress', 'In progress'),
            ('finished', 'Done'),
            ],default='concept')

This creates a new model with the name ‘statusbar.demo’ which contains two fields, the field ‘name’ and the field ‘state’. The state field is of the type selection, which means it will contain multiple values which can be chosen. A selection is always built up in the same way, the first part if the database name and the second part is the text that will be shown to the user. For example:

('progress', 'In progress'),

If you wish to change the state to ‘In progress’ you will need to tell the database about ‘progress’, since this is the name the database listens to, while the name shown to the user will be ‘In progress’. This is all you need on the database level. You have your selection values in the database so you only need to show them to the user now!

2. Creating the view

Now that you have the database part ready it is time to create your view. In this example I will create a simple form view which shows the field ‘name’ and the statusbar with some buttons:

<record model="ir.ui.view" id="view_statusbar_form">
    <field name="name">Statusbar</field>
    <field name="model">statusbar.demo</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
    <form string="Workflow record">
    <!--The header tag is built to add buttons within. This puts them at the top -->
    <header>
        <button string="Set to concept" type="object" name="concept_progressbar" attrs="{'invisible': [('state', '=', 'concept')]}"/>
        <!--The oe_highlight class gives the button a red color when it is saved.
        It is usually used to indicate the expected behaviour. -->
        <button string="Set to started" type="object" name="started_progressbar" class="oe_highlight" attrs="{'invisible': [('state','!=','concept')]}"/>
        <button string="In progress" type="object" name="progress_progressbar" attrs="{'invisible': [('state','=','progress')]}"/>
        <button string="Done" type="object" name="done_progressbar" attrs="{'invisible': [('state','=','finished')]}"/>
        <!--This will create the statusbar, thanks to the widget. -->
        <field name="state" widget="statusbar"/>
    </header>
    <group>
        <field name="name"/>
    </group>
        </form>
    </field>
</record>

So what does this tell us? Let us break this down part by part. The first thing you will notice is the field header.

<header>
</header>

Everything within these tags will be shown at the top of your Odoo form. In this code example it will create four buttons and it will also show the field ‘state’. The field ‘state’ has a widget set to it that is made by Odoo and which will make the typical statusbar, just by adding a widget! This will generate the following bar at the top of your Odoo screen:

Statusbar and button example

Now there is one more thing I should explain, the buttons. By using those buttons we will modify in which state we are on the statusbar (selection) and we will control the whole flow. Let us take the button ‘In progress’ as an example to explain the code:

<button string="In progress" type="object" name="progress_progressbar" attrs="{'invisible': [('state','=','progress')]}"/>

So what does this do exactly? It gives the button the text ‘In progress’ and we’ll give the button the type object, this will make it accessible in a function from the Python code later on. The name of the button will be the name of the function in your Python code and last but not least is the attrs. The attrs is a handy feature to make buttons visible/invisible when you’re in a state for example. In this example the button ‘In progress’ will not be shown when the statusbar is in the state ‘progress’.

Tip: If you have multiple states where buttons should be hidden you can use domains! For example:

[(‘state’,’=’,[‘progress’,’finished’])]

Makes sense right? Let us continue and create the button functions!

3. Adding functions for the buttons

Now that the view is ready and you have buttons they should also trigger something. Open up your Python file and create a function for all of those button names you have. Every button will have a separate Python function and will modify the current record with a write. In this tutorial I will create four functions that simply change the state of what the current record is in:

#This function is triggered when the user clicks on the button 'Set to concept'
@api.one
def concept_progressbar(self):
    self.write({
        'state': 'concept',
    })

#This function is triggered when the user clicks on the button 'Set to started'
@api.one
def started_progressbar(self):
    self.write({
    'state': 'started'
    })

#This function is triggered when the user clicks on the button 'In progress'
@api.one
def progress_progressbar(self):
    self.write({
    'state': 'progress'
    })

#This function is triggered when the user clicks on the button 'Done'
@api.one
def done_progressbar(self):
    self.write({
    'state': 'finished',
    })

When you now click on the button ‘In progress’ it will trigger the function ‘progress_progressbar’. The self.write will be triggered, which is telling Odoo that it should write on the current record, and it will modify the state to ‘finished’. Depending on in which state you are you’ll see buttons showing up and dissapearing. This is controller by the attrs value on the buttons. For example when I save my record and set it to in progress:

Statusbars in progress

4. Conclusion

Statusbars (selections) are very handy in Odoo and give you the ability to show the progress to the user in a visual way. Statusbars are ideal to use in combination with buttons which will modify the state where a record is in. Another great thing about statusbars is that they’re always at the exact same location and you can find out what the progress is within seconds.

Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account.

Has this tutorial helped you, do you have any feedback or questions? Post away!