2016-11-24 70 views
1

我正在尝试为我的自定义模型'product_images.product_image'实现Odoo发布工作流。Odoo中工作流模型的状态

我的模式是这样的:

# product_images/models/models.py 
# -*- coding: utf-8 -*- 
from odoo import models, fields, api, tools 

class PublishingStatus(models.Model): 
    _name = 'product_images.publishing_status' 
    _description = 'Publishing status' 

    name = fields.Char(string="Name") 
    slug = fields.Char(string="Slug") 


class ProductImage(models.Model): 
    _name = 'product_images.product_image' 
    _description = 'Product image' 

    name = fields.Char(string="Alternative text") 
    product_id = fields.Many2one('product.product', string='Product', ondelete='set null', index=True) 
    original_image = fields.Binary(string='Original image') 

    @api.model 
    def _get_default_state(self): 
     return self.env['product_images.publishing_status'].search([['slug', '=', 'draft']]) 

    @api.model 
    def _get_all_states(self, groups, domain, order): 
     state_ids = self.env['product_images.publishing_status'].search([]) 
     return state_ids 

    state_id = fields.Many2one(
     'product_images.publishing_status', 
     string='Publishing status', 
     default=_get_default_state, 
     group_expand='_get_all_states', 
    ) 

    @api.multi 
    def action_set_to_draft(self): 
     self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'draft']]) 

    @api.multi 
    def action_request_for_approval(self): 
     self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'pending']]) 

    @api.multi 
    def action_approve(self): 
     self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'approved']]) 

    @api.multi 
    def action_reject(self): 
     self.state_id = self.env['product_images.publishing_status'].search([['slug', '=', 'rejected']]) 

然后,我必须为发布状态的一些数据记录:

<!-- product_images/data/data.xml --> 
<odoo> 
    <data> 
     <!-- explicit list view definition --> 
     <record model="product_images.publishing_status" id="product_images.publishing_status_draft"> 
      <field name="name">Draft</field> 
      <field name="slug">draft</field> 
     </record> 
     <record model="product_images.publishing_status" id="product_images.publishing_status_pending"> 
      <field name="name">Pending</field> 
      <field name="slug">pending</field> 
     </record> 
     <record model="product_images.publishing_status" id="product_images.publishing_status_approved"> 
      <field name="name">Approved</field> 
      <field name="slug">approved</field> 
     </record> 
     <record model="product_images.publishing_status" id="product_images.publishing_status_rejected"> 
      <field name="name">Rejected</field> 
      <field name="slug">rejected</field> 
     </record> 
    </data> 
</odoo> 

我也有一些记录,以创建工作流程,它允许之间切换发布状态:

<odoo> 
    <data> 
     <record model="workflow" id="product_images.wkf_image_publishing"> 
      <field name="name">Product Image Publishing Workflow</field> 
      <field name="osv">product_images.product_image</field> 
      <field name="on_create">True</field> 
     </record> 

     <record model="workflow.activity" id="product_images.wkf_activity_draft"> 
      <field name="name">Draft</field> 
      <field name="wkf_id" ref="product_images.wkf_image_publishing" /> 
      <field name="flow_start" eval="True" /> 
      <field name="kind">function</field> 
      <field name="action">action_set_to_draft()</field> 
     </record> 
     <record model="workflow.activity" id="product_images.wkf_activity_pending"> 
      <field name="name">Pending</field> 
      <field name="wkf_id" ref="product_images.wkf_image_publishing" /> 
      <field name="kind">function</field> 
      <field name="action">action_request_for_approval()</field> 
     </record> 
     <record model="workflow.activity" id="product_images.wkf_activity_approved"> 
      <field name="name">Approved</field> 
      <field name="wkf_id" ref="product_images.wkf_image_publishing" /> 
      <field name="flow_stop" eval="True" /> 
      <field name="kind">function</field> 
      <field name="action">action_approve()</field> 
     </record> 
     <record model="workflow.activity" id="product_images.wkf_activity_rejected"> 
      <field name="name">Rejected</field> 
      <field name="wkf_id" ref="product_images.wkf_image_publishing" /> 
      <field name="flow_stop" eval="True" /> 
      <field name="kind">function</field> 
      <field name="action">action_reject()</field> 
     </record> 

     <record model="workflow.transition" id="product_images.wkf_transition_draft_to_pending"> 
      <field name="act_from" ref="product_images.wkf_activity_draft" /> 
      <field name="act_to" ref="product_images.wkf_activity_pending" /> 
      <field name="condition">name != "" and original_image != ""</field> 
      <field name="signal">pending</field> 
     </record> 
     <record model="workflow.transition" id="product_images.wkf_transition_pending_to_draft"> 
      <field name="act_from" ref="product_images.wkf_activity_pending" /> 
      <field name="act_to" ref="product_images.wkf_activity_draft" /> 
      <field name="signal">draft</field> 
     </record> 
     <record model="workflow.transition" id="product_images.wkf_transition_pending_to_approved"> 
      <field name="act_from" ref="product_images.wkf_activity_pending" /> 
      <field name="act_to" ref="product_images.wkf_activity_approved" /> 
      <field name="signal">approve</field> 
     </record> 
     <record model="workflow.transition" id="product_images.wkf_transition_pending_to_rejected"> 
      <field name="act_from" ref="product_images.wkf_activity_pending" /> 
      <field name="act_to" ref="product_images.wkf_activity_rejected" /> 
      <field name="signal">reject</field> 
     </record> 
    </data> 
</odoo> 

现在是最麻烦的部分!我需要一个带按钮的表单来切换工作流状态和显示当前活动状态的状态栏。这是我的尝试:

<record model="ir.ui.view" id="product_images.form"> 
     <field name="name">Product Image</field> 
     <field name="model">product_images.product_image</field> 
     <field name="arch" type="xml"> 
      <form> 
       <header> 
        <!-- 
        <button name="draft" 
          type="workflow" 
          string="Set to draft" 
          attrs="{'invisible': [('state_id.slug','not in',['pending'])]}" 
        /> 
        <button name="pending" 
          type="workflow" 
          string="Request for approval" 
          attrs="{'invisible': [('state_id.slug','not in',['draft'])]}" 
        /> 
        <button name="approve" 
          type="workflow" 
          string="Approve" 
          attrs="{'invisible': [('state_id.slug','not in',['pending'])]}" 
          class="oe_highlight" 
        /> 
        <button name="reject" 
          type="workflow" 
          string="Reject" 
          attrs="{'invisible': [('state_id.slug','not in',['pending'])]}" 
          class="oe_highlight" 
        /> 
        --> 
        <field name="state_id" widget="statusbar" /> 
       </header> 

       <sheet> 
        <group> 
         <field name="product_id" /> 
         <field name="name" string="Alternative text" /> 
         <field name="original_image" widget="image" class="oe_avatar" /> 
         <field name="state_id" /> 
        </group> 
       </sheet> 
      </form> 
     </field> 
    </record> 

我的问题是:显示

  1. 的状态栏,但目前的发布状态没有被激活。
  2. 如果我取消的按钮,他们乱扔无效域错误:

Uncaught Error: Unknown field state_id.slug in domain [["state_id.slug","not in",["pending"]]]

我缺少什么?

回答

1

属性,我们不能在左边使用父字段。在你的情况下,我们需要添加相关字段。

例如:

在你的视图文件

<field name="slug" invisible="1"/> 

现在取消注释<button>码STATE_ID后。

之后,重新启动Odoo服务器并升级您的自定义模块。

+1

谢谢。有效。我只需要修改这些按钮:

+0

是的。不需要* state_id.slug *在* button attrs *中。 –